Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Variable with the same name as a submodule occasionally breaks semantic highlighting/hover text #750

Closed
apple1417 opened this issue Dec 16, 2020 · 2 comments
Labels
bug Something isn't working fixed in next version (main) A fix has been implemented and will appear in an upcoming version

Comments

@apple1417
Copy link

Environment data

  • Language Server version: 2020.12.2
  • OS and version: Windows 10 Version 10.0.18363 Build 18363
  • Python version (& distribution if applicable, e.g. Anaconda): 3.9.0

Expected behaviour

If a module exports a variable with the same name as one of it's submodules, pylance should always interpret it as a variable (just like python), and highlight it and show the relevant hover text accordingly.

Actual behaviour

If the first import from a submodule has the same name as the submodule, python overwrites it to the imported variable, while pylance still interprets it as a module.

Logs

Expand
User belongs to experiment group 'ShowPlayIcon - start'
User belongs to experiment group 'DebugAdapterFactory - experiment'
User belongs to experiment group 'PtvsdWheels37 - experiment'
User belongs to experiment group 'UseTerminalToGetActivatedEnvVars - control'
User belongs to experiment group 'AA_testing - control'
User belongs to experiment group 'LocalZMQKernel - experiment'
User belongs to experiment group 'CollectLSRequestTiming - control'
User belongs to experiment group 'CollectNodeLSRequestTiming - experiment'
User belongs to experiment group 'EnableIPyWidgets - experiment'
User belongs to experiment group 'DeprecatePythonPath - experiment'
User belongs to experiment group 'RunByLine - experiment'
User belongs to experiment group 'CustomEditorSupport - control'
User belongs to experiment group 'pythonaacf'
User belongs to experiment group 'pythonSendEntireLineToREPLcf'
User belongs to experiment group 'pythonInstallPylintButtonFirst'
Error 2020-12-17 11:03:13: Failed to create File hash for interpreter C:\Users\Administrator\Desktop\Python\python.exe [Error: ENOENT: no such file or directory, lstat 'C:\Users\Administrator\Desktop\Python\python.exe'] {
  errno: -4058,
  code: 'ENOENT',
  syscall: 'lstat',
  path: 'C:\\Users\\Administrator\\Desktop\\Python\\python.exe'
}
> conda --version
> pyenv root
> python3.7 c:\Users\Administrator\.vscode\extensions\ms-python.python-2020.12.424452561\pythonFiles\pyvsc-run-isolated.py -c "import sys;print(sys.executable)"
> python3.6 c:\Users\Administrator\.vscode\extensions\ms-python.python-2020.12.424452561\pythonFiles\pyvsc-run-isolated.py -c "import sys;print(sys.executable)"
> python3 c:\Users\Administrator\.vscode\extensions\ms-python.python-2020.12.424452561\pythonFiles\pyvsc-run-isolated.py -c "import sys;print(sys.executable)"
> python2 c:\Users\Administrator\.vscode\extensions\ms-python.python-2020.12.424452561\pythonFiles\pyvsc-run-isolated.py -c "import sys;print(sys.executable)"
> python c:\Users\Administrator\.vscode\extensions\ms-python.python-2020.12.424452561\pythonFiles\pyvsc-run-isolated.py -c "import sys;print(sys.executable)"
> py -3.7 c:\Users\Administrator\.vscode\extensions\ms-python.python-2020.12.424452561\pythonFiles\pyvsc-run-isolated.py -c "import sys;print(sys.executable)"
> py -3.6 c:\Users\Administrator\.vscode\extensions\ms-python.python-2020.12.424452561\pythonFiles\pyvsc-run-isolated.py -c "import sys;print(sys.executable)"
> py -3 c:\Users\Administrator\.vscode\extensions\ms-python.python-2020.12.424452561\pythonFiles\pyvsc-run-isolated.py -c "import sys;print(sys.executable)"
> py -2 c:\Users\Administrator\.vscode\extensions\ms-python.python-2020.12.424452561\pythonFiles\pyvsc-run-isolated.py -c "import sys;print(sys.executable)"
> python c:\Users\Administrator\.vscode\extensions\ms-python.python-2020.12.424452561\pythonFiles\pyvsc-run-isolated.py -c "import sys;print(sys.executable)"
> conda info --json
Error 2020-12-17 11:03:14: Detection of Python Interpreter for Command python3 and args  failed as file  does not exist
> python c:\Users\Administrator\.vscode\extensions\ms-python.python-2020.12.424452561\pythonFiles\pyvsc-run-isolated.py -c "import sys;print(sys.executable)"
> python c:\Users\Administrator\.vscode\extensions\ms-python.python-2020.12.424452561\pythonFiles\pyvsc-run-isolated.py -c "import sys;print(sys.executable)"
Error 2020-12-17 11:03:14: Detection of Python Interpreter for Command py and args -3.7 failed as file Python 3.7 not found!

 -3.9-64
 -2.7-64 does not exist
Error 2020-12-17 11:03:14: Detection of Python Interpreter for Command py and args -3.6 failed as file Python 3.6 not found!

 -3.9-64
 -2.7-64 does not exist
Starting Pylance language server.
Python interpreter path: ~\AppData\Local\Programs\Python\Python39\python.exe
Error 2020-12-17 11:03:14: Failed to get interpreter information for 'C:\Users\Administrator\Desktop\Python\python.exe' [Error: Command failed: "C:\\Users\Administrator\Desktop\Python\python.exe" "c:\Users\Administrator\.vscode\extensions\ms-python.python-2020.12.424452561\pythonFiles\pyvsc-run-isolated.py" "c:\Users\Administrator\.vscode\extensions\ms-python.python-2020.12.424452561\pythonFiles\interpreterInfo.py"
The system cannot find the path specified.

	at ChildProcess.exithandler (child_process.js:304:12)
	at ChildProcess.emit (events.js:223:5)
	at maybeClose (internal/child_process.js:1021:16)
	at Socket.<anonymous> (internal/child_process.js:430:11)
	at Socket.emit (events.js:223:5)
	at Pipe.<anonymous> (net.js:664:12)] {
  killed: false,
  code: 1,
  signal: null,
  cmd: '"C:\\\\Users\\Administrator\\Desktop\\Python\\python.exe" "c:\\Users\\Administrator\\.vscode\\extensions\\ms-python.python-2020.12.424452561\\pythonFiles\\pyvsc-run-isolated.py" "c:\\Users\\Administrator\\.vscode\\extensions\\ms-python.python-2020.12.424452561\\pythonFiles\\interpreterInfo.py"'
}
Error 2020-12-17 11:03:14: Failed to get interpreter information for 'C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps\python3.exe' [Error: Command failed: "C:\\Users\Administrator\AppData\Local\Microsoft\WindowsApps\python3.exe" "c:\Users\Administrator\.vscode\extensions\ms-python.python-2020.12.424452561\pythonFiles\pyvsc-run-isolated.py" "c:\Users\Administrator\.vscode\extensions\ms-python.python-2020.12.424452561\pythonFiles\interpreterInfo.py"
Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings > Manage App Execution Aliases.

	at ChildProcess.exithandler (child_process.js:304:12)
	at ChildProcess.emit (events.js:223:5)
	at maybeClose (internal/child_process.js:1021:16)
	at Process.ChildProcess._handle.onexit (internal/child_process.js:283:5)] {
  killed: false,
  code: 9009,
  signal: null,
  cmd: '"C:\\\\Users\\Administrator\\AppData\\Local\\Microsoft\\WindowsApps\\python3.exe" "c:\\Users\\Administrator\\.vscode\\extensions\\ms-python.python-2020.12.424452561\\pythonFiles\\pyvsc-run-isolated.py" "c:\\Users\\Administrator\\.vscode\\extensions\\ms-python.python-2020.12.424452561\\pythonFiles\\interpreterInfo.py"'
}
Error 2020-12-17 11:03:14: Failed to get interpreter information for 'C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps\python.exe' [Error: Command failed: "C:\\Users\Administrator\AppData\Local\Microsoft\WindowsApps\python.exe" "c:\Users\Administrator\.vscode\extensions\ms-python.python-2020.12.424452561\pythonFiles\pyvsc-run-isolated.py" "c:\Users\Administrator\.vscode\extensions\ms-python.python-2020.12.424452561\pythonFiles\interpreterInfo.py"
Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings > Manage App Execution Aliases.

	at ChildProcess.exithandler (child_process.js:304:12)
	at ChildProcess.emit (events.js:223:5)
	at maybeClose (internal/child_process.js:1021:16)
	at Process.ChildProcess._handle.onexit (internal/child_process.js:283:5)] {
  killed: false,
  code: 9009,
  signal: null,
  cmd: '"C:\\\\Users\\Administrator\\AppData\\Local\\Microsoft\\WindowsApps\\python.exe" "c:\\Users\\Administrator\\.vscode\\extensions\\ms-python.python-2020.12.424452561\\pythonFiles\\pyvsc-run-isolated.py" "c:\\Users\\Administrator\\.vscode\\extensions\\ms-python.python-2020.12.424452561\\pythonFiles\\interpreterInfo.py"'
}
> conda --version

Code Snippet / Additional information

I have a setup like this

demo\__init__.py:

from .test import test
from .test import something

demo\test.py:

test = 1
something = None

example.py:

from demo import test
print(type(test))

In example.py test gets highlighted/hover test as if it is a module, while in demo\__init__.py it's correctly highlighted as an int.

This particular issue only happens if from .test import test is the first import from the test submodule in demo\__init__.py. If you swap the order of the imports (or add a third beforehand) it changes slightly. In example.py test will be correctly highlighted as an int, while in demo\__init__.py it will now be highlighted as a module.

@jakebailey
Copy link
Member

jakebailey commented Dec 17, 2020

This seems like a bug. Normally, I'd say that this is difficult because you can import the module elsewhere and have a side effect that it's the module that's exposed, but every variation I've tried always has python saying that test is an int, never a module.

The original code will print (inserting a few prints for types):

E:\work\issues-pylance\issue750\demo\test.py: type(test) = <class 'int'>
E:\work\issues-pylance\issue750\demo\__init__.py: type(test) = <class 'int'>
example.py: type(test) = <class 'int'>

Create a module import_demo_test:

import demo.test

print(f"{__file__}: {type(demo.test) = }")

Then stick import import_demo_test either before from demo import test in example.py, and the output is always:

E:\work\issues-pylance\issue750\demo\test.py: type(test) = <class 'int'>
E:\work\issues-pylance\issue750\demo\__init__.py: type(test) = <class 'int'>        
E:\work\issues-pylance\issue750\import_demo_test.py: type(demo.test) = <class 'int'>
example.py: type(test) = <class 'int'>

Swapping the lines in __init__.py changes:

image

Into:

image

So there's some behavior about the order of assignments we aren't handling properly.

@jakebailey jakebailey added bug Something isn't working needs investigation Could be an issue - needs investigation labels Dec 17, 2020
@github-actions github-actions bot removed the triage label Dec 17, 2020
@erictraut erictraut added fixed in next version (main) A fix has been implemented and will appear in an upcoming version and removed needs investigation Could be an issue - needs investigation labels Dec 21, 2020
@jakebailey
Copy link
Member

This issue has been fixed in version 2021.1.0, which we've just released. You can find the changelog here: https://github.com/microsoft/pylance-release/blob/main/CHANGELOG.md#202110-6-january-2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed in next version (main) A fix has been implemented and will appear in an upcoming version
Projects
None yet
Development

No branches or pull requests

3 participants