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

Generic type variables broken i latest release #636

Closed
dbrattli opened this issue Nov 21, 2020 · 2 comments
Closed

Generic type variables broken i latest release #636

dbrattli opened this issue Nov 21, 2020 · 2 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

@dbrattli
Copy link

The latest release of Pylance broke the typing in https://github.com/dbrattli/Expression/blob/master/oryx/handler.py . It looks like Pylance (or Python or me) cannot handle generic type aliases properly:

E.g:

TSource = TypeVar("TSource")
TError = TypeVar("TError")
TResult = TypeVar("TResult")
TNext = TypeVar("TNext")


# HttpFuncResult[TResult, TError]
HttpFuncResult = Result[Context[TResult], TError]
# HttpFuncResultAsync[TResult, TError]
HttpFuncResultAsync = Awaitable[Result[Context[TResult], TError]]

# HttpFunc[TNext, TResult, TError]
HttpFunc = Callable[
    [Context[TNext]],
    HttpFuncResultAsync[TResult, TError],
]

# HttpHandler[TNext, TResult, TError, TSource]
HttpHandler = Callable[
    [
        HttpFunc[TNext, TResult, TError],
        Context[TSource],
    ],
    HttpFuncResultAsync[TResult, TError],
]

E.g code like this now failes to type check using the HttpHandler type alias:

async def run_async(
    ctx: Context[TSource],
    handler: HttpHandler[TNext, TResult, TError, TSource],
) -> Result[TResult, TError]:
    result = await handler(finish_early, ctx)

    def mapper(x: Context[TResult]) -> TResult:
        return x.Response

    return result.map(mapper)

Environment data

  • Pylance language server 2020.11.2 (pyright 750a296b)
  • OS and version: Mac OS 11.01
  • Python version (& distribution if applicable, e.g. Anaconda): Python 3.9 (brew)

Expected behaviour

Generic type aliases should work.

Actual behaviour

Screenshot 2020-11-21 at 11 17 14

However, substituting the type alias HttpHandler[TNext, TResult, TError, TSource] with:

Callable[
    [
        HttpFunc[TNext, TResult, TError],
        Context[TSource],
    ],
    HttpFuncResultAsync[TResult, TError],
]

... fixes the problem. Why does not the type alias HttpHandler work with Pylance? It used to type check with Pylance, but after upgrading it suddenly stopped working.

Screenshot 2020-11-21 at 11 20 36

Logs

[Info  - 10:52:27 AM] Pylance language server 2020.11.2 (pyright 750a296b) starting
[Info  - 10:52:27 AM] Server root directory: /Users/dbrattli/.vscode/extensions/ms-python.vscode-pylance-2020.11.2/dist
[Error - 10:52:27 AM] stubPath /Users/dbrattli/Developer/Github/Expression/typings is not a valid directory.
[Info  - 11:28:37 AM] No configuration file found.
[Info  - 11:28:37 AM] Setting pythonPath for service "Expression": "/usr/local/opt/python@3.9/bin/python3"
Search paths found for configured python interpreter:
  /usr/local/Cellar/python@3.9/3.9.0_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9
  /usr/local/Cellar/python@3.9/3.9.0_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/lib-dynload
  /Users/dbrattli/Library/Python/3.9/lib/python/site-packages
  /usr/local/lib/python3.9/site-packages
  /Users/dbrattli/Developer/Github/aioreactive
  /Users/dbrattli/Developer/Github/Expression
  /Users/dbrattli/Developer/Github/RxPY
  /usr/local/Cellar/protobuf/3.13.0_1/libexec/lib/python3.9/site-packages
