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

BUG: Fix shadowed reference of dtype in type stub #20278

Merged
merged 2 commits into from Nov 2, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
89 changes: 45 additions & 44 deletions numpy/__init__.pyi
Expand Up @@ -926,7 +926,7 @@ _DTypeScalar_co = TypeVar("_DTypeScalar_co", covariant=True, bound=generic)
_ByteOrder = L["S", "<", ">", "=", "|", "L", "B", "N", "I"]

class dtype(Generic[_DTypeScalar_co]):
names: Optional[Tuple[str, ...]]
names: Optional[Tuple[builtins.str, ...]]
# Overload for subclass of generic
@overload
def __new__(
Expand All @@ -953,7 +953,7 @@ class dtype(Generic[_DTypeScalar_co]):
@overload
def __new__(cls, dtype: Type[complex], align: bool = ..., copy: bool = ...) -> dtype[complex_]: ...
@overload
def __new__(cls, dtype: Type[str], align: bool = ..., copy: bool = ...) -> dtype[str_]: ...
def __new__(cls, dtype: Type[builtins.str], align: bool = ..., copy: bool = ...) -> dtype[str_]: ...
@overload
def __new__(cls, dtype: Type[bytes], align: bool = ..., copy: bool = ...) -> dtype[bytes_]: ...

Expand Down Expand Up @@ -1067,7 +1067,7 @@ class dtype(Generic[_DTypeScalar_co]):
@overload
def __new__(
cls,
dtype: str,
dtype: builtins.str,
align: bool = ...,
copy: bool = ...,
) -> dtype[Any]: ...
Expand All @@ -1089,9 +1089,9 @@ class dtype(Generic[_DTypeScalar_co]):
) -> dtype[object_]: ...

@overload
def __getitem__(self: dtype[void], key: List[str]) -> dtype[void]: ...
def __getitem__(self: dtype[void], key: List[builtins.str]) -> dtype[void]: ...
@overload
def __getitem__(self: dtype[void], key: Union[str, int]) -> dtype[Any]: ...
def __getitem__(self: dtype[void], key: Union[builtins.str, int]) -> dtype[Any]: ...

# NOTE: In the future 1-based multiplications will also yield `void` dtypes
@overload
Expand All @@ -1114,15 +1114,15 @@ class dtype(Generic[_DTypeScalar_co]):
@property
def base(self: _DType) -> _DType: ...
@property
def byteorder(self) -> str: ...
def byteorder(self) -> builtins.str: ...
@property
def char(self) -> str: ...
def char(self) -> builtins.str: ...
@property
def descr(self) -> List[Union[Tuple[str, str], Tuple[str, str, _Shape]]]: ...
def descr(self) -> List[Union[Tuple[builtins.str, builtins.str], Tuple[builtins.str, builtins.str, _Shape]]]: ...
@property
def fields(
self,
) -> Optional[Mapping[str, Union[Tuple[dtype[Any], int], Tuple[dtype[Any], int, Any]]]]: ...
) -> Optional[Mapping[builtins.str, Union[Tuple[dtype[Any], int], Tuple[dtype[Any], int, Any]]]]: ...
@property
def flags(self) -> int: ...
@property
Expand All @@ -1136,11 +1136,11 @@ class dtype(Generic[_DTypeScalar_co]):
@property
def itemsize(self) -> int: ...
@property
def kind(self) -> str: ...
def kind(self) -> builtins.str: ...
@property
def metadata(self) -> Optional[Mapping[str, Any]]: ...
def metadata(self) -> Optional[Mapping[builtins.str, Any]]: ...
@property
def name(self) -> str: ...
def name(self) -> builtins.str: ...
@property
def names(self) -> Optional[Tuple[str, ...]]: ...
@property
Expand All @@ -1152,8 +1152,6 @@ class dtype(Generic[_DTypeScalar_co]):
@property
def subdtype(self: _DType) -> Optional[Tuple[_DType, _Shape]]: ...
def newbyteorder(self: _DType, __new_order: _ByteOrder = ...) -> _DType: ...
# Leave str and type for end to avoid having to use `builtins.str`
# everywhere. See https://github.com/python/mypy/issues/3775
@property
def str(self) -> builtins.str: ...
@property
Expand Down Expand Up @@ -1659,6 +1657,9 @@ _ArrayComplex_co = NDArray[Union[bool_, integer[Any], floating[Any], complexfloa
_ArrayNumber_co = NDArray[Union[bool_, number[Any]]]
_ArrayTD64_co = NDArray[Union[bool_, integer[Any], timedelta64]]

# Introduce an alias for `dtype` to avoid naming conflicts.
_dtype = dtype

class _SupportsItem(Protocol[_T_co]):
def item(self, __args: Any) -> _T_co: ...

Expand All @@ -1680,13 +1681,13 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]):
@property
def real(
self: NDArray[_SupportsReal[_ScalarType]], # type: ignore[type-var]
) -> ndarray[_ShapeType, dtype[_ScalarType]]: ...
) -> ndarray[_ShapeType, _dtype[_ScalarType]]: ...
@real.setter
def real(self, value: ArrayLike) -> None: ...
@property
def imag(
self: NDArray[_SupportsImag[_ScalarType]], # type: ignore[type-var]
) -> ndarray[_ShapeType, dtype[_ScalarType]]: ...
) -> ndarray[_ShapeType, _dtype[_ScalarType]]: ...
@imag.setter
def imag(self, value: ArrayLike) -> None: ...
def __new__(
Expand Down Expand Up @@ -1720,12 +1721,12 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]):
# Use the same output type as that of the underlying `generic`
@overload
def item(
self: ndarray[Any, dtype[_SupportsItem[_T]]], # type: ignore[type-var]
self: ndarray[Any, _dtype[_SupportsItem[_T]]], # type: ignore[type-var]
*args: SupportsIndex,
) -> _T: ...
@overload
def item(
self: ndarray[Any, dtype[_SupportsItem[_T]]], # type: ignore[type-var]
self: ndarray[Any, _dtype[_SupportsItem[_T]]], # type: ignore[type-var]
__args: Tuple[SupportsIndex, ...],
) -> _T: ...

