Skip to content

Conversation

@MarcoGorelli
Copy link
Member

Addressing #29634 (comment)

I haven't removed | None from DTypeLike, as it's a publicly exported type and so changing it would be a breaking change, and I didn't know if that would be OK

Note that there are some places which currently accept dtype: DTypeLike, even though None cannot be passed. For example, isdtype:

def isdtype(dtype: dtype | type, kind: DTypeLike | tuple[DTypeLike, ...]) -> py_bool: ...

It says DTypeLike, which includes None, although None is not valid at runtime

Anyway, I've left such cases alone

@jorenham jorenham self-requested a review September 11, 2025 16:16
@jorenham
Copy link
Member

We could do a little experiment and try to see what mypy_primer will report when we remove | None from DTypeLike?

@jorenham jorenham changed the title Dtypelike none fix TYP: DTypeLike | None fix Sep 11, 2025
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@jorenham
Copy link
Member

Hmm, I expected worse. It's mostly xarray that's affected by this it seems.

@MarcoGorelli MarcoGorelli marked this pull request as ready for review September 12, 2025 15:13
@MarcoGorelli MarcoGorelli changed the title TYP: DTypeLike | None fix TYP: Remove None from definition of DTypeLike type alias Sep 12, 2025
@MarcoGorelli MarcoGorelli changed the title TYP: Remove None from definition of DTypeLike type alias TYP: Remove None from definition of DTypeLike type alias Sep 12, 2025
@MarcoGorelli
Copy link
Member Author

Yup. And even then, it looks manageable

IMHO this would be an acceptable change, but it's your decision of course

@jorenham
Copy link
Member

Since there's no way to deprecate this, there's no painless way of doing this, so I'm inclined to agree.

Copy link
Member

@jorenham jorenham left a comment

Choose a reason for hiding this comment

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

there are also some None-less DTypeLike's in _core.numerictypes

@MarcoGorelli
Copy link
Member Author

sure, i've added it to issubdtype (but the isdtype rejects None so i think it's correct without it?)

@jorenham
Copy link
Member

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@jorenham jorenham added the 56 - Needs Release Note. Needs an entry in doc/release/upcoming_changes label Sep 12, 2025
@jorenham jorenham added this to the 2.4.0 release milestone Sep 12, 2025
@jorenham
Copy link
Member

Ok the xarray PR has been merged, so it should be gone from the next mypy_primer run.

Also, it's probably a good idea to put this in the release notes.

@MarcoGorelli MarcoGorelli marked this pull request as draft September 29, 2025 18:51
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@MarcoGorelli MarcoGorelli marked this pull request as ready for review September 29, 2025 20:48
@github-actions

This comment has been minimized.

Co-authored-by: Joren Hammudoglu <[email protected]>
@github-actions
Copy link

github-actions bot commented Oct 1, 2025

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

