8000 ENH: Add aliases for common scalar unions by BvB93 · Pull Request #17096 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

ENH: Add aliases for common scalar unions #17096

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

Closed
wants to merge 8 commits into from
Closed

Conversation

BvB93
Copy link
Member
@BvB93 BvB93 commented Aug 18, 2020

This pull request adds a number of aliases for common scalar unions.

Numpy and builtin scalars can generally be used interchangeably, and this is reflected in the annotations of a number of functions (often when a plain ArrayLike is insufficiently descriptive).
The goal of this pull request is to define such annotations for common scalar-like objects and store them in a centralized location.

Examples

>>> from typing import Union
>>> import numpy as np

>>> _IntLike = Union[int, np.integer]
>>> _StrLike = Union[str, np.str_]
...

Bas van Beek added 2 commits August 18, 2020 23:43
…r `_FloatLike` and `_ComplexLike`

Mypy has special rules which ensures that `int` is treated as a `float` (and `complex`) superclass, however these rules do not apply to its `np.generic` counterparts.
Solution: manually add them them to the unions.
@rgommers
Copy link
Member

+1 this looks like a good idea to me.

# NOTE: mypy has special rules which ensures that `int` is treated as
# a `float` (and `complex`) superclass, however these rules do not apply
# to its `np.generic` counterparts.
# Solution: manually add them them to the unions below
Copy link
Member

Choose a reason for hiding this comment

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

double them

# to its `np.generic` counterparts.
# Solution: manually add them them to the unions below
_IntLike = Union[int, np.integer]
_FloatLike = Union[float, np.floating, np.integer]
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
_FloatLike = Union[float, np.floating, np.integer]
_FloatLike = Union[float, np.floating, _IntLike]

And same for the line below

@BvB93
Copy link
Member Author
BvB93 commented Aug 19, 2020

Converting this into a draft for now as #17105 arguably has a more elegant solution for the same issue.

@BvB93
Copy link
Member Author
BvB93 commented Sep 22, 2020

With #17117 closed the plan is to pick this up again once #17273 is merged.

@BvB93
Copy link
Member Author
BvB93 commented Oct 2, 2020

Rebasing this is starting to getting kinda messy with the amount of changes in __init__.pyi.
Closing this PR in favor of a new one.

@BvB93
Copy link
Member Author
BvB93 commented Oct 3, 2020

And here is the follow up: #17429

@BvB93 BvB93 deleted the scalar-union branch October 7, 2020 18:27
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.

4 participants
0