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

Type subscript runtime exception false positive #1145

Closed
sbdchd opened this issue Apr 10, 2021 · 3 comments
Closed

Type subscript runtime exception false positive #1145

sbdchd opened this issue Apr 10, 2021 · 3 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

@sbdchd
Copy link

sbdchd commented Apr 10, 2021

Environment data

  • Language Server version: 2021.4.0
  • OS and version: darwin x64
  • Python version (and distribution if applicable, e.g. Anaconda):
  • python.analysis.indexing: undefined
  • python.analysis.typeCheckingMode: off

Expected behaviour

Shouldn't report that indexing type will generate a runtime exception since the type isn't evaluated at runtime

Actual behaviour

error:

Subscript for class "OrderedDict" will generate runtime exception; enclose type annotation in quotes

Logs

Python Language Server Log

[Info  - 6:34:52 PM] Pylance language server 2021.4.0 (pyright 2918da1d) starting
[Info  - 6:34:52 PM] Server root directory: ~/.vscode/extensions/ms-python.vscode-pylance-2021.4.0/dist

Code Snippet / Additional information

Screen Shot 2021-04-09 at 10 50 37 PM

if we run the following snippet it doesn't generate a runtime exception

from collections import OrderedDict


def main() -> None:
    x: OrderedDict[str, str] = OrderedDict()
    print(x)


main()
@erictraut
Copy link
Contributor

It's very strange that it does not generate a runtime exception in the context of a variable type annotation, but it does when used in the context of a return type or parameter type annotation.

If I modify your code as follows and then run it in a version of Python < 3.9 (I tried 3.6), it generates a runtime exception on the return type annotation.

def main() -> OrderedDict[str, str]:
    x: OrderedDict[str, str] = OrderedDict()
    print(x)
    return x
    def main() -> OrderedDict[str, str]:
TypeError: 'type' object is not subscriptable

@erictraut
Copy link
Contributor

I've updated the logic to differentiate between cases where this will cause runtime exceptions (parameter annotations, return type annotations, type aliases, and other non-annotation expressions) and cases where it will not (PEP 526-style variable type annotations). This change will be in the next release of Pylance.

Thanks for reporting this issue.

@erictraut erictraut added bug Something isn't working fixed in next version (main) A fix has been implemented and will appear in an upcoming version and removed triage labels Apr 10, 2021
@jakebailey
Copy link
Member

This issue has been fixed in version 2021.4.1, which we've just released. You can find the changelog here: https://github.com/microsoft/pylance-release/blob/main/CHANGELOG.md#202141-14-april-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