Skip to content
This repository was archived by the owner on Apr 14, 2022. It is now read-only.
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

Description

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!

Activity

  1. jakebailey commented on Oct 31, 2019

    @jakebailey
    Member

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

  2. max-sixty commented on Oct 31, 2019

    @max-sixty
    Author

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

  3. jakebailey commented on Nov 5, 2019

    @jakebailey
    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.

  4. jakebailey commented on Nov 5, 2019

    @jakebailey
    Member

    Ah, sorry, I forgot you linked the code.

    image

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

  5. max-sixty commented on Nov 5, 2019

    @max-sixty
    Author

    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?

  6. jakebailey commented on Nov 5, 2019

    @jakebailey
    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.

  7. TiemenSch commented on Nov 6, 2019

    @TiemenSch

    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

  8. jakebailey commented on Nov 6, 2019

    @jakebailey
    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

  9. MikhailArkhipov commented on Dec 16, 2019

    @MikhailArkhipov

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

  10. max-sixty commented on Jul 1, 2020

    @max-sixty
    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?

  11. erictraut commented on Jul 1, 2020

    @erictraut

    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.

  12. jakebailey commented on Jul 1, 2020

    @jakebailey
    Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions