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

trio_typing.plugin incompatible with mypy 1.7's new type inference #90

Closed
Hnasar opened this issue Nov 13, 2023 · 3 comments · Fixed by #92
Closed

trio_typing.plugin incompatible with mypy 1.7's new type inference #90

Hnasar opened this issue Nov 13, 2023 · 3 comments · Fixed by #92

Comments

@Hnasar
Copy link
Contributor

Hnasar commented Nov 13, 2023

When the trio_typing.plugin is enabled with mypy 1.7, it can cause false positive errors (even when trio is not even in use) related to Mypy's new type inference which was enabled by default in
python/mypy#16345 for mypy 1.7.

When linting async functions that take generic callables, the new type inference appears to automatically infer the variance of typevars. For example:
https://mypy-play.net/?mypy=latest&python=3.11&gist=73fc0a0b94a3f2410080c0482df7ecab
Outputs:

main.py:24: note: Revealed type is "def () -> __main__.C"
main.py:25: note: Revealed type is "def [_T_co] (make_client: def () -> _T_co`173) -> contextlib._AsyncGeneratorContextManager[_T_co`173]"
main.py:26: note: Revealed type is "contextlib._AsyncGeneratorContextManager[__main__.C]"

But if you add # mypy: old-type-inference to the top: https://mypy-play.net/?mypy=latest&python=3.11&gist=be6764bbead8f238976dd1be12d8d7e8 then it outputs:

main.py:25: note: Revealed type is "def () -> __main__.C"
main.py:26: note: Revealed type is "def [T] (make_client: def () -> T`-1) -> contextlib._AsyncGeneratorContextManager[T`-1]"
main.py:27: note: Revealed type is "contextlib._AsyncGeneratorContextManager[__main__.C]"

When the new type inference is used with a config file that enables the plugin

# mypy.ini
[mypy]
plugins = trio_typing.plugin

then an error is reported for this code:

    client: C
    async with _do_open_client(make_client) as client:
example.py:27: error: Incompatible types in assignment (expression has type "_T_co", variable has type "C")  [assignment]
@jakkdl
Copy link
Member

jakkdl commented Nov 14, 2023

Since trio 0.23 we've added inline types, and once mypy releases support for TypeVarTuples (which should be in the next release afaict) I think we can fully deprecate the plugin.
It would maybe be good to do a final release of trio-typing which is compatible with mypy 1.7, but I haven't touched the plugin previously myself so am not especially tempted to try and debug this thing in particular.

Is there any particular functionality you're after the plugin provides that's not provided by using trio 0.23 and current mypy?

@TeamSpen210
Copy link
Contributor

Mypy 1.7 is the release that supports TypeVarTuple actually, so we should start adding those.

@jakkdl
Copy link
Member

jakkdl commented Nov 17, 2023

Ooh, in that case it would work to mark trio-typing as requiring mypy<1.7

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

Successfully merging a pull request may close this issue.

3 participants