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 alias in try block stopped working #3155

Closed
judfs opened this issue Aug 8, 2022 · 2 comments
Closed

Type alias in try block stopped working #3155

judfs opened this issue Aug 8, 2022 · 2 comments

Comments

@judfs
Copy link

judfs commented Aug 8, 2022

Environment data

  • Language Server version: 2022.8.10
  • OS and version: linux x64. uname -v: 138~18.04.1-Ubuntu
  • Python version (and distribution if applicable, e.g. Anaconda): python3 --version: Python 3.6.9. (/usr/bin/python3)
  • python.analysis.indexing: null
  • python.analysis.typeCheckingMode: basic

Code Snippet

import typing
from typing import Dict, Any

try:
    # Python 3.8
    class _Config(typing.TypedDict):
        a: int
        b: str
    Config = _Config
except AttributeError:
    Config = Dict[str, Any]

def get_config() -> Config:
    return {"a":1, "b":"hello"}

print(get_config())

Repro Steps

  1. Have above snippet in a saved file.

Expected behavior

No errors or warnings in the file.

In the past I was seeing the TypedDict editor hints even though my project interpreter was 36. I was surprised that it worked but I liked it. Now it does not type check at all.

Don't know when it last worked, or when it stopped working other than this year.

Actual behavior

(variable) Config: Unknown
Illegal type annotation: variable not allowed unless it is a type aliasPylance[reportGeneralTypeIssues](https://github.com/microsoft/pylance-release/blob/main/DIAGNOSTIC_SEVERITY_RULES.md#diagnostic-severity-rules)

Logs

[Info  - 1:49:00 PM] (27710) Pylance language server 2022.8.10 (pyright 6fa755c0) starting
[Info  - 1:49:00 PM] (27710) Server root directory: /home/josh/.vscode/extensions/ms-python.vscode-pylance-2022.8.10/dist
[Info  - 1:49:00 PM] (27710) Starting service instance "pyissue"
[Info  - 1:49:00 PM] (27710) Notebook support: LSP
[Info  - 1:49:00 PM] (27710) Interactive window support: LSP
[Info  - 1:49:00 PM] (27710) No configuration file found.
[Info  - 1:49:00 PM] (27710) No pyproject.toml file found.
[Warn  - 1:49:00 PM] (27710) stubPath /home/josh/code/pyissue/typings is not a valid directory.
[Info  - 1:49:00 PM] (27710) Assuming Python platform Linux
[Info  - 1:49:00 PM] (27710) Searching for source files
[Info  - 1:49:00 PM] (27710) Found 1 source file
[Info  - 1:49:00 PM] (27710) Background analysis(1) root directory: /home/josh/.vscode/extensions/ms-python.vscode-pylance-2022.8.10/dist
[Info  - 1:49:00 PM] (27710) Background analysis(1) started
[Info  - 1:50:12 PM] (27710) No configuration file found.
[Info  - 1:50:12 PM] (27710) No pyproject.toml file found.
[Warn  - 1:50:12 PM] (27710) stubPath /home/josh/code/pyissue/typings is not a valid directory.
[Info  - 1:50:12 PM] (27710) Assuming Python platform Linux
[Info  - 1:50:12 PM] (27710) Searching for source files
[Info  - 1:50:12 PM] (27710) Found 1 source file
[Info  - 1:50:30 PM] (27710) Searching for source files
[Info  - 1:50:30 PM] (27710) Found 1 source file
[Info  - 1:50:49 PM] (27710) No configuration file found.
[Info  - 1:50:49 PM] (27710) No pyproject.toml file found.
[Warn  - 1:50:49 PM] (27710) stubPath /home/josh/code/pyissue/typings is not a valid directory.
[Info  - 1:50:49 PM] (27710) Assuming Python platform Linux
[Info  - 1:50:49 PM] (27710) Search paths for /home/josh/code/pyissue
[Info  - 1:50:49 PM] (27710)   /home/josh/.vscode/extensions/ms-python.vscode-pylance-2022.8.10/dist/typeshed-fallback/stdlib
[Info  - 1:50:49 PM] (27710)   /home/josh/code/pyissue
[Info  - 1:50:49 PM] (27710)   /home/josh/code/REDACT
[Info  - 1:50:49 PM] (27710)   /home/josh/code/REDACT
[Info  - 1:50:49 PM] (27710)   /home/josh/code/pyissue/typings
[Info  - 1:50:49 PM] (27710)   /home/josh/.vscode/extensions/ms-python.vscode-pylance-2022.8.10/dist/typeshed-fallback/stubs/...
[Info  - 1:50:49 PM] (27710)   /home/josh/.vscode/extensions/ms-python.vscode-pylance-2022.8.10/dist/bundled/stubs
[Info  - 1:50:49 PM] (27710)   /home/josh/code/REDACT
[Info  - 1:50:49 PM] (27710)   /home/josh/.vscode/extensions/ms-python.vscode-pylance-2022.8.10/dist
[Info  - 1:50:49 PM] (27710)   /usr/lib/python3.6
[Info  - 1:50:49 PM] (27710)   /usr/lib/python3.6/lib-dynload
[Info  - 1:50:49 PM] (27710)   /home/josh/.local/lib/python3.6/site-packages
[Info  - 1:50:49 PM] (27710)   /usr/local/lib/python3.6/dist-packages
[Info  - 1:50:49 PM] (27710)   /usr/lib/python3/dist-packages
[Info  - 1:50:49 PM] (27710) Adding fs watcher for library directories:
 /home/josh/code/REDACT
/home/josh/.vscode/extensions/ms-python.vscode-pylance-2022.8.10/dist
/usr/lib/python3.6
/usr/lib/python3.6/lib-dynload
/home/josh/.local/lib/python3.6/site-packages
/usr/local/lib/python3.6/dist-packages
/usr/lib/python3/dist-packages
[Info  - 1:50:49 PM] (27710) Adding fs watcher for directories:
 /home/josh/code/pyissue
[Info  - 1:50:49 PM] (27710) Searching for source files
[Info  - 1:50:49 PM] (27710) Found 1 source file
(27710) [IDX(FG)] index libraries /home/josh/code/pyissue (index) ...
(27710) [IDX(FG)]   read stdlib indices (21ms)
(27710) [IDX(FG)] index libraries /home/josh/code/pyissue (index) [succeed] (22ms)
(27710) Background analysis message: setConfigOptions
(27710) Background analysis message: setImportResolver
(27710) Background analysis message: ensurePartialStubPackages
(27710) Background analysis message: setTrackedFiles
(27710) Background analysis message: markAllFilesDirty
(27710) Background analysis message: analyze
[Info  - 1:50:49 PM] (27710) SourceFile: Received fs event 'change' for path '/home/josh/code/pyissue/.vscode/settings.json'
(27710) [BG(1)] analyzing: /home/josh/code/pyissue/tmp.py ...
(27710) [BG(1)]   parsing: /home/josh/code/pyissue/tmp.py (36ms)
(27710) [BG(1)]   parsing: /home/josh/.vscode/extensions/ms-python.vscode-pylance-2022.8.10/dist/typeshed-fallback/stdlib/builtins.pyi [fs read 1ms] (35ms)
(27710) [BG(1)]   binding: /home/josh/.vscode/extensions/ms-python.vscode-pylance-2022.8.10/dist/typeshed-fallback/stdlib/builtins.pyi (9ms)
(27710) [BG(1)]   binding: /home/josh/code/pyissue/tmp.py (1ms)
(27710) [BG(1)]   checking: /home/josh/code/pyissue/tmp.py ...
(27710) [BG(1)]     parsing: /home/josh/.vscode/extensions/ms-python.vscode-pylance-2022.8.10/dist/typeshed-fallback/stdlib/typing_extensions.pyi [fs read 0ms] (11ms)
(27710) [BG(1)]     binding: /home/josh/.vscode/extensions/ms-python.vscode-pylance-2022.8.10/dist/typeshed-fallback/stdlib/typing_extensions.pyi (3ms)
(27710) [BG(1)]     parsing: /home/josh/.vscode/extensions/ms-python.vscode-pylance-2022.8.10/dist/typeshed-fallback/stdlib/typing.pyi [fs read 1ms] (13ms)
(27710) [BG(1)]     binding: /home/josh/.vscode/extensions/ms-python.vscode-pylance-2022.8.10/dist/typeshed-fallback/stdlib/typing.pyi (4ms)
(27710) [BG(1)]     parsing: /home/josh/.vscode/extensions/ms-python.vscode-pylance-2022.8.10/dist/typeshed-fallback/stdlib/_typeshed/__init__.pyi [fs read 0ms] (8ms)
(27710) [BG(1)]     binding: /home/josh/.vscode/extensions/ms-python.vscode-pylance-2022.8.10/dist/typeshed-fallback/stdlib/_typeshed/__init__.pyi (1ms)
(27710) [BG(1)]     parsing: /home/josh/.vscode/extensions/ms-python.vscode-pylance-2022.8.10/dist/typeshed-fallback/stdlib/types.pyi [fs read 0ms] (6ms)
(27710) [BG(1)]     binding: /home/josh/.vscode/extensions/ms-python.vscode-pylance-2022.8.10/dist/typeshed-fallback/stdlib/types.pyi (3ms)
(27710) [BG(1)]     parsing: /home/josh/.vscode/extensions/ms-python.vscode-pylance-2022.8.10/dist/typeshed-fallback/stdlib/abc.pyi [fs read 1ms] (1ms)
(27710) [BG(1)]     binding: /home/josh/.vscode/extensions/ms-python.vscode-pylance-2022.8.10/dist/typeshed-fallback/stdlib/abc.pyi (0ms)
(27710) [BG(1)]   checking: /home/josh/code/pyissue/tmp.py (59ms)
(27710) [BG(1)] analyzing: /home/josh/code/pyissue/tmp.py (141ms)
(27710) Background analysis message: getSemanticTokens delta
(27710) [BG(1)] getSemanticTokens delta previousResultId:1659981012991 at /home/josh/code/pyissue/tmp.py (1ms)
(27710) Background analysis message: resumeAnalysis

@erictraut
Copy link
Contributor

Variables are not allowed within type annotations. In this code snippet, Config is considered a variable (as opposed to a type alias) because it is assigned more than one value on different code paths. A static type analyzer cannot determine which of these code paths will be taken at runtime, so it cannot determine which type definition of Config should be used for static analysis purposes.

The recommended approach here is to leverage typing_extensions for backward compatibility.

from typing_extensions import TypedDict

class Config(TypedDict):
    a: int
    b: str

If, for some reason, you don't want to take a dependency on typing_extensions, then the recommended approach is to use a sys.version_info conditional rather than a try statement.

if sys.version_info >= (3, 8):
    class _Config(typing.TypedDict):
        a: int
        b: str

    Config = _Config
else:
    Config = Dict[str, Any]

@judfs
Copy link
Author

judfs commented Aug 8, 2022

Ok cool. Again I was surprised it worked at all before, but it did so I went with it. Seems to be broken as intended now then.

A static type analyzer cannot determine which of these code paths will be taken at runtime, so it cannot determine which type definition of Config should be used for static analysis purposes.

It did pick something before though. Unless there was result caching nonsense going on.

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

No branches or pull requests

4 participants