Closed haelmj closed 2 years ago
I'm not seeing a difference between the two. Here's what I see when I add/remove the -> None
return type annotation. If you're seeing something different, perhaps you could post a screen shot?
@erictraut That's odd, notice the change in syntax highlighting in the image below. If I take None, out it works fine but changes once I put it in
Strange. Let's see if someone else on the Pylance team can reproduce what you're seeing.
I have a partial theory about what might be happening here. Pyright, the type checker that Pylance is built upon, has some special techniques for dealing with classes whose constructors are completely unannotated. In such cases, it internally treats that class as though it's generic with each of the constructor input parameters representing a different type parameter. This improves type inference. For example, if you construct a new object nc = NetCat([1, 2], "")
, pyright will treat that as though it's a generic class instance with the type NetCat[list[int], str]
, and if you then access nc.args
, it will infer the type to be list[int]
. As soon as pyright sees a type annotation (even a return type annotation) on the __init__
method, it disables this technique, so accessing nc.args
will have an Any
type.
If my theory is on track, you should be able to repro the problem by adding a type annotation to any one of the __init__
parameters. For example, if you add a : int
after args
.
@erictraut So it seems, this isn't solely for classes but independent functions as well. Apparently, adding just parameter type annotations works just fine until a return type annotation is added in. I've attached some extra screenshots to show what I'm referring to.
It also seems to affect the syntax highlighting for strings within the same code block
Thanks for the additional experimentation. That disproves my theory.
Let's see if someone else on the Pylance team is able to replicate the behavior you're seeing.
I wasn't able to repro this.
@debonte Could you share a screenshot of what you did and the output you got?
I have further tested this on a Windows machine with the same result. That verifies that the issue isn't OS-related.
couldn't repo.
"Python Language Server" logs would help. https://github.com/microsoft/pylance-release/blob/main/TROUBLESHOOTING.md#filing-an-issue
@haelmj, which version of VS Code are you using? Is it the latest (or at least a recent) version?
@erictraut I've just tested this on a windows 10 and couldn't repro the issue
VSCode version: 1.64.2(user setup)
OS: Windows_NT x64 10.0.19044
However, the issue still appears when I test it on MacOS:
VSCode version: 1.64.2(Universal)
OS: Darwin arm64 20.5.0
@bschnurr I'll attempt the troubleshooting over the weekend and let you know how that goes.
@erictraut I verified that I have the latest versions of VSCode as well as Pylance. However, the behaviour still persists on the mac
@bschnurr Here are the logs:
[Info - 6:33:54 PM] (1838) Pylance language server 2022.2.4 (pyright 101e717a) starting
[Info - 6:33:54 PM] (1838) Server root directory: /Users/haelmj/.vscode/extensions/ms-python.vscode-pylance-2022.2.4/dist
[Info - 6:33:54 PM] (1838) No configuration file found.
[Info - 6:33:54 PM] (1838) No pyproject.toml file found.
[Info - 6:33:54 PM] (1838) Setting pythonPath for service "blackhatpython": "/Users/haelmj/Documents/Security/blackhatpython/venv3/bin/python"
[Warn - 6:33:54 PM] (1838) stubPath /Users/haelmj/Documents/Security/blackhatpython/typings is not a valid directory.
[Info - 6:33:54 PM] (1838) Assuming Python version 3.8
[Info - 6:33:54 PM] (1838) Assuming Python platform Darwin
[Info - 6:33:54 PM] (1838) Searching for source files
[Info - 6:33:54 PM] (1838) Auto-excluding /Users/haelmj/Documents/Security/blackhatpython/venv3
[Info - 6:33:54 PM] (1838) Found 4 source files
[Info - 6:33:54 PM] (1838) Background analysis(1) root directory: /Users/haelmj/.vscode/extensions/ms-python.vscode-pylance-2022.2.4/dist
[Info - 6:33:54 PM] (1838) Background analysis(1) started
[Info - 6:33:54 PM] (1838) Indexer background runner(2) root directory: /Users/haelmj/.vscode/extensions/ms-python.vscode-pylance-2022.2.4/dist (index)
[Info - 6:33:54 PM] (1838) Indexing(2) started
[Info - 6:33:55 PM] (1838) Indexer done(2). indexed 424 files
https://github.com/microsoft/pylance-release/blob/main/TROUBLESHOOTING.md#filing-an-issue
Please: Enable trace logging by adding "python.analysis.logLevel": "Trace" to your settings.json configuration file.
Adding this will cause a large amount of info to be printed to the Python output panel. This should not be left long term, as the performance impact of the logging is significant.
Select "View: Toggle Output" from the command palette (Ctrl+Shift+P on Windows/Linux, Command+Shift+P on macOS), then select "Python Language Server" in the dropdown on the right.
Please reopen this issue with additional logging as described if this is still an issue.
Environment data
Expected behaviour
When return type hint is added to function definition, syntax highlighting should remain consistent.
Actual behaviour
When return type hint is added to function definition, the syntax highlighting for the next function is disabled
Logs
Code Snippet / Additional information
In the following the second class method below, syntax highlighting on
def
will be disabled due to the return type hintNone