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

TYP: Concrete ``float64`` and ``complex128`` scalar types with builtin bases by jorenham · Pull Request #27334 · numpy/numpy · GitHub

/ numpy Public

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

Merged
charris merged 2 commits into
numpy:mainfrom
jorenham:typing/scalar_builtin_base
Sep 3, 2024
Merged

charris merged 2 commits into
numpy:mainfrom
jorenham:typing/scalar_builtin_base

Conversation

jorenham commented Sep 3, 2024
edited
Loading

Copy link
Copy Markdown
Member

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

Copy link
Copy Markdown
Member

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

charris commented Sep 3, 2024

Copy link
Copy Markdown
Member

Fixes #23081 #21906 #23663

You need "fixes" for each of the issues.

jorenham commented Sep 4, 2024

Copy link
Copy Markdown
Member Author

Fixes #23081 #21906 #23663

You need "fixes" for each of the issues.

Ah that explains

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 commented Oct 23, 2024
edited
Loading

Copy link
Copy Markdown

@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)?

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

Copy link
Copy Markdown
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)?

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.

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

3 participants


Back | FazBrowse Home | New Git URL