Skip to content

TYP: replace scalar type __init__ with __new__#29629

Merged
charris merged 2 commits intonumpy:mainfrom
jorenham:numtype/692
Sep 5, 2025
Merged

TYP: replace scalar type __init__ with __new__#29629
charris merged 2 commits intonumpy:mainfrom
jorenham:numtype/692

Conversation

@jorenham
Copy link
Copy Markdown
Member

@jorenham jorenham commented Aug 26, 2025

ported from numpy/numtype#692


Long story short; this is why:

>>> np.int8.__init__.__qualname__
'object.__init__'
>>> np.int8.__new__.__qualname__
'int8.__new__'

@jorenham jorenham added the numtype Isssue/PR related to numpy/numtype label Aug 26, 2025
@github-actions
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:

xarray (https://github.com/pydata/xarray)
+ xarray/namedarray/dtypes.py: note: In function "maybe_promote":
+ xarray/namedarray/dtypes.py:86: error: Too many arguments for "generic"  [call-arg]

@jorenham
Copy link
Copy Markdown
Member Author

jorenham commented Aug 27, 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/namedarray/dtypes.py: note: In function "maybe_promote":
+ xarray/namedarray/dtypes.py:86: error: Too many arguments for "generic"  [call-arg]

Relevant code: https://github.com/pydata/xarray/blob/98732e76481bdf5c497a030657cff32db134e27d/xarray/namedarray/dtypes.py#L48-L86

This new primer error is a true positive. They're calling dtype_out.type(fill_value) with dtype_out: np.dtype[np.generic]. Type-checkers will interpret that as np.generic(fill_value).
It's easy to fix by using np.dtype instead of np.dtype[np.generic] in the function signature. A generic-typing approach could also work, but would be more involved.

@charris charris merged commit 46709e8 into numpy:main Sep 5, 2025
78 checks passed
@charris
Copy link
Copy Markdown
Member

charris commented Sep 5, 2025

Thanks Joren. IIUC, it is up to xarray to fix the problem.

@jorenham
Copy link
Copy Markdown
Member Author

jorenham commented Sep 6, 2025

IIUC, it is up to xarray to fix the problem.

That's right

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

Labels

41 - Static typing numtype Isssue/PR related to numpy/numtype

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants