8000 MAINT: Misc cleaning of `numpy.typing` by BvB93 · Pull Request #19118 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

MAINT: Misc cleaning of numpy.typing #19118

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 5 commits into from
May 28, 2021
Merged

MAINT: Misc cleaning of numpy.typing #19118

merged 5 commits into from
May 28, 2021

Conversation

BvB93
Copy link
Member
@BvB93 BvB93 commented May 27, 2021

This PR introduces a number of miscellaneous cleaning- and style-related changes to numpy.typing

To summarize:

  1. It adds a global _HAS_TYPING_EXTENSIONS constant to signify whether or not typing_extensions is installed. This makes it redundant to perform the very same try/except checks for every single sub-module.
  2. While type-checking, all python >3.7 exclusive types are now unconditionally imported from typing_extensions, thus removing some if-/else-check related verbosity. Aforementioned package is a de facto hard-dependency of type checkers, so this change should be safe to make. Note that this change is not applied to situations where the likes of Protocol and Literal must be accessed at runtime.
  3. All Any-based runtime placeholders are now replaced with NotImplemented, the latter better describing the variables' intent. This gradual shift from Any to NotImplemented has been going on for some time now and is merely being finalized here.
  4. _GenericAlias is now used to get rid of these ugly literal-string expressions in numpy.typing._dtype_like, the former allowing us the construct a subscriptable type on the spot.
  5. Last but not least: Optional and Union have been replaced with the PEP 604 pipe operator, the latter generally resulting in much cleaner looking code. Note that, as of now, not all type-checkers support usage of the pipe operator. With the 1.22 release being >6 months away, I'd say that we've given them a reasonable time frame to fix things or, alternatively, be left behind (xref MAINT: Remove usage of the PEP 604 pipe operator #18985).

Changes 2. and 5. should eventually be extended to the main stubs, but that's for the future.

Bas van Beek added 5 commits May 27, 2021 17:24
…ype checking

`typing_extensions` is a de facto hard dependency of all static type checkers (via its use in typeshed), so we can safely import all >3.7-exclusive types from there without version checking
PEP 604 -- Allow writing union types as X | Y
@BvB93
Copy link
Member Author
BvB93 commented May 28, 2021

The line-length-related linting errors can be addressed if so desired, just note that this will result in a substantially larger diff.

@charris
Copy link
Member
charris commented May 28, 2021

The line-length-related linting errors can be addressed

It's not any worse than it was :) This seems to be a case where long line improve readability -- if your screen is wide enough to show them.

@charris charris merged commit 0725c47 into numpy:main May 28, 2021
@charris
Copy link
Member
charris commented May 28, 2021

Thanks Bas.

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.

2 participants
0