Skip to content

TYP: Fix class annotations using names shadowed by a class member - #30754

Merged
jorenham merged 3 commits into
numpy:mainfrom
hamdanal:ty
Jan 31, 2026
Merged

TYP: Fix class annotations using names shadowed by a class member#30754
jorenham merged 3 commits into
numpy:mainfrom
hamdanal:ty

Conversation

@hamdanal

Copy link
Copy Markdown
Contributor

Fixes #30752

Comment thread numpy/lib/_nanfunctions_impl.py

@jorenham jorenham left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks.

But could you add a quick inline comment next to the py_* imports so that we don't remove it again in the future?

@jorenham

Copy link
Copy Markdown
Member

CI is failing because the pyright type-completeness dropped for some reason. So there is something in this PR that causes pyright to not be able to completely determine the type of a commonly used thing, such as dtype or ndarray.

@hamdanal

Copy link
Copy Markdown
Contributor Author

CI is failing because the pyright type-completeness dropped for some reason. So there is something in this PR that causes pyright to not be able to completely determine the type of a commonly used thing, such as dtype or ndarray.

I am not familiar with pyright's type completeness. Do you know if it can tell what symbols are the root cause of the problem?

@jorenham

Copy link
Copy Markdown
Member

CI is failing because the pyright type-completeness dropped for some reason. So there is something in this PR that causes pyright to not be able to completely determine the type of a commonly used thing, such as dtype or ndarray.

I am not familiar with pyright's type completeness. Do you know if it can tell what symbols are the root cause of the problem?

I took a look, and it turns out that it's caused by the lack of generic type arguments passed to _dtype in __init__.pyi. Those should instead be _dtype[Any] or _dtype[generic]

@hamdanal

Copy link
Copy Markdown
Contributor Author

Made _dtype an actual alias of dtype so that they have the same type parameters with proper defaults.

@jorenham
jorenham merged commit 4227ede into numpy:main Jan 31, 2026
79 checks passed
@jorenham jorenham added this to the 2.5.0 Release milestone Jan 31, 2026
@jorenham

Copy link
Copy Markdown
Member

Thanks @hamdanal

@hamdanal
hamdanal deleted the ty branch February 1, 2026 07:23
jorenham pushed a commit that referenced this pull request Jul 10, 2026
NumPy gh-30754 fixed annotations shadowed by class members under
Python 3.14 annotation-scope rules. Later ndarray and MaskedArray
signatures reintroduced bare dtype references, which ty resolves to
the dtype property and consequently infers Unknown for operations
such as argmax(keepdims=True).

Use _dtype or np.dtype for the 53 affected annotations and bounds, and
move the _dtype alias below the dtype definition. This restores precise
ty inference without changing runtime behavior.

A follow-up could add ty alongside Pyrefly in the type-checking
workflow and check the 166 production .pyi files. With the small
existing baseline of final-class, override, parameter-default, and
type-argument diagnostics suppressed, invalid-type-form can remain
enabled and would catch regressions like this.

Follow-up to gh-30754.
charris pushed a commit that referenced this pull request Jul 10, 2026
NumPy gh-30754 fixed annotations shadowed by class members under
Python 3.14 annotation-scope rules. Later ndarray and MaskedArray
signatures reintroduced bare dtype references, which ty resolves to
the dtype property and consequently infers Unknown for operations
such as argmax(keepdims=True).

Use _dtype or np.dtype for the 53 affected annotations and bounds, and
move the _dtype alias below the dtype definition. This restores precise
ty inference without changing runtime behavior.

A follow-up could add ty alongside Pyrefly in the type-checking
workflow and check the 166 production .pyi files. With the small
existing baseline of final-class, override, parameter-default, and
type-argument diagnostics suppressed, invalid-type-form can remain
enabled and would catch regressions like this.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants