FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

DEP: deprecate ``numpy.typing.NBitBase`` by jorenham · Pull Request #28884 · numpy/numpy · GitHub

/ numpy Public

DEP: deprecate numpy.typing.NBitBase - #28884

Merged
charris merged 2 commits into
numpy:mainfrom
jorenham:typing/deprecate-npt.NBitBase
May 3, 2025
Merged

charris merged 2 commits into
numpy:mainfrom
jorenham:typing/deprecate-npt.NBitBase

Conversation

jorenham commented May 1, 2025
edited
Loading

Copy link
Copy Markdown
Member

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 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 removed the 56 - Needs Release Note. Needs an entry in doc/release/upcoming_changes label May 1, 2025
jorenham marked this pull request as ready for review May 1, 2025 18:54

jorenham commented May 1, 2025

Copy link
Copy Markdown
Member Author

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 force-pushed the typing/deprecate-npt.NBitBase branch from c404985 to 8176a27 Compare May 1, 2025 18:58

jorenham commented May 1, 2025

Copy link
Copy Markdown
Member Author

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 force-pushed the typing/deprecate-npt.NBitBase branch from 8176a27 to c2fb472 Compare May 1, 2025 21:18
jorenham force-pushed the typing/deprecate-npt.NBitBase branch from c2fb472 to 72fcaa9 Compare May 1, 2025 21:23

github-actions Bot commented May 1, 2025

Copy link
Copy Markdown

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 commented May 2, 2025

Copy link
Copy Markdown
Member

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

jorenham commented May 3, 2025

Copy link
Copy Markdown
Member Author

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 merged commit 297caea into numpy:main May 3, 2025

charris commented May 3, 2025

Copy link
Copy Markdown
Member

Thanks Joren, let's see how it goes.

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

jorenham commented May 3, 2025

Copy link
Copy Markdown
Member Author

Is there anyway to control the line length?

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

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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


Back | FazBrowse Home | New Git URL