You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Apr 14, 2022. It is now read-only.
Repository navigation
This repository was archived by the owner on Apr 14, 2022. It is now read-only.
Undefined variable: 'str' as part of correct type annotation #1732
Ah, that's probably it, in which case this may actually be a class scoping issue along with missing dataflow analysis to know that the assignment comes after.
I think this issue is much broader as I'm getting a lot more false positives lately, where just editing a single character with valid python trips up the parser for the entire file. I reported it in the wrong place, though: microsoft/PTVS#4835.
Even after a clean Languageserver install, this keeps happening. My colleague has the exact same laptop and extensions on vscode except for IntelliCode installed and doesn't have the issue so far, if that may be of any help!
languageServer
languageServer.0.4.71
Python Extension
Name: Python
Id: ms-python.python
Description: Linting, Debugging (multi-threaded, remote), Intellisense, code formatting, refactoring, unit tests, snippets, and more.
Version: 2019.10.44104
Publisher: Microsoft
VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=ms-python.python
Type annotations do not follow code flow order when an annotation symbol is resolved. They support forward declarations.
Here are two options for how to work around this in your code:
Don't redefine str in your code. This is the preferable solution, since redefining built-in symbols is a bad practice in general and can lead to significant confusion and fragility in your code.
If option 1 doesn't work for some reason, define an alias to str in your code using a type alias and use that alias within your type annotations. Type aliases do follow code flow rules, so if you put this at the top of your file, the type alias will be defined in terms of the built-in str. Here's how that might look:
buitin_str = str
x: builtin_str = "hi"
I realize that mypy may allow you to get away with redeclaring a built-in symbol at the end of the file, but Pylint/Pyright don't because of the lazy evaluation design that gives them their fast performance. I'll also point out that mypy will likely need to change their behavior for Python 3.9 because of this new language feature: Postponed Evaluation of Type Annotations.
Activity
jakebailey commentedon Oct 31, 2019
Just to verify, does writing
strelsewhere work as expected? Want to check that this isn't #1697.max-sixty commentedon Oct 31, 2019
Yes, I should have added—everywhere else everything is fine
jakebailey commentedon Nov 5, 2019
I can't seem to reproduce this:
What context is your variable defined in? I'm seeing some indentation, so it'd be helpful to know what scope it's being defined in.
jakebailey commentedon Nov 5, 2019
Ah, sorry, I forgot you linked the code.
I'll have to go test this on the real thing.
max-sixty commentedon Nov 5, 2019
Thanks Jake Bailey (@jakebailey) . I just looked again to see if I could see any anomaly—I think maybe I found it: https://github.com/pydata/xarray/blob/b649846b9ceef0db8631e7148f5ee9415bdd4621/xarray/core/dataarray.py#L3117
So potentially mypy is ignoring that, but python-language-server isn't?
jakebailey commentedon Nov 5, 2019
Ah, that's probably it, in which case this may actually be a class scoping issue along with missing dataflow analysis to know that the assignment comes after.
TiemenSch commentedon Nov 6, 2019
I think this issue is much broader as I'm getting a lot more false positives lately, where just editing a single character with valid python trips up the parser for the entire file. I reported it in the wrong place, though: microsoft/PTVS#4835.
Even after a clean Languageserver install, this keeps happening. My colleague has the exact same laptop and extensions on vscode except for IntelliCode installed and doesn't have the issue so far, if that may be of any help!
languageServer
languageServer.0.4.71
Python Extension
Name: Python
Id: ms-python.python
Description: Linting, Debugging (multi-threaded, remote), Intellisense, code formatting, refactoring, unit tests, snippets, and more.
Version: 2019.10.44104
Publisher: Microsoft
VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=ms-python.python
VS Code
Version: 1.39.2 (user setup)
Commit: 6ab598523be7a800d7f3eb4d92d7ab9a66069390
Date: 2019-10-15T15:35:18.241Z
Electron: 4.2.10
Chrome: 69.0.3497.128
Node.js: 10.11.0
V8: 6.9.427.31-electron.0
OS: Windows_NT x64 10.0.18362
jakebailey commentedon Nov 6, 2019
0.4.71 is fairly old (as we haven't pushed to stable in a bit). Could you try out the beta build? https://github.com/microsoft/python-language-server/wiki/Versioning-and-download-channels
MikhailArkhipov commentedon Dec 16, 2019
Most probably addressed by #1798. Feel free to reopen if it is still an issue.
max-sixty commentedon Jul 1, 2020
FYI I do still get this. I rechecked since trying Pylance, though it occurs even if Pylance is disabled in favor of
Microsoft.To repro, it's on this line: https://github.com/pydata/xarray/blob/v0.15.1/xarray/core/dataarray.py#L242
And I suspect caused by this definition of
str: https://github.com/pydata/xarray/blob/v0.15.1/xarray/core/dataarray.py#L3435I can also provide logs if helpful
Should I open a new issue?
erictraut commentedon Jul 1, 2020
Type annotations do not follow code flow order when an annotation symbol is resolved. They support forward declarations.
Here are two options for how to work around this in your code:
strin your code. This is the preferable solution, since redefining built-in symbols is a bad practice in general and can lead to significant confusion and fragility in your code.strin your code using a type alias and use that alias within your type annotations. Type aliases do follow code flow rules, so if you put this at the top of your file, the type alias will be defined in terms of the built-instr. Here's how that might look:I realize that mypy may allow you to get away with redeclaring a built-in symbol at the end of the file, but Pylint/Pyright don't because of the lazy evaluation design that gives them their fast performance. I'll also point out that mypy will likely need to change their behavior for Python 3.9 because of this new language feature: Postponed Evaluation of Type Annotations.
jakebailey commentedon Jul 1, 2020
microsoft/pylance-release#19 (if you want the pylance discussion).