[Error - 11:28:37 AM] stubPath /Users/dbrattli/Developer/Github/Expression/typings is not a valid directory.
[Info  - 11:28:37 AM] Assuming Python version 3.9
[Info  - 11:28:37 AM] Assuming Python platform Darwin
[Info  - 11:28:37 AM] Searching for source files
[Info  - 11:28:37 AM] Found 69 source files
Background analysis message: setConfigOptions
Background analysis message: setTrackedFiles
Background analysis message: markAllFilesDirty
Background analysis message: analyze
[BG(1)] analyzing: /Users/dbrattli/Developer/Github/Expression/expression/collections/frozenlist.py ...
[BG(1)]   parsing: /Users/dbrattli/Developer/Github/Expression/expression/collections/frozenlist.py (36ms)
[BG(1)]   parsing: /Users/dbrattli/.vscode/extensions/ms-python.vscode-pylance-2020.11.2/dist/typeshed-fallback/stdlib/3/builtins.pyi [fs read 2ms] (58ms)
[BG(1)]   binding: /Users/dbrattli/.vscode/extensions/ms-python.vscode-pylance-2020.11.2/dist/typeshed-fallback/stdlib/3/builtins.pyi (12ms)
[BG(1)]   binding: /Users/dbrattli/Developer/Github/Expression/expression/collections/frozenlist.py (7ms)
[BG(1)]   checking: /Users/dbrattli/Developer/Github/Expression/expression/collections/frozenlist.py ...
[BG(1)]     parsing: /Users/dbrattli/.vscode/extensions/ms-python.vscode-pylance-2020.11.2/dist/typeshed-fallback/stdlib/2and3/_typeshed/__init__.pyi [fs read 0ms] (17ms)
[BG(1)]     binding: /Users/dbrattli/.vscode/extensions/ms-python.vscode-pylance-2020.11.2/dist/typeshed-fallback/stdlib/2and3/_typeshed/__init__.pyi (2ms)
[BG(1)]     parsing: /Users/dbrattli/.vscode/extensions/ms-python.vscode-pylance-2020.11.2/dist/typeshed-fallback/third_party/2and3/typing_extensions.pyi [fs read 1ms] (4ms)
[BG(1)]     binding: /Users/dbrattli/.vscode/extensions/ms-python.vscode-pylance-2020.11.2/dist/typeshed-fallback/third_party/2and3/typing_extensions.pyi (1ms)
[BG(1)]     parsing: /Users/dbrattli/.vscode/extensions/ms-python.vscode-pylance-2020.11.2/dist/typeshed-fallback/stdlib/3/typing.pyi [fs read 1ms] (21ms)
[BG(1)]     binding: /Users/dbrattli/.vscode/extensions/ms-python.vscode-pylance-2020.11.2/dist/typeshed-fallback/stdlib/3/typing.pyi (7ms)
[BG(1)]     parsing: /Users/dbrattli/.vscode/extensions/ms-python.vscode-pylance-2020.11.2/dist/typeshed-fallback/stdlib/3/functools.pyi [fs read 1ms] (6ms)
[BG(1)]     binding: /Users/dbrattli/.vscode/extensions/ms-python.vscode-pylance-2020.11.2/dist/typeshed-fallback/stdlib/3/functools.pyi (2ms)
[BG(1)]     parsing: /Users/dbrattli/Developer/Github/Expression/expression/core/__init__.py [fs read 1ms] (2ms)
[BG(1)]     binding: /Users/dbrattli/Developer/Github/Expression/expression/core/__init__.py (0ms)
[BG(1)]     parsing: /Users/dbrattli/Developer/Github/Expression/expression/core/match.py [fs read 1ms] (3ms)
[BG(1)]     binding: /Users/dbrattli/Developer/Github/Expression/expression/core/match.py (0ms)
[BG(1)]     parsing: /Users/dbrattli/Developer/Github/Expression/expression/core/option.py [fs read 1ms] (6ms)
[BG(1)]     binding: /Users/dbrattli/Developer/Github/Expression/expression/core/option.py (1ms)
[BG(1)]     parsing: /Users/dbrattli/.vscode/extensions/ms-python.vscode-pylance-2020.11.2/dist/typeshed-fallback/stdlib/3/abc.pyi [fs read 1ms] (1ms)
[BG(1)]     binding: /Users/dbrattli/.vscode/extensions/ms-python.vscode-pylance-2020.11.2/dist/typeshed-fallback/stdlib/3/abc.pyi (0ms)
[BG(1)]     parsing: /Users/dbrattli/Developer/Github/Expression/expression/core/error.py [fs read 1ms] (1ms)
[BG(1)]     binding: /Users/dbrattli/Developer/Github/Expression/expression/core/error.py (0ms)
[BG(1)]     parsing: /Users/dbrattli/Developer/Github/Expression/expression/core/pipe.py [fs read 1ms] (2ms)
[BG(1)]     binding: /Users/dbrattli/Developer/Github/Expression/expression/core/pipe.py (1ms)
[BG(1)]     parsing: /Users/dbrattli/Developer/Github/Expression/expression/collections/seq.py [fs read 1ms] (8ms)
[BG(1)]     binding: /Users/dbrattli/Developer/Github/Expression/expression/collections/seq.py (2ms)
[BG(1)]   checking: /Users/dbrattli/Developer/Github/Expression/expression/collections/frozenlist.py (178ms)
[BG(1)] analyzing: /Users/dbrattli/Developer/Github/Expression/expression/collections/frozenlist.py (291ms)
Background analysis message: resumeAnalysis
[BG(1)] analyzing: /Users/dbrattli/Developer/Github/Expression/oryx/handler.py ...
[BG(1)]   parsing: /Users/dbrattli/Developer/Github/Expression/oryx/handler.py (7ms)
[BG(1)]   binding: /Users/dbrattli/Developer/Github/Expression/oryx/handler.py (1ms)
[BG(1)]   checking: /Users/dbrattli/Developer/Github/Expression/oryx/handler.py ...
[BG(1)]     parsing: /usr/local/lib/python3.9/site-packages/aiohttp/__init__.py [fs read 1ms] (3ms)
[BG(1)]     binding: /usr/local/lib/python3.9/site-packages/aiohttp/__init__.py (1ms)
[BG(1)]     parsing: /usr/local/lib/python3.9/site-packages/aiohttp/client.py [fs read 0ms] (44ms)
[BG(1)]     binding: /usr/local/lib/python3.9/site-packages/aiohttp/client.py (4ms)
[BG(1)]     parsing: /usr/local/lib/python3.9/site-packages/aiohttp/client_reqrep.py [fs read 0ms] (24ms)
[BG(1)]     binding: /usr/local/lib/python3.9/site-packages/aiohttp/client_reqrep.py (5ms)
[BG(1)]     parsing: /usr/local/lib/python3.9/site-packages/aiohttp/helpers.py [fs read 1ms] (41ms)
[BG(1)]     binding: /usr/local/lib/python3.9/site-packages/aiohttp/helpers.py (5ms)
[BG(1)]     parsing: /usr/local/lib/python3.9/site-packages/attr/__init__.pyi [fs read 1ms] (4ms)
[BG(1)]     binding: /usr/local/lib/python3.9/site-packages/attr/__init__.pyi (0ms)
[BG(1)]     parsing: /Users/dbrattli/.vscode/extensions/ms-python.vscode-pylance-2020.11.2/dist/typeshed-fallback/stdlib/3/re.pyi [fs read 1ms] (4ms)
[BG(1)]     binding: /Users/dbrattli/.vscode/extensions/ms-python.vscode-pylance-2020.11.2/dist/typeshed-fallback/stdlib/3/re.pyi (0ms)
[BG(1)]     parsing: /Users/dbrattli/.vscode/extensions/ms-python.vscode-pylance-2020.11.2/dist/typeshed-fallback/stdlib/3/enum.pyi [fs read 0ms] (1ms)
[BG(1)]     binding: /Users/dbrattli/.vscode/extensions/ms-python.vscode-pylance-2020.11.2/dist/typeshed-fallback/stdlib/3/enum.pyi (0ms)
[BG(1)]     parsing: /Users/dbrattli/Developer/Github/Expression/expression/core/result.py [fs read 0ms] (3ms)
[BG(1)]     binding: /Users/dbrattli/Developer/Github/Expression/expression/core/result.py (1ms)
[BG(1)]     parsing: /Users/dbrattli/Developer/Github/Expression/oryx/context.py [fs read 1ms] (2ms)
[BG(1)]     binding: /Users/dbrattli/Developer/Github/Expression/oryx/context.py (0ms)
[BG(1)]     parsing: /Users/dbrattli/Developer/Github/Expression/expression/collections/__init__.py [fs read 0ms] (1ms)
[BG(1)]     binding: /Users/dbrattli/Developer/Github/Expression/expression/collections/__init__.py (0ms)
[BG(1)]     parsing: /usr/local/lib/python3.9/site-packages/aiohttp/typedefs.py [fs read 1ms] (1ms)
[BG(1)]     binding: /usr/local/lib/python3.9/site-packages/aiohttp/typedefs.py (1ms)
[BG(1)]     parsing: /Users/dbrattli/.vscode/extensions/ms-python.vscode-pylance-2020.11.2/dist/typeshed-fallback/stdlib/3/json/__init__.pyi [fs read 0ms] (1ms)
[BG(1)]     binding: /Users/dbrattli/.vscode/extensions/ms-python.vscode-pylance-2020.11.2/dist/typeshed-fallback/stdlib/3/json/__init__.pyi (0ms)
[BG(1)]     parsing: /Users/dbrattli/.vscode/extensions/ms-python.vscode-pylance-2020.11.2/dist/typeshed-fallback/stdlib/3/json/decoder.pyi [fs read 1ms] (1ms)
[BG(1)]     binding: /Users/dbrattli/.vscode/extensions/ms-python.vscode-pylance-2020.11.2/dist/typeshed-fallback/stdlib/3/json/decoder.pyi (0ms)
[BG(1)]   checking: /Users/dbrattli/Developer/Github/Expression/oryx/handler.py (164ms)
[BG(1)] analyzing: /Users/dbrattli/Developer/Github/Expression/oryx/handler.py (172ms)
Background analysis message: resumeAnalysis
[BG(1)] analyzing: /Users/dbrattli/Developer/Github/Expression/oryx/perf/classic.py ...
[BG(1)]   parsing: /Users/dbrattli/Developer/Github/Expression/oryx/perf/classic.py (1ms)
[BG(1)]   binding: /Users/dbrattli/Developer/Github/Expression/oryx/perf/classic.py (1ms)
[BG(1)]   checking: /Users/dbrattli/Developer/Github/Expression/oryx/perf/classic.py (11ms)
[BG(1)] analyzing: /Users/dbrattli/Developer/Github/Expression/oryx/perf/classic.py (14ms)
Background analysis message: resumeAnalysis
Background analysis message: getDiagnosticsForRange
Background analysis message: getDiagnosticsForRange
Background analysis message: getDiagnosticsForRange
Background analysis message: getDiagnosticsForRange
[FG] parsing: /Users/dbrattli/Developer/Github/Expression/oryx/handler.py (20ms)
[FG] parsing: /Users/dbrattli/.vscode/extensions/ms-python.vscode-pylance-2020.11.2/dist/typeshed-fallback/stdlib/3/builtins.pyi [fs read 0ms] (87ms)
[FG] binding: /Users/dbrattli/.vscode/extensions/ms-python.vscode-pylance-2020.11.2/dist/typeshed-fallback/stdlib/3/builtins.pyi (14ms)
[FG] binding: /Users/dbrattli/Developer/Github/Expression/oryx/handler.py (1ms)
Background analysis message: getDiagnosticsForRange
Background analysis message: getDiagnosticsForRange
Background analysis message: setFileClosed
Background analysis message: analyze
Background analysis message: setFileOpened
Background analysis message: getDiagnosticsForRange
Background analysis message: getDiagnosticsForRange
Background analysis message: getSemanticTokens
[BG(1)] parsing: /Users/dbrattli/Developer/Github/Expression/oryx/handler.py (12ms)
[BG(1)] binding: /Users/dbrattli/Developer/Github/Expression/oryx/handler.py (2ms)
Background analysis message: getSemanticTokens
Background analysis message: getDiagnosticsForRange
Background analysis message: getDiagnosticsForRange
[FG] parsing: /Users/dbrattli/Developer/Github/Expression/oryx/handler.py (4ms)
[FG] binding: /Users/dbrattli/Developer/Github/Expression/oryx/handler.py (1ms)
Background analysis message: analyze
[BG(1)] analyzing: /Users/dbrattli/Developer/Github/Expression/oryx/handler.py ...
[BG(1)]   checking: /Users/dbrattli/Developer/Github/Expression/oryx/handler.py (5ms)
[BG(1)] analyzing: /Users/dbrattli/Developer/Github/Expression/oryx/handler.py (6ms)
Background analysis message: resumeAnalysis
Background analysis message: getDiagnosticsForRange
Background analysis message: getDiagnosticsForRange
Background analysis message: getDiagnosticsForRange
Background analysis message: getDiagnosticsForRange```
@dbrattli dbrattli changed the title Generic type aliases broken i latest release Generic type variables broken i latest release Nov 21, 2020
@erictraut
Copy link
Contributor

Thanks for the bug report. The bug is specific to a generic type alias that is defined in terms of other generic type aliases where a type variable appears more than once. We didn't have any such examples in our regression test suite, so this error went unnoticed. I've fixed the problem and added a variant of your sample to our test suite.

Incidentally, I think that this was partly broken prior to the latest release. It wasn't performing the correct TypeVar substitution, but no error was reported so it went unnoticed. The changes in the latest version exposed the underlying bug.

The fix will be included in the next release of Pylance.

@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 Nov 21, 2020
@jakebailey
Copy link
Member

This issue has been fixed in version 2020.12.0, which we've just released. You can find the changelog here: https://github.com/microsoft/pylance-release/blob/master/CHANGELOG.md#2020120-2-december-2020

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