Skip to content

TYP: Concrete float64 and complex128 scalar types with builtin bases #27334

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
Sep 3, 2024

Conversation

jorenham
Copy link
Member

@jorenham jorenham commented Sep 3, 2024

At runtime, numpy.float64 subclasses builtins.float, and numpy.complex128 subclasses builtins.complex. But the typing stubs currently do not reflect this. The consequence is that e.g. spam: float = np.float64(22 / 7) will be marked as an error by static type-checkers.

This PR introduces concrete types for float64 and complex128 that, like at runtime, (also) inherit from the float and complex builtin types, respectively.

Note that that the amount of type-tests had to be adjusted to accommodate this change, doesn't reflect the impact of this change on existing codebases is this case. This is becaue the typing.assert that is used in these type-tests, requires the exact type to match, so assert_type considers floating[_64Bit] and float64 as totally different types, even though float64 <: floating[_64Bit]. So for existing typing annotations this won't require a change.

Fixes #23081
Fixes #21906
Fixes #23663

@charris charris merged commit 27574f8 into numpy:main Sep 3, 2024
67 checks passed
@charris
Copy link
Member

charris commented Sep 3, 2024

Thanks @jorenham . I'll go with your judgement here.

@charris
Copy link
Member

charris commented Sep 3, 2024

Fixes #23081 #21906 #23663

You need "fixes" for each of the issues.

@jorenham
Copy link
Member Author

jorenham commented Sep 4, 2024

Fixes #23081 #21906 #23663

You need "fixes" for each of the issues.

Ah that explains

@jorenham jorenham deleted the typing/scalar_builtin_base branch September 4, 2024 08:31
jorenham added a commit to jorenham/numpy that referenced this pull request Sep 18, 2024
charris added a commit that referenced this pull request Sep 18, 2024
@allanleal
Copy link

allanleal commented Oct 23, 2024

@jorenham Thanks for implementing these changes to resolve static type checking issues involving Python and numpy float types. At the time you implement them, did you also consider covering the case below (assigning a float to a np.float64)?

image

I installed latest numpy with your changes using:
pip install git+https://github.com/numpy/numpy.git@main

@jorenham
Copy link
Member Author

@jorenham Thanks for implementing these changes to resolve static type checking issues involving Python and numpy float types. At the time you implement them, did you also consider covering the case below (assigning a float to a np.float64)?

image

I installed latest numpy with your changes using:
pip install git+https://github.com/numpy/numpy.git@main

You're assigning a supertype to a subtype here; that's not valid. You also can't assign an int to a bool, but you can assign a bool to an int.

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