Skip to content
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

Closed
max-sixty opened this issue Oct 31, 2019 · 12 comments
Closed

Undefined variable: 'str' as part of correct type annotation #1732

max-sixty opened this issue Oct 31, 2019 · 12 comments

Comments

@max-sixty
Copy link

max-sixty commented Oct 31, 2019

Environment data

  • Language Server version: Microsoft Python Language Server version 0.4.81.0
  • OS and version: macOS 10.15 (19A602)
  • Python version (& distribution if applicable, e.g. Anaconda): '3.7.4 (default, Sep 7 2019, 18:27:02) \n[Clang 10.0.1 (clang-1001.0.46.4)]'

Expected behaviour

No error message

Actual behaviour

Undefined variable: 'str' as part of correct type annotation

image

(other errors are unrelated)

Potentially related: #1453

Logs

https://gist.github.com/max-sixty/630b88f44038174dc46229edc51734b9

Reproducible from dataarray.py on xarray master, if anyone wants a full repro

Thank you!

@jakebailey
Copy link
Member

Just to verify, does writing str elsewhere work as expected? Want to check that this isn't #1697.

@max-sixty
Copy link
Author

Yes, I should have added—everywhere else everything is fine

@jakebailey
Copy link
Member

I can't seem to reproduce this:

image

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
Copy link
Member

Ah, sorry, I forgot you linked the code.

image

I'll have to go test this on the real thing.

@max-sixty
Copy link
Author

Thanks @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
Copy link
Member

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
Copy link

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
Copy link
Member

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
Copy link

MikhailArkhipov commented Dec 16, 2019

Most probably addressed by #1798. Feel free to reopen if it is still an issue.

@max-sixty
Copy link
Author

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#L3435

I can also provide logs if helpful

Should I open a new issue?

@erictraut
Copy link

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:

  1. 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.
  2. 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.

@jakebailey
Copy link
Member

jakebailey commented Jul 1, 2020

microsoft/pylance-release#19 (if you want the pylance discussion).

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants