MAINT: Misc cleaning of numpy.typing
#19118
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR introduces a number of miscellaneous cleaning- and style-related changes to
numpy.typing
To summarize:
_HAS_TYPING_EXTENSIONS
constant to signify whether or nottyping_extensions
is installed. This makes it redundant to perform the very sametry
/except
checks for every single sub-module.typing_extensions
, thus removing someif
-/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 ofProtocol
andLiteral
must be accessed at runtime.Any
-based runtime placeholders are now replaced withNotImplemented
, the latter better describing the variables' intent. This gradual shift fromAny
toNotImplemented
has been going on for some time now and is merely being finalized here._GenericAlias
is now used to get rid of these ugly literal-string expressions innumpy.typing._dtype_like
, the former allowing us the construct a subscriptable type on the spot.Optional
andUnion
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.