vscode-python: Specifc relative import statement causing issues with VSC highlighting and 'jumping' functionality.
Environment data
- VS Code version: 1.47.3 x64 , although also tested on 1.48.0 Insider
- Extension version (available under the Extensions sidebar): v2020.7.96456
- OS and version: Windows 10 Pro Verision 1909 64-bit
- Python version (& distribution if applicable, e.g. Anaconda): Python 3.8.5 64-bit
- Type of virtual environment used (N/A | venv | virtualenv | conda | …): N/A
- Relevant/affected Python packages and their versions: N/A
- Relevant/affected Python-related VS Code extensions and their versions: N/A
- Value of the
python.languageServer
setting: Jedi
Steps to reproduce:
Example Directory: VSC_behaviour.zip
- Have the ‘Python’ extension installed.
- Download the Example Directory above, unzip, and open it with Visual Studio Code. You should arrive at this tree:
- This is the set-up for the testing for ‘Actual behaviour’ and ‘Expected behaviour’.
project
├── directory1
│ ├── __init__.py
│ ├── file2.py
│ └── file3.py
├── file1.py
├── run.py
Expected behaviour
I will describe the highlighting as ‘blue-highlighting’, as that’s what’s shown in my screenshots due to my theme, but presumably, there will be different colours for others.
1.1. Navigate to run.py
. Assuming default keybindings, hover your mouse over the file1
part of import file1
. It should highlight and underline in blue, shown below, and you should be able to left-click and ‘jump’ to file1
.
1.2. Type
file1.
inside run.py
, as if you were calling a function from file1
. You should be auto-suggested the do_nothing()
function, the behaviour of which I believe comes from the Python extension.
-
Now navigate to
file2.py
. Notice thefrom . import file3
. Hovering your mouse over file3 should result in highlighting/underlining/jumping (but it doesn’t - see Actual Behaviour). Moreover, writingfile3.
infile2.py
should suggest thedo_nothing()
function. -
Replacing
from . import file3
infile2.py
with an alternative syntax should also have blue-highlighting/underlining/jumping/function suggestions. For instance,from .file3 import *
should have this behaviour, as well asimport file3
, although the latter throws an editor error, but still has that highlighting/underlining and ‘jumping’.
Actual behaviour
Steps 1.1 and 1.2 show the expected behaviour (see images above). There is the expected blue-highlighting/underlining/jumping/function suggestions.
However, Step 2 does not show the expected behaviour. There is no blue-highlighting/underlining/jumping/function suggestions.
Perhaps this behaviour is due to my syntax, but my project appears to run fine with this step 2 syntax. This seems frustrating to me, as this syntax seems to be ‘legal’. Furthermore, if I manually write
file3.do_nothing()
, the function can run perfectly normally at execution, provided the function is ultimately called from run.py
, the Python file that’s ran for the project. You can see an example of this expected behaviour here. You should see the text ‘I’m working!’ on terminal.
VSC_Behaviour_run.zip
One possible theory I have is that the highlighting/etc. is being missed out due to the nature of the relative import. However, the highlighting/etc. work for another relative imports method, as shown in Step 3 below. Hence, I think that this specific relative import syntax could be causing conflicts with the Python extension for some reason, but I’m not sure why this might be occurring.
As mentioned above, the alternative import syntaxes in Step 3 do show the expected behaviour. There is highlighting/etc.
However, the reason I don’t the relative import
from .file3 import *
is because I’ve heard it’s generally considered bad practice. Furthermore, I cannot use import file3
, as this does not work as a relative import; when run.py
is ran, the import fails. I just showed it as an example. I’d like to still use the syntax used in Step 2 (from . import file3
) if possible, as it fits my current project specifications the best.
Also, I noticed that disabling the Python extension stopped the highlighting/etc. for all imports, which made me think that the Python extension could be linked to this behaviour, rather than VSC itself.
Thank you!
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 20 (10 by maintainers)
It is good to check with
jedi
. But I suspect it might be our usage of the Jedi API. We have plans on updating these and put Jedi behind LSP which should make use of the newerjedi
APIs (see #11995 ). But that is a large change and will take some time.