Skip to content

Y026: explicitly declare type aliases as TypeAlias #349

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 1 commit into from
Oct 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ repos:
- flake8-pyi==22.8.2
types: [pyi]
args: [
--ignore=E301 E302 E305 E402 E501 E701 E704 F401 F811 W503 Y019 Y026 Y027 Y034 Y037 Y041,
--ignore=E301 E302 E305 E402 E501 E701 E704 F401 F811 W503 Y019 Y027 Y034 Y037 Y041 Y042,
# TypeVars in private files are already private
--per-file-ignores=_*.pyi:Y001
]
5 changes: 4 additions & 1 deletion pandas-stubs/_libs/tslibs/nattype.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ from datetime import (
from typing import Union

import numpy as np
from typing_extensions import TypeAlias

from pandas._libs.tslibs.period import Period

NaT: NaTType
iNaT: int
nat_strings: set[str]

_NaTComparisonTypes = Union[datetime, timedelta, Period, np.datetime64, np.timedelta64]
_NaTComparisonTypes: TypeAlias = Union[
datetime, timedelta, Period, np.datetime64, np.timedelta64
]

class _NatComparison:
def __call__(self, other: _NaTComparisonTypes) -> bool: ...
Expand Down
3 changes: 2 additions & 1 deletion pandas-stubs/_libs/tslibs/timedeltas.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ from typing import (
)

import numpy as np
from typing_extensions import TypeAlias

from pandas._libs.tslibs import (
NaTType,
Expand All @@ -16,7 +17,7 @@ from pandas._typing import npt

# This should be kept consistent with the keys in the dict timedelta_abbrevs
# in pandas/_libs/tslibs/timedeltas.pyx
UnitChoices = Literal[
UnitChoices: TypeAlias = Literal[
"Y",
"y",
"M",
Expand Down
189 changes: 97 additions & 92 deletions pandas-stubs/_typing.pyi
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
from builtins import type as type_t
import datetime
from io import (
BufferedIOBase,
RawIOBase,
TextIOBase,
TextIOWrapper,
)
from mmap import mmap
from os import PathLike
from typing import (
IO,
Any,
AnyStr,
Callable,
Hashable,
Iterator,
Expand All @@ -32,6 +23,7 @@ from pandas.core.generic import NDFrame
from pandas.core.groupby.grouper import Grouper
from pandas.core.indexes.base import Index
from pandas.core.series import Series
from typing_extensions import TypeAlias

from pandas._libs.tslibs import (
Period,
Expand All @@ -43,22 +35,28 @@ from pandas.core.dtypes.dtypes import ExtensionDtype

from pandas.io.formats.format import EngFormatter

ArrayLike = Union[ExtensionArray, np.ndarray]
AnyArrayLike = Union[Index, Series, np.ndarray]
PythonScalar = Union[str, bool, complex]
ArrayLike: TypeAlias = Union[ExtensionArray, np.ndarray]
AnyArrayLike: TypeAlias = Union[Index, Series, np.ndarray]
PythonScalar: TypeAlias = Union[str, bool, complex]
DatetimeLikeScalar = TypeVar("DatetimeLikeScalar", Period, Timestamp, Timedelta)
PandasScalar = Union[bytes, datetime.date, datetime.datetime, datetime.timedelta]
# Scalar = Union[PythonScalar, PandasScalar]
PandasScalar: TypeAlias = Union[
bytes, datetime.date, datetime.datetime, datetime.timedelta
]
# Scalar: TypeAlias = Union[PythonScalar, PandasScalar]

DatetimeLike = Union[datetime.date, datetime.datetime, np.datetime64, Timestamp]
DatetimeLike: TypeAlias = Union[
datetime.date, datetime.datetime, np.datetime64, Timestamp
]

# dtypes
NpDtype = Union[str, np.dtype[np.generic], type[Union[str, complex, bool, object]]]
Dtype = Union[ExtensionDtype, NpDtype]
AstypeArg = Union[ExtensionDtype, npt.DTypeLike]
NpDtype: TypeAlias = Union[
str, np.dtype[np.generic], type[Union[str, complex, bool, object]]
]
Dtype: TypeAlias = Union[ExtensionDtype, NpDtype]
AstypeArg: TypeAlias = Union[ExtensionDtype, npt.DTypeLike]
# DtypeArg specifies all allowable dtypes in a functions its dtype argument
DtypeArg = Union[Dtype, dict[Any, Dtype]]
DtypeObj = Union[np.dtype[np.generic], ExtensionDtype]
DtypeArg: TypeAlias = Union[Dtype, dict[Any, Dtype]]
DtypeObj: TypeAlias = Union[np.dtype[np.generic], ExtensionDtype]

# filenames and file-like-objects
AnyStr_cov = TypeVar("AnyStr_cov", str, bytes, covariant=True)
Expand Down Expand Up @@ -91,47 +89,48 @@ class ReadCsvBuffer(ReadBuffer[AnyStr_cov], Protocol[AnyStr_cov]):
class WriteExcelBuffer(WriteBuffer[bytes], Protocol):
def truncate(self, size: int | None = ...) -> int: ...

FilePath = Union[str, PathLike[str]]
FilePathOrBuffer = Union[
FilePath, IO[AnyStr], RawIOBase, BufferedIOBase, TextIOBase, TextIOWrapper, mmap
]

Axis = Union[str, int]
IndexLabel = Union[Hashable, Sequence[Hashable]]
Label = Optional[Hashable]
Level = Union[Hashable, int]
Suffixes = tuple[Optional[str], Optional[str]]
Ordered = Optional[bool]
JSONSerializable = Union[PythonScalar, list, dict]
Axes = Union[AnyArrayLike, list, dict, range]
Renamer = Union[Mapping[Any, Label], Callable[[Any], Label]]
FilePath: TypeAlias = Union[str, PathLike[str]]

Axis: TypeAlias = Union[str, int]
IndexLabel: TypeAlias = Union[Hashable, Sequence[Hashable]]
Label: TypeAlias = Optional[Hashable]
Level: TypeAlias = Union[Hashable, int]
Suffixes: TypeAlias = tuple[Optional[str], Optional[str]]
Ordered: TypeAlias = Optional[bool]
JSONSerializable: TypeAlias = Union[PythonScalar, list, dict]
Axes: TypeAlias = Union[AnyArrayLike, list, dict, range]
Renamer: TypeAlias = Union[Mapping[Any, Label], Callable[[Any], Label]]
T = TypeVar("T")
FuncType = Callable[..., Any]
FuncType: TypeAlias = Callable[..., Any]
F = TypeVar("F", bound=FuncType)
HashableT = TypeVar("HashableT", bound=Hashable)

AggFuncTypeBase = Union[Callable, str, np.ufunc]
AggFuncTypeDictSeries = dict[Hashable, AggFuncTypeBase]
AggFuncTypeDictFrame = dict[Hashable, Union[AggFuncTypeBase, list[AggFuncTypeBase]]]
AggFuncTypeSeriesToFrame = Union[
AggFuncTypeBase: TypeAlias = Union[Callable, str, np.ufunc]
AggFuncTypeDictSeries: TypeAlias = dict[Hashable, AggFuncTypeBase]
AggFuncTypeDictFrame: TypeAlias = dict[
Hashable, Union[AggFuncTypeBase, list[AggFuncTypeBase]]
]
AggFuncTypeSeriesToFrame: TypeAlias = Union[
list[AggFuncTypeBase],
AggFuncTypeDictSeries,
]
AggFuncTypeFrame = Union[
AggFuncTypeFrame: TypeAlias = Union[
AggFuncTypeBase,
list[AggFuncTypeBase],
AggFuncTypeDictFrame,
]

num = complex
SeriesAxisType = Literal["index", 0] # Restricted subset of _AxisType for series
AxisType = Literal["columns", "index", 0, 1]
num: TypeAlias = complex
SeriesAxisType: TypeAlias = Literal[
"index", 0
] # Restricted subset of _AxisType for series
AxisType: TypeAlias = Literal["columns", "index", 0, 1]
DtypeNp = TypeVar("DtypeNp", bound=np.dtype[np.generic])
KeysArgType = Any
KeysArgType: TypeAlias = Any
ListLike = TypeVar("ListLike", Sequence, np.ndarray, "Series", "Index")
ListLikeU = Union[Sequence, np.ndarray, Series, Index]
StrLike = Union[str, np.str_]
Scalar = Union[
ListLikeU: TypeAlias = Union[Sequence, np.ndarray, Series, Index]
StrLike: TypeAlias = Union[str, np.str_]
Scalar: TypeAlias = Union[
str,
bytes,
datetime.date,
Expand All @@ -146,14 +145,14 @@ Scalar = Union[
]
ScalarT = TypeVar("ScalarT", bound=Scalar)
# Refine the definitions below in 3.9 to use the specialized type.
np_ndarray_int64 = npt.NDArray[np.int64]
np_ndarray_int = npt.NDArray[np.signedinteger]
np_ndarray_anyint = npt.NDArray[np.integer]
np_ndarray_bool = npt.NDArray[np.bool_]
np_ndarray_str = npt.NDArray[np.str_]

IndexType = Union[slice, np_ndarray_int64, Index, list[int], Series[int]]
MaskType = Union[Series[bool], np_ndarray_bool, list[bool]]
np_ndarray_int64: TypeAlias = npt.NDArray[np.int64]
np_ndarray_int: TypeAlias = npt.NDArray[np.signedinteger]
np_ndarray_anyint: TypeAlias = npt.NDArray[np.integer]
np_ndarray_bool: TypeAlias = npt.NDArray[np.bool_]
np_ndarray_str: TypeAlias = npt.NDArray[np.str_]

IndexType: TypeAlias = Union[slice, np_ndarray_int64, Index, list[int], Series[int]]
MaskType: TypeAlias = Union[Series[bool], np_ndarray_bool, list[bool]]
# Scratch types for generics
S1 = TypeVar(
"S1",
Expand All @@ -177,13 +176,13 @@ T1 = TypeVar(
)
T2 = TypeVar("T2", str, int)

IndexingInt = Union[
IndexingInt: TypeAlias = Union[
int, np.int_, np.integer, np.unsignedinteger, np.signedinteger, np.int8
]
TimestampConvertibleTypes = Union[
TimestampConvertibleTypes: TypeAlias = Union[
Timestamp, datetime.datetime, np.datetime64, np.int64, float, str
]
TimedeltaConvertibleTypes = Union[
TimedeltaConvertibleTypes: TypeAlias = Union[
Timedelta, datetime.timedelta, np.timedelta64, np.int64, float, str
]
# NDFrameT is stricter and ensures that the same subclass of NDFrame always is
Expand All @@ -196,39 +195,39 @@ IndexT = TypeVar("IndexT", bound=Index)

# Interval closed type

IntervalClosedType = Literal["left", "right", "both", "neither"]
IntervalClosedType: TypeAlias = Literal["left", "right", "both", "neither"]

DateTimeErrorChoices = Literal["ignore", "raise", "coerce"]
DateTimeErrorChoices: TypeAlias = Literal["ignore", "raise", "coerce"]

# Shared by functions such as drop and astype
IgnoreRaise = Literal["ignore", "raise"]
IgnoreRaise: TypeAlias = Literal["ignore", "raise"]

# for arbitrary kwargs passed during reading/writing files
StorageOptions = Optional[dict[str, Any]]
StorageOptions: TypeAlias = Optional[dict[str, Any]]

# compression keywords and compression
CompressionDict = dict[str, Any]
CompressionOptions = Optional[
CompressionDict: TypeAlias = dict[str, Any]
CompressionOptions: TypeAlias = Optional[
Union[Literal["infer", "gzip", "bz2", "zip", "xz", "zstd"], CompressionDict]
]
FormattersType = Union[
FormattersType: TypeAlias = Union[
list[Callable], tuple[Callable, ...], Mapping[Union[str, int], Callable]
]
FloatFormatType = str | Callable | EngFormatter
FloatFormatType: TypeAlias = str | Callable | EngFormatter
# converters
ConvertersArg = dict[Hashable, Callable[[Dtype], Dtype]]
ConvertersArg: TypeAlias = dict[Hashable, Callable[[Dtype], Dtype]]

# parse_dates
ParseDatesArg = Union[
ParseDatesArg: TypeAlias = Union[
bool, list[Hashable], list[list[Hashable]], dict[Hashable, list[Hashable]]
]

# read_xml parsers
XMLParsers = Literal["lxml", "etree"]
XMLParsers: TypeAlias = Literal["lxml", "etree"]

# Any plain Python or numpy function
Function = Union[np.ufunc, Callable[..., Any]]
GroupByObjectNonScalar = Union[
Function: TypeAlias = Union[np.ufunc, Callable[..., Any]]
GroupByObjectNonScalar: TypeAlias = Union[
tuple,
list[HashableT],
Function,
Expand All @@ -244,9 +243,9 @@ GroupByObjectNonScalar = Union[
Grouper,
list[Grouper],
]
GroupByObject = Union[Scalar, GroupByObjectNonScalar]
GroupByObject: TypeAlias = Union[Scalar, GroupByObjectNonScalar]

StataDateFormat = Literal[
StataDateFormat: TypeAlias = Literal[
"tc",
"%tc",
"td",
Expand All @@ -263,37 +262,43 @@ StataDateFormat = Literal[
"%ty",
]

FillnaOptions = Literal["backfill", "bfill", "ffill", "pad"]
ReplaceMethod = Literal["pad", "ffill", "bfill"]
SortKind = Literal["quicksort", "mergesort", "heapsort", "stable"]
NaPosition = Literal["first", "last"]
MergeHow = Literal["left", "right", "outer", "inner"]
JsonFrameOrient = Literal["split", "records", "index", "columns", "values", "table"]
JsonSeriesOrient = Literal["split", "records", "index"]
FillnaOptions: TypeAlias = Literal["backfill", "bfill", "ffill", "pad"]
ReplaceMethod: TypeAlias = Literal["pad", "ffill", "bfill"]
SortKind: TypeAlias = Literal["quicksort", "mergesort", "heapsort", "stable"]
NaPosition: TypeAlias = Literal["first", "last"]
MergeHow: TypeAlias = Literal["left", "right", "outer", "inner"]
JsonFrameOrient: TypeAlias = Literal[
"split", "records", "index", "columns", "values", "table"
]
JsonSeriesOrient: TypeAlias = Literal["split", "records", "index"]

TimestampConvention = Literal["start", "end", "s", "e"]
TimestampConvention: TypeAlias = Literal["start", "end", "s", "e"]

CSVEngine = Literal["c", "python", "pyarrow", "python-fwf"]
CSVQuoting = Literal[0, 1, 2, 3]
CSVEngine: TypeAlias = Literal["c", "python", "pyarrow", "python-fwf"]
CSVQuoting: TypeAlias = Literal[0, 1, 2, 3]

HDFCompLib = Literal["zlib", "lzo", "bzip2", "blosc"]
ParquetEngine = Literal["auto", "pyarrow", "fastparquet"]
FileWriteMode = Literal[
HDFCompLib: TypeAlias = Literal["zlib", "lzo", "bzip2", "blosc"]
ParquetEngine: TypeAlias = Literal["auto", "pyarrow", "fastparquet"]
FileWriteMode: TypeAlias = Literal[
"a", "w", "x", "at", "wt", "xt", "ab", "wb", "xb", "w+", "w+b", "a+", "a+b"
]
ColspaceArgType = str | int | Sequence[int | str] | Mapping[Hashable, str | int]
ColspaceArgType: TypeAlias = (
str | int | Sequence[int | str] | Mapping[Hashable, str | int]
)

# Windowing rank methods
WindowingRankType = Literal["average", "min", "max"]
WindowingEngine = Union[Literal["cython", "numba"], None]
WindowingRankType: TypeAlias = Literal["average", "min", "max"]
WindowingEngine: TypeAlias = Union[Literal["cython", "numba"], None]

class _WindowingNumbaKwargs(TypedDict, total=False):
nopython: bool
nogil: bool
parallel: bool

WindowingEngineKwargs = Union[_WindowingNumbaKwargs, None]
QuantileInterpolation = Literal["linear", "lower", "higher", "midpoint", "nearest"]
WindowingEngineKwargs: TypeAlias = Union[_WindowingNumbaKwargs, None]
QuantileInterpolation: TypeAlias = Literal[
"linear", "lower", "higher", "midpoint", "nearest"
]

class StyleExportDict(TypedDict, total=False):
apply: Any
Expand All @@ -305,6 +310,6 @@ class StyleExportDict(TypedDict, total=False):
hide_column_names: bool
css: dict[str, str | int]

CalculationMethod = Literal["single", "table"]
CalculationMethod: TypeAlias = Literal["single", "table"]

__all__ = ["npt", "type_t"]
2 changes: 1 addition & 1 deletion pandas-stubs/_version.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ version_json: str = ...

def get_versions(): ...

_stub_version = Literal["1.5.0.220926"]
_stub_version: Literal["1.5.0.220926"]
Copy link
Member Author

Choose a reason for hiding this comment

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

Not sure why this is needed.

If we assign Literal as a value, we need a TypeAlias, but the type alias is unused (private type aliases need to be used) which triggers another check from flake8-pyi.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Microsoft is copying the stubs over to the VS Code pylance distribution, so by having this, we can tell what version they copied over.

3 changes: 2 additions & 1 deletion pandas-stubs/core/dtypes/common.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ from typing import Union

import numpy as np
import pandas as pd
from typing_extensions import TypeAlias

from pandas._typing import (
ArrayLike,
Expand All @@ -28,7 +29,7 @@ from pandas.core.dtypes.inference import (
is_scalar as is_scalar,
)

_ArrayOrDtype = Union[ArrayLike, npt.DTypeLike, pd.Series, pd.DataFrame]
_ArrayOrDtype: TypeAlias = Union[ArrayLike, npt.DTypeLike, pd.Series, pd.DataFrame]

def is_object_dtype(arr_or_dtype: _ArrayOrDtype) -> bool: ...
def is_sparse(arr: ArrayLike | pd.Series | pd.DataFrame) -> bool: ...
Expand Down
5 changes: 3 additions & 2 deletions pandas-stubs/core/dtypes/generic.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from pandas import Series
from pandas.core.arrays import ExtensionArray
from typing_extensions import TypeAlias

ABCSeries = type[Series]
ABCExtensionArray = type[ExtensionArray]
ABCSeries: TypeAlias = type[Series]
ABCExtensionArray: TypeAlias = type[ExtensionArray]
Loading