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
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
ENH: Add a global constant to numpy.typing denoting whether or not …
…`typing_extensions` is available
  • Loading branch information
Bas van Beek committed May 27, 2021
commit 66fa0481c0dd85d4f90dc56afe3f1b42b96945ad
12 changes: 12 additions & 0 deletions numpy/typing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,18 @@

from typing import TYPE_CHECKING, List

if TYPE_CHECKING:
# typing_extensions is always available when type-checking
from typing_extensions import Literal as L
_HAS_TYPING_EXTENSIONS: L[True]
else:
try:
import typing_extensions
except ImportError:
_HAS_TYPING_EXTENSIONS = False
else:
_HAS_TYPING_EXTENSIONS = True

if TYPE_CHECKING:
import sys
if sys.version_info >= (3, 8):
Expand Down
15 changes: 6 additions & 9 deletions numpy/typing/_array_like.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,20 @@
bytes_,
)

from . import _HAS_TYPING_EXTENSIONS
from ._dtype_like import DTypeLike

if sys.version_info >= (3, 8):
from typing import Protocol
HAVE_PROTOCOL = True
else:
try:
from typing_extensions import Protocol
except ImportError:
HAVE_PROTOCOL = False
else:
HAVE_PROTOCOL = True
elif _HAS_TYPING_EXTENSIONS:
from typing_extensions import Protocol

_T = TypeVar("_T")
_ScalarType = TypeVar("_ScalarType", bound=generic)
_DType = TypeVar("_DType", bound="dtype[Any]")
_DType_co = TypeVar("_DType_co", covariant=True, bound="dtype[Any]")

if TYPE_CHECKING or HAVE_PROTOCOL:
if TYPE_CHECKING or _HAS_TYPING_EXTENSIONS:
# The `_SupportsArray` protocol only cares about the default dtype
# (i.e. `dtype=None` or no `dtype` parameter at all) of the to-be returned
# array.
Expand Down
16 changes: 5 additions & 11 deletions numpy/typing/_callable.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,15 @@
_FloatLike_co,
_NumberLike_co,
)
from . import NBitBase
from . import NBitBase, _HAS_TYPING_EXTENSIONS
from ._generic_alias import NDArray

if sys.version_info >= (3, 8):
from typing import Protocol
HAVE_PROTOCOL = True
else:
try:
from typing_extensions import Protocol
except ImportError:
HAVE_PROTOCOL = False
else:
HAVE_PROTOCOL = True

if TYPE_CHECKING or HAVE_PROTOCOL:
elif _HAS_TYPING_EXTENSIONS:
from typing_extensions import Protocol

if TYPE_CHECKING or _HAS_TYPING_EXTENSIONS:
_T1 = TypeVar("_T1")
_T2 = TypeVar("_T2")
_2Tuple = Tuple[_T1, _T1]
Expand Down
16 changes: 6 additions & 10 deletions numpy/typing/_char_codes.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import sys
from typing import Any, TYPE_CHECKING

from . import _HAS_TYPING_EXTENSIONS

if sys.version_info >= (3, 8):
from typing import Literal
HAVE_LITERAL = True
else:
try:
from typing_extensions import Literal
except ImportError:
HAVE_LITERAL = False
else:
HAVE_LITERAL = True

if TYPE_CHECKING or HAVE_LITERAL:
elif _HAS_TYPING_EXTENSIONS:
from typing_extensions import Literal

if TYPE_CHECKING or _HAS_TYPING_EXTENSIONS:
_BoolCodes = Literal["?", "=?", "<?", ">?", "bool", "bool_", "bool8"]

_UInt8Codes = Literal["uint8", "u1", "=u1", "<u1", ">u1"]
Expand Down
14 changes: 5 additions & 9 deletions numpy/typing/_dtype_like.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,14 @@
from typing import Any, List, Sequence, Tuple, Union, Type, TypeVar, TYPE_CHECKING

import numpy as np

from . import _HAS_TYPING_EXTENSIONS
from ._shape import _ShapeLike

if sys.version_info >= (3, 8):
from typing import Protocol, TypedDict
HAVE_PROTOCOL = True
else:
try:
from typing_extensions import Protocol, TypedDict
except ImportError:
HAVE_PROTOCOL = False
else:
HAVE_PROTOCOL = True
elif _HAS_TYPING_EXTENSIONS:
from typing_extensions import Protocol, TypedDict

from ._char_codes import (
_BoolCodes,
Expand Down Expand Up @@ -59,7 +55,7 @@

_DTypeLikeNested = Any # TODO: wait for support for recursive types

if TYPE_CHECKING or HAVE_PROTOCOL:
if TYPE_CHECKING or _HAS_TYPING_EXTENSIONS:
# Mandatory keys
class _DTypeDictBase(TypedDict):
names: Sequence[str]
Expand Down
9 changes: 5 additions & 4 deletions numpy/typing/_shape.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import sys
from typing import Sequence, Tuple, Union

from . import _HAS_TYPING_EXTENSIONS

if sys.version_info >= (3, 8):
from typing import SupportsIndex
elif _HAS_TYPING_EXTENSIONS:
from typing_extensions import SupportsIndex
else:
try:
from typing_extensions import SupportsIndex
except ImportError:
SupportsIndex = NotImplemented
SupportsIndex = NotImplemented

_Shape = Tuple[int, ...]

Expand Down
0