Expand Down Expand Up @@ -1765,7 +1766,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]):
axis: Optional[SupportsIndex] = ...,
kind: _PartitionKind = ...,
order: Union[None, str, Sequence[str]] = ...,
) -> ndarray[Any, dtype[intp]]: ...
) -> ndarray[Any, _dtype[intp]]: ...

def diagonal(
self,
Expand All @@ -1784,7 +1785,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]):
def dot(self, b: ArrayLike, out: _NdArraySubClass) -> _NdArraySubClass: ...

# `nonzero()` is deprecated for 0d arrays/generics
def nonzero(self) -> Tuple[ndarray[Any, dtype[intp]], ...]: ...
def nonzero(self) -> Tuple[ndarray[Any, _dtype[intp]], ...]: ...

def partition(
self,
Expand Down Expand Up @@ -1816,7 +1817,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]):
v: ArrayLike,
side: _SortSide = ...,
sorter: Optional[_ArrayLikeInt_co] = ...,
) -> ndarray[Any, dtype[intp]]: ...
) -> ndarray[Any, _dtype[intp]]: ...

def setfield(
self,
Expand Down Expand Up @@ -1853,7 +1854,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]):

@overload
def take( # type: ignore[misc]
self: ndarray[Any, dtype[_ScalarType]],
self: ndarray[Any, _dtype[_ScalarType]],
indices: _IntLike_co,
axis: Optional[SupportsIndex] = ...,
out: None = ...,
Expand Down Expand Up @@ -1950,19 +1951,19 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]):

# Dispatch to the underlying `generic` via protocols
def __int__(
self: ndarray[Any, dtype[SupportsInt]], # type: ignore[type-var]
self: ndarray[Any, _dtype[SupportsInt]], # type: ignore[type-var]
) -> int: ...

def __float__(
self: ndarray[Any, dtype[SupportsFloat]], # type: ignore[type-var]
self: ndarray[Any, _dtype[SupportsFloat]], # type: ignore[type-var]
) -> float: ...

def __complex__(
self: ndarray[Any, dtype[SupportsComplex]], # type: ignore[type-var]
self: ndarray[Any, _dtype[SupportsComplex]], # type: ignore[type-var]
) -> complex: ...

def __index__(
self: ndarray[Any, dtype[SupportsIndex]], # type: ignore[type-var]
self: ndarray[Any, _dtype[SupportsIndex]], # type: ignore[type-var]
) -> int: ...

def __len__(self) -> int: ...
Expand Down Expand Up @@ -2138,7 +2139,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]):
@overload
def __mod__(self: _ArrayFloat_co, other: _ArrayLikeFloat_co) -> NDArray[floating[Any]]: ... # type: ignore[misc]
@overload
def __mod__(self: _ArrayTD64_co, other: _NestedSequence[_SupportsArray[dtype[timedelta64]]]) -> NDArray[timedelta64]: ...
def __mod__(self: _ArrayTD64_co, other: _NestedSequence[_SupportsArray[_dtype[timedelta64]]]) -> NDArray[timedelta64]: ...
@overload
def __mod__(self: NDArray[object_], other: Any) -> Any: ...
@overload
Expand All @@ -2160,7 +2161,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]):
@overload
def __rmod__(self: _ArrayFloat_co, other: _ArrayLikeFloat_co) -> NDArray[floating[Any]]: ... # type: ignore[misc]
@overload
def __rmod__(self: _ArrayTD64_co, other: _NestedSequence[_SupportsArray[dtype[timedelta64]]]) -> NDArray[timedelta64]: ...
def __rmod__(self: _ArrayTD64_co, other: _NestedSequence[_SupportsArray[_dtype[timedelta64]]]) -> NDArray[timedelta64]: ...
@overload
def __rmod__(self: NDArray[object_], other: Any) -> Any: ...
@overload
Expand All @@ -2182,7 +2183,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]):
@overload
def __divmod__(self: _ArrayFloat_co, other: _ArrayLikeFloat_co) -> _2Tuple[NDArray[floating[Any]]]: ... # type: ignore[misc]
@overload
def __divmod__(self: _ArrayTD64_co, other: _NestedSequence[_SupportsArray[dtype[timedelta64]]]) -> Tuple[NDArray[int64], NDArray[timedelta64]]: ...
def __divmod__(self: _ArrayTD64_co, other: _NestedSequence[_SupportsArray[_dtype[timedelta64]]]) -> Tuple[NDArray[int64], NDArray[timedelta64]]: ...
@overload
def __divmod__(
self: NDArray[Union[bool_, integer[Any], floating[Any], timedelta64]],
Expand All @@ -2200,7 +2201,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]):
@overload
def __rdivmod__(self: _ArrayFloat_co, other: _ArrayLikeFloat_co) -> _2Tuple[NDArray[floating[Any]]]: ... # type: ignore[misc]
@overload
def __rdivmod__(self: _ArrayTD64_co, other: _NestedSequence[_SupportsArray[dtype[timedelta64]]]) -> Tuple[NDArray[int64], NDArray[timedelta64]]: ...
def __rdivmod__(self: _ArrayTD64_co, other: _NestedSequence[_SupportsArray[_dtype[timedelta64]]]) -> Tuple[NDArray[int64], NDArray[timedelta64]]: ...
@overload
def __rdivmod__(
self: NDArray[Union[bool_, integer[Any], floating[Any], timedelta64]],
Expand Down Expand Up @@ -2384,7 +2385,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]):
@overload
def __floordiv__(self: _ArrayComplex_co, other: _ArrayLikeComplex_co) -> NDArray[complexfloating[Any, Any]]: ... # type: ignore[misc]
@overload
def __floordiv__(self: NDArray[timedelta64], other: _NestedSequence[_SupportsArray[dtype[timedelta64]]]) -> NDArray[int64]: ...
def __floordiv__(self: NDArray[timedelta64], other: _NestedSequence[_SupportsArray[_dtype[timedelta64]]]) -> NDArray[int64]: ...
@overload
def __floordiv__(self: NDArray[timedelta64], other: _ArrayLikeBool_co) -> NoReturn: ...
@overload
Expand Down Expand Up @@ -2412,7 +2413,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]):
@overload
def __rfloordiv__(self: _ArrayComplex_co, other: _ArrayLikeComplex_co) -> NDArray[complexfloating[Any, Any]]: ... # type: ignore[misc]
@overload
def __rfloordiv__(self: NDArray[timedelta64], other: _NestedSequence[_SupportsArray[dtype[timedelta64]]]) -> NDArray[int64]: ...
def __rfloordiv__(self: NDArray[timedelta64], other: _NestedSequence[_SupportsArray[_dtype[timedelta64]]]) -> NDArray[int64]: ...
@overload
def __rfloordiv__(self: NDArray[bool_], other: _ArrayLikeTD64_co) -> NoReturn: ...
@overload
Expand Down Expand Up @@ -2480,7 +2481,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]):
@overload
def __truediv__(self: _ArrayComplex_co, other: _ArrayLikeComplex_co) -> NDArray[complexfloating[Any, Any]]: ... # type: ignore[misc]
@overload
def __truediv__(self: NDArray[timedelta64], other: _NestedSequence[_SupportsArray[dtype[timedelta64]]]) -> NDArray[float64]: ...
def __truediv__(self: NDArray[timedelta64], other: _NestedSequence[_SupportsArray[_dtype[timedelta64]]]) -> NDArray[float64]: ...
@overload
def __truediv__(self: NDArray[timedelta64], other: _ArrayLikeBool_co) -> NoReturn: ...
@overload
Expand All @@ -2504,7 +2505,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]):
@overload
def __rtruediv__(self: _ArrayComplex_co, other: _ArrayLikeComplex_co) -> NDArray[complexfloating[Any, Any]]: ... # type: ignore[misc]
@overload
def __rtruediv__(self: NDArray[timedelta64], other: _NestedSequence[_SupportsArray[dtype[timedelta64]]]) -> NDArray[float64]: ...
def __rtruediv__(self: NDArray[timedelta64], other: _NestedSequence[_SupportsArray[_dtype[timedelta64]]]) -> NDArray[float64]: ...
@overload
def __rtruediv__(self: NDArray[bool_], other: _ArrayLikeTD64_co) -> NoReturn: ...
@overload
Expand Down Expand Up @@ -2817,7 +2818,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]):
@overload
def __imod__(self: NDArray[floating[_NBit1]], other: _ArrayLikeFloat_co) -> NDArray[floating[_NBit1]]: ...
@overload
def __imod__(self: NDArray[timedelta64], other: _NestedSequence[_SupportsArray[dtype[timedelta64]]]) -> NDArray[timedelta64]: ...
def __imod__(self: NDArray[timedelta64], other: _NestedSequence[_SupportsArray[_dtype[timedelta64]]]) -> NDArray[timedelta64]: ...
@overload
def __imod__(self: NDArray[object_], other: Any) -> NDArray[object_]: ...
@overload
Expand Down Expand Up @@ -2904,7 +2905,7 @@ class generic(_ArrayOrScalarCommon):
@abstractmethod
def __init__(self, *args: Any, **kwargs: Any) -> None: ...
@overload
def __array__(self: _ScalarType, __dtype: None = ...) -> ndarray[Any, dtype[_ScalarType]]: ...
def __array__(self: _ScalarType, __dtype: None = ...) -> ndarray[Any, _dtype[_ScalarType]]: ...
@overload
def __array__(self, __dtype: _DType) -> ndarray[Any, _DType]: ...
@property
Expand All @@ -2919,7 +2920,7 @@ class generic(_ArrayOrScalarCommon):
def strides(self) -> Tuple[()]: ...
def byteswap(self: _ScalarType, inplace: L[False] = ...) -> _ScalarType: ...
@property
def flat(self: _ScalarType) -> flatiter[ndarray[Any, dtype[_ScalarType]]]: ...
def flat(self: _ScalarType) -> flatiter[ndarray[Any, _dtype[_ScalarType]]]: ...

@overload
def astype(
Expand Down Expand Up @@ -2993,7 +2994,7 @@ class generic(_ArrayOrScalarCommon):
axis: Optional[SupportsIndex] = ...,
out: None = ...,
mode: _ModeKind = ...,
) -> ndarray[Any, dtype[_ScalarType]]: ...
) -> ndarray[Any, _dtype[_ScalarType]]: ...
@overload
def take(
self,
Expand All @@ -3007,34 +3008,34 @@ class generic(_ArrayOrScalarCommon):
self: _ScalarType,
repeats: _ArrayLikeInt_co,
axis: Optional[SupportsIndex] = ...,
) -> ndarray[Any, dtype[_ScalarType]]: ...
) -> ndarray[Any, _dtype[_ScalarType]]: ...

def flatten(
self: _ScalarType,
order: _OrderKACF = ...,
) -> ndarray[Any, dtype[_ScalarType]]: ...
) -> ndarray[Any, _dtype[_ScalarType]]: ...

def ravel(
self: _ScalarType,
order: _OrderKACF = ...,
) -> ndarray[Any, dtype[_ScalarType]]: ...
) -> ndarray[Any, _dtype[_ScalarType]]: ...

@overload
def reshape(
self: _ScalarType, __shape: _ShapeLike, *, order: _OrderACF = ...
) -> ndarray[Any, dtype[_ScalarType]]: ...
) -> ndarray[Any, _dtype[_ScalarType]]: ...
@overload
def reshape(
self: _ScalarType, *shape: SupportsIndex, order: _OrderACF = ...
) -> ndarray[Any, dtype[_ScalarType]]: ...
) -> ndarray[Any, _dtype[_ScalarType]]: ...

def squeeze(
self: _ScalarType, axis: Union[L[0], Tuple[()]] = ...
) -> _ScalarType: ...
def transpose(self: _ScalarType, __axes: Tuple[()] = ...) -> _ScalarType: ...
# Keep `dtype` at the bottom to avoid name conflicts with `np.dtype`
@property
def dtype(self: _ScalarType) -> dtype[_ScalarType]: ...
def dtype(self: _ScalarType) -> _dtype[_ScalarType]: ...

class number(generic, Generic[_NBit1]): # type: ignore
@property
Expand Down