xarray (https://github.com/pydata/xarray)
- xarray/compat/npcompat.py:54: note:     def isdtype(dtype: dtype[Any] | type, kind: type[Any] | dtype[Any] | _SupportsDType[dtype[Any]] | tuple[Any, Any] | list[Any] | _DTypeDict | str | None | tuple[type[Any] | dtype[Any] | _SupportsDType[dtype[Any]] | tuple[Any, Any] | list[Any] | _DTypeDict | str | None, ...]) -> bool
+ xarray/compat/npcompat.py:54: note:     def isdtype(dtype: dtype[Any] | type, kind: type[Any] | dtype[Any] | _SupportsDType[dtype[Any]] | tuple[Any, Any] | list[Any] | _DTypeDict | str | tuple[type[Any] | dtype[Any] | _SupportsDType[dtype[Any]] | tuple[Any, Any] | list[Any] | _DTypeDict | str, ...]) -> bool
- xarray/compat/npcompat.py:54: note:     def isdtype(dtype: dtype[Any] | type[Any], kind: type[Any] | dtype[Any] | _SupportsDType[dtype[Any]] | tuple[Any, Any] | list[Any] | _DTypeDict | str | None | tuple[type[Any] | dtype[Any] | _SupportsDType[dtype[Any]] | tuple[Any, Any] | list[Any] | _DTypeDict | str | None, ...]) -> bool
+ xarray/compat/npcompat.py:54: note:     def isdtype(dtype: dtype[Any] | type[Any], kind: type[Any] | dtype[Any] | _SupportsDType[dtype[Any]] | tuple[Any, Any] | list[Any] | _DTypeDict | str | tuple[type[Any] | dtype[Any] | _SupportsDType[dtype[Any]] | tuple[Any, Any] | list[Any] | _DTypeDict | str, ...]) -> bool

pandera (https://github.com/pandera-dev/pandera)
- pandera/engines/numpy_engine.py:60: error: Item "_SupportsDType[dtype[Any]]" of "type[Any] | dtype[Any] | _SupportsDType[dtype[Any]] | tuple[Any, Any] | list[Any] | _DTypeDict | str | None" has no attribute "type"  [union-attr]
+ pandera/engines/numpy_engine.py:60: error: Item "_SupportsDType[dtype[Any]]" of "type[Any] | dtype[Any] | _SupportsDType[dtype[Any]] | tuple[Any, Any] | list[Any] | _DTypeDict | str" has no attribute "type"  [union-attr]
- pandera/engines/numpy_engine.py:60: error: Item "tuple[Any, ...]" of "type[Any] | dtype[Any] | _SupportsDType[dtype[Any]] | tuple[Any, Any] | list[Any] | _DTypeDict | str | None" has no attribute "type"  [union-attr]
+ pandera/engines/numpy_engine.py:60: error: Item "tuple[Any, ...]" of "type[Any] | dtype[Any] | _SupportsDType[dtype[Any]] | tuple[Any, Any] | list[Any] | _DTypeDict | str" has no attribute "type"  [union-attr]
- pandera/engines/numpy_engine.py:60: error: Item "list[Any]" of "type[Any] | dtype[Any] | _SupportsDType[dtype[Any]] | tuple[Any, Any] | list[Any] | _DTypeDict | str | None" has no attribute "type"  [union-attr]
+ pandera/engines/numpy_engine.py:60: error: Item "list[Any]" of "type[Any] | dtype[Any] | _SupportsDType[dtype[Any]] | tuple[Any, Any] | list[Any] | _DTypeDict | str" has no attribute "type"  [union-attr]
- pandera/engines/numpy_engine.py:60: error: Item "_DTypeDict" of "type[Any] | dtype[Any] | _SupportsDType[dtype[Any]] | tuple[Any, Any] | list[Any] | _DTypeDict | str | None" has no attribute "type"  [union-attr]
+ pandera/engines/numpy_engine.py:60: error: Item "_DTypeDict" of "type[Any] | dtype[Any] | _SupportsDType[dtype[Any]] | tuple[Any, Any] | list[Any] | _DTypeDict | str" has no attribute "type"  [union-attr]
- pandera/engines/numpy_engine.py:60: error: Item "str" of "type[Any] | dtype[Any] | _SupportsDType[dtype[Any]] | tuple[Any, Any] | list[Any] | _DTypeDict | str | None" has no attribute "type"  [union-attr]
+ pandera/engines/numpy_engine.py:60: error: Item "str" of "type[Any] | dtype[Any] | _SupportsDType[dtype[Any]] | tuple[Any, Any] | list[Any] | _DTypeDict | str" has no attribute "type"  [union-attr]
- pandera/engines/numpy_engine.py:60: error: Item "None" of "type[Any] | dtype[Any] | _SupportsDType[dtype[Any]] | tuple[Any, Any] | list[Any] | _DTypeDict | str | None" has no attribute "type"  [union-attr]
- pandera/engines/numpy_engine.py:77: error: Item "_SupportsDType[dtype[Any]]" of "type[Any] | dtype[Any] | _SupportsDType[dtype[Any]] | tuple[Any, Any] | list[Any] | _DTypeDict | str | None" has no attribute "name"  [union-attr]
+ pandera/engines/numpy_engine.py:77: error: Item "_SupportsDType[dtype[Any]]" of "type[Any] | dtype[Any] | _SupportsDType[dtype[Any]] | tuple[Any, Any] | list[Any] | _DTypeDict | str" has no attribute "name"  [union-attr]
- pandera/engines/numpy_engine.py:77: error: Item "tuple[Any, ...]" of "type[Any] | dtype[Any] | _SupportsDType[dtype[Any]] | tuple[Any, Any] | list[Any] | _DTypeDict | str | None" has no attribute "name"  [union-attr]
+ pandera/engines/numpy_engine.py:77: error: Item "tuple[Any, ...]" of "type[Any] | dtype[Any] | _SupportsDType[dtype[Any]] | tuple[Any, Any] | list[Any] | _DTypeDict | str" has no attribute "name"  [union-attr]
- pandera/engines/numpy_engine.py:77: error: Item "list[Any]" of "type[Any] | dtype[Any] | _SupportsDType[dtype[Any]] | tuple[Any, Any] | list[Any] | _DTypeDict | str | None" has no attribute "name"  [union-attr]
+ pandera/engines/numpy_engine.py:77: error: Item "list[Any]" of "type[Any] | dtype[Any] | _SupportsDType[dtype[Any]] | tuple[Any, Any] | list[Any] | _DTypeDict | str" has no attribute "name"  [union-attr]
- pandera/engines/numpy_engine.py:77: error: Item "_DTypeDict" of "type[Any] | dtype[Any] | _SupportsDType[dtype[Any]] | tuple[Any, Any] | list[Any] | _DTypeDict | str | None" has no attribute "name"  [union-attr]
+ pandera/engines/numpy_engine.py:77: error: Item "_DTypeDict" of "type[Any] | dtype[Any] | _SupportsDType[dtype[Any]] | tuple[Any, Any] | list[Any] | _DTypeDict | str" has no attribute "name"  [union-attr]
- pandera/engines/numpy_engine.py:77: error: Item "str" of "type[Any] | dtype[Any] | _SupportsDType[dtype[Any]] | tuple[Any, Any] | list[Any] | _DTypeDict | str | None" has no attribute "name"  [union-attr]
+ pandera/engines/numpy_engine.py:77: error: Item "str" of "type[Any] | dtype[Any] | _SupportsDType[dtype[Any]] | tuple[Any, Any] | list[Any] | _DTypeDict | str" has no attribute "name"  [union-attr]
- pandera/engines/numpy_engine.py:77: error: Item "None" of "type[Any] | dtype[Any] | _SupportsDType[dtype[Any]] | tuple[Any, Any] | list[Any] | _DTypeDict | str | None" has no attribute "name"  [union-attr]

spark (https://github.com/apache/spark)
- python/pyspark/pandas/frame.py:11229: error: Argument 1 to "is_bool_dtype" has incompatible type "pyspark.pandas.series.Series[Any]"; expected "ExtensionArray | ndarray[tuple[Any, ...], dtype[Any]] | type[Any] | dtype[Any] | _SupportsDType[dtype[Any]] | tuple[Any, Any] | list[Any] | _DTypeDict | str | None | pandas.core.series.Series[Any] | DataFrame | Index[Any] | ExtensionDtype"  [arg-type]
+ python/pyspark/pandas/frame.py:11229: error: Argument 1 to "is_bool_dtype" has incompatible type "pyspark.pandas.series.Series[Any]"; expected "ExtensionArray | ndarray[tuple[Any, ...], dtype[Any]] | type[Any] | dtype[Any] | _SupportsDType[dtype[Any]] | tuple[Any, Any] | list[Any] | _DTypeDict | str | pandas.core.series.Series[Any] | DataFrame | Index[Any] | ExtensionDtype"  [arg-type]

@jorenham jorenham merged commit ffe55df into numpy:main Oct 2, 2025
76 of 79 checks passed
@jorenham
Copy link
Member

jorenham commented Oct 2, 2025

In it goes... Thanks Marco; great QOL improvement!

bwhitt7 pushed a commit to bwhitt7/numpy that referenced this pull request Oct 4, 2025
bwhitt7 pushed a commit to bwhitt7/numpy that referenced this pull request Oct 7, 2025
IndifferentArea pushed a commit to IndifferentArea/numpy that referenced this pull request Dec 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

41 - Static typing 56 - Needs Release Note. Needs an entry in doc/release/upcoming_changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants