Skip to content

[PEP 695] Unexpected var-annotated error #17630

Closed
@jolaf

Description

@jolaf

The following code:

from __future__ import annotations

class A:
    class B[T]:
        def __get__(self, instance: A, owner: type[A]) -> T:
            return None
    f = B[int]()

a = A()
v = a.f

print("OK")

produces the following output:

test.py: note: In member "__get__" of class "B":
test.py:5:59: error: A function returning TypeVar should receive at least one argument containing the same TypeVar  [type-var]
            def __get__(self, instance: A, owner: type[A]) -> T:
                                                              ^
test.py:6:20: error: Incompatible return value type (got "None", expected "T")  [return-value]
                return None
                       ^~~~
test.py: note: At top level:
test.py:10:5: error: Need type annotation for "v"  [var-annotated]
    v = a.f
        ^~~
Found 3 errors in 1 file (checked 1 source file)

Here the first error [type-var] is due to #17596.

The second error [return-value] is legitimate and appears here because of synthetic nature of this test, extracted from much more complicated code.

The third error [var-annotated] is the essence of this issue.
It looks like a false positive to me.

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

$ python --version
Python 3.12.3

$ lsb_release -d
No LSB modules are available.
Description:	Ubuntu 24.04 LTS

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-pep-695Issues related to PEP 695 syntax

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions