python / mypy

Optional static typing for Python
https://www.mypy-lang.org/
Other
18.18k stars 2.77k forks source link

[PEP 695] Nested generic class fails to typecheck #17596

Open jolaf opened 1 month ago

jolaf commented 1 month ago

The following code:

class A:
    class B[T]:
        def f(self) -> T: ...

produces the following output:

$ mypy --enable-incomplete-feature=NewGenericSyntax test.pyi 
test.pyi: note: In member "f" of class "B":
test.pyi:3:24: error: A function returning TypeVar should receive at least one argument containing the same TypeVar  [type-var]
            def f(self) -> T: ...
                           ^
Found 1 error in 1 file (checked 1 source file)

This looks like a false positive to me.

$ mypy --version
mypy 1.11.0 (compiled: yes)

$ python3 --version
Python 3.12.3

$ lsb_release -d
No LSB modules are available.
Description:    Ubuntu 24.04 LTS
JelleZijlstra commented 1 month ago

Confirmed this doesn't reproduce with the old generic syntax (https://mypy-play.net/?mypy=latest&python=3.12&gist=302b923e5d91433fadcc4026f604d253), so added the PEP 695 label.