Skip to content

DEP: deprecate numpy.typing.NBitBase #28884

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 3, 2025

Conversation

jorenham
Copy link
Member

@jorenham jorenham commented May 1, 2025

numpy.typing.NBitBase is intended to be used as e.g.

import numpy as np
import numpy.typing as npt

def f[NT: npt.NBitBase](x: np.complexfloating[NT]) -> np.floating[NT]: ...

This will lead to problems for float64 and complex128, which were changed into concrete subtypes of floating and complexfloating in NumPy 2.2. That means that e.g. floating[_64Bit] is no longer assignable to float64, causing type-checkers to reject x: float64 = f(complex128(1)). This defeats its purpose, and should therefore no longer be used.

The general alternative is to use typing.overload:

import numpy as np
from typing import overload

@overload
def f(x: np.complex64) -> np.float32: ...
@overload
def f(x: np.complex128) -> np.float64: ...
@overload
def f(x: np.clongdouble) -> np.longdouble: ...

And even though it has gotten more verbose, it requires less mental to interpret.


In situations where the input scalar type is the same as the output type, there's a simpler alternative:

Old:

import numpy as np
import numpy.typing as npt

def f[NT: npt.NBitBase](x: np.floating[NT]) -> np.floating[NT]: ...

New:

import numpy as np

def f[FloatT: np.floating](x: FloatT) -> FloatT: ...

This also makes the code more readable, IMO.


Note that at the moment, float32 is assignable to floating[_32Bit], because it is defined as a type alias, as opposed to a proper subtype. In NumType this has already been fixed (i.e. it's incorrect) for all scalar types.

@jorenham jorenham added 07 - Deprecation 56 - Needs Release Note. Needs an entry in doc/release/upcoming_changes 41 - Static typing labels May 1, 2025

This comment has been minimized.

@jorenham jorenham removed the 56 - Needs Release Note. Needs an entry in doc/release/upcoming_changes label May 1, 2025
@jorenham jorenham marked this pull request as ready for review May 1, 2025 18:54
@jorenham
Copy link
Member Author

jorenham commented May 1, 2025

I'm a sphinx noob, so I had Claude write the release note. Content-wise it looks fine to me, but there might be sphinx issues or something 🤷🏻.

@jorenham jorenham force-pushed the typing/deprecate-npt.NBitBase branch from c404985 to 8176a27 Compare May 1, 2025 18:58
@jorenham
Copy link
Member Author

jorenham commented May 1, 2025

Diff from mypy_primer, showing the effect of this PR on type check results on a corpus of open source code:

pandas (https://github.com/pandas-dev/pandas)
+ pandas/_typing.py:91: error: Unused "type: ignore" comment  [unused-ignore]

See pandas-dev/pandas#61387

This comment has been minimized.

@jorenham jorenham force-pushed the typing/deprecate-npt.NBitBase branch from 8176a27 to c2fb472 Compare May 1, 2025 21:18
@jorenham jorenham force-pushed the typing/deprecate-npt.NBitBase branch from c2fb472 to 72fcaa9 Compare May 1, 2025 21:23
Copy link

github-actions bot commented May 1, 2025

Diff from mypy_primer, showing the effect of this PR on type check results on a corpus of open source code:

pandas (https://github.com/pandas-dev/pandas)
+ pandas/_typing.py:91: error: Unused "type: ignore" comment  [unused-ignore]

@charris
Copy link
Member

charris commented May 2, 2025

Do you consider this ready? I note several runs of mypy_primer.

@jorenham
Copy link
Member Author

jorenham commented May 3, 2025

Do you consider this ready? I note several runs of mypy_primer.

Ah yea sorry about that, I was tweaking "vibe coding" the release notes for a bit before. But it should be ready for review.

@charris charris merged commit 297caea into numpy:main May 3, 2025
75 checks passed
@charris
Copy link
Member

charris commented May 3, 2025

Thanks Joren, let's see how it goes.

The vibe coding is an interesting experiment. Is there anyway to control the line length?

@jorenham
Copy link
Member Author

jorenham commented May 3, 2025

Is there anyway to control the line length?

Hmmm, I guess you could try including it in the prompt 🤷🏻?

@jorenham jorenham deleted the typing/deprecate-npt.NBitBase branch May 3, 2025 20:12
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.

2 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy