Skip to content
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

[typing] add internal DTypeLike specializations #18194

Closed
wants to merge 1 commit into from

Conversation

jakevdp
Copy link
Collaborator

@jakevdp jakevdp commented Oct 19, 2023

No description provided.

@jakevdp jakevdp requested a review from superbobry October 19, 2023 18:11
@jakevdp jakevdp self-assigned this Oct 19, 2023
@jakevdp jakevdp added the pull ready Ready for copybara import and testing label Oct 19, 2023
@jakevdp jakevdp force-pushed the dtypelike branch 4 times, most recently from cf6d817 to 6d87bfd Compare October 19, 2023 19:38
jax/_src/typing/dtypes.py Outdated Show resolved Hide resolved
jax/_src/typing/dtypes.py Show resolved Hide resolved
jax/_src/typing/dtypes.py Show resolved Hide resolved
jax/_src/typing/dtypes.py Outdated Show resolved Hide resolved
# Unlike np.typing.DTypeLike, we exclude None, and instead require
# explicit annotations when None is acceptable.
# TODO(jakevdp): consider whether to add ExtendedDtype to the union.
DTypeLike = Union[
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why is it a bad idea to define DTypeLike as a union of the specialized DTypeLike* aliases below?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

DTypeLike is used in public APIs, so if we change it there's a good chance the perturbation will break the builds of downstream packages. Plus the simplicity here is nice (even if there are invalid strings that it lets through).

@jakevdp
Copy link
Collaborator Author

jakevdp commented Nov 6, 2023

I don't think we can merge this: the issue is that these specializations reject valid code if the value of the string input is unknown statically to the type checker.

The semantics I want are to allow any generic string in general, BUT if the string value is known statically, it must match one of the known values.

As far as I understand, Python's type annotation syntax cannot express that, so accepting any str is the best we can do.

@jakevdp jakevdp closed this Nov 6, 2023
@jakevdp jakevdp deleted the dtypelike branch November 6, 2023 19:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pull ready Ready for copybara import and testing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants