Skip to content

TYP: Fix DTypeLike runtime type-checker support#31425

Merged
charris merged 1 commit into
numpy:mainfrom
jorenham:typing/_HasNumPyDType/runtime_checkable
May 13, 2026
Merged

TYP: Fix DTypeLike runtime type-checker support#31425
charris merged 1 commit into
numpy:mainfrom
jorenham:typing/_HasNumPyDType/runtime_checkable

Conversation

@jorenham
Copy link
Copy Markdown
Member

@jorenham jorenham commented May 13, 2026

Runtime type-checkers like beartype currently aren't able to check if something is an instance of numpy.typing.DTypeLike because not all protocols in the union type aren't runtime checkable (see https://github.com/mikedh/trimesh/actions/runs/25796361932/job/75774562690?pr=2541 for example). The fix is to to add a @runtime_checkable to the _HasNumPyDType protocol.


On the 2.4 branch the _HasDType protocol is also missing a @runtime_checkable (

class _HasDType(Protocol[_DTypeT_co]):
@property
def dtype(self) -> _DTypeT_co: ...
class _HasNumPyDType(Protocol[_DTypeT_co]):
@property
def __numpy_dtype__(self, /) -> _DTypeT_co: ...
). So it should also be added there when backporting this, as well as importing runtime_checkable from typing.


No AI

@jorenham jorenham added 09 - Backport-Candidate PRs tagged should be backported 41 - Static typing labels May 13, 2026
@jorenham
Copy link
Copy Markdown
Member Author

Test failure seems unrelated

@charris charris merged commit 9c44f90 into numpy:main May 13, 2026
87 of 88 checks passed
@charris
Copy link
Copy Markdown
Member

charris commented May 13, 2026

Thanks Joren.

@charris
Copy link
Copy Markdown
Member

charris commented May 13, 2026

I note the file in 2.4.x is very different. Would it make sense to just backport the whole current version?

@jorenham
Copy link
Copy Markdown
Member Author

I note the file in 2.4.x is very different. Would it make sense to just backport the whole current version?

Hmm yea, maybe it's easier if I backport it myself then

@jorenham jorenham deleted the typing/_HasNumPyDType/runtime_checkable branch May 13, 2026 15:34
charris added a commit that referenced this pull request May 13, 2026
TYP: Fix `DTypeLike` runtime type-checker support (#31425)
@jorenham jorenham removed the 09 - Backport-Candidate PRs tagged should be backported label May 13, 2026
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