python-trio / trio-typing

Type hints for Trio and related projects
Other
27 stars 16 forks source link

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

Closed Hnasar closed 10 months ago

Hnasar commented 10 months ago

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 https://github.com/python/mypy/pull/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 commented 10 months ago

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 commented 10 months ago

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

jakkdl commented 10 months ago

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