Skip to content

TYP: types in a class body that use names shadowed by a class method confuse ty #30752

Closed

Description

@hamdanal

Describe the issue:

Better explain with an example

# file test.py
class dtype: ...
class ndarray:
    def method(self) -> dtype: ...  # defined before the dtype property
    @property
    def dtype(self): ...
    def method2(self) -> dtype: ...  # defined after the dtype property

print(ndarray.method.__annotations__)
print(ndarray.method2.__annotations__)
$ uvx --isolated --python=3.13 python test.py
{'return': <class '__main__.dtype'>}
{'return': <property object at 0x7e4a7e223510>}
$ uvx --isolated --python=3.14 python test.py
{'return': <property object at 0x787a8e5a4a90>}
{'return': <property object at 0x787a8e5a4a90>}

So starting with Python 3.14, the dtype annotation of methods defined anywhere in the class body will point to ndarray.dtype property instead of the dtype class. While mypy models the <3.14 behavior, ty chose to model the >=3.14 behavior.

I'll prepare a PR to fix these annotations so that work with all type checkers and across Python versions.

Reproduce the code example:

import numpy as np

a = np.zeros(3).tolist()
reveal_type(a)

Error message:

ty: Revealed type: `list[Unknown]`

Python and NumPy Versions:

2.4.1
3.13.11 (main, Dec 6 2025, 08:52:51) [GCC 11.4.0]

Type-checker version and settings:

ty: 0.0.14

Additional typing packages.

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions