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

Added TypeAlias #52452

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
10 changes: 5 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ repos:
types_or: [python, pyi]
additional_dependencies: [black==23.1.0]
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.0.259
rev: v0.0.261
hooks:
- id: ruff
args: [--exit-non-zero-on-fix]
Expand All @@ -40,13 +40,13 @@ repos:
pass_filenames: true
require_serial: false
- repo: https://github.com/codespell-project/codespell
rev: v2.2.2
rev: v2.2.4
hooks:
- id: codespell
types_or: [python, rst, markdown, cython, c]
additional_dependencies: [tomli]
- repo: https://github.com/MarcoGorelli/cython-lint
rev: v0.12.5
rev: v0.15.0
hooks:
- id: cython-lint
- id: double-quote-cython-strings
Expand Down Expand Up @@ -79,12 +79,12 @@ repos:
'--filter=-readability/casting,-runtime/int,-build/include_subdir,-readability/fn_size'
]
- repo: https://github.com/pycqa/pylint
rev: v2.16.2
rev: v3.0.0a6
hooks:
- id: pylint
stages: [manual]
- repo: https://github.com/pycqa/pylint
rev: v2.16.2
rev: v3.0.0a6
hooks:
- id: pylint
alias: redefined-outer-name
Expand Down
7 changes: 4 additions & 3 deletions pandas/_libs/lib.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ from typing import (
)

import numpy as np
from typing_extensions import TypeAlias

from pandas._libs.interval import Interval
from pandas._libs.tslibs import Period
Expand All @@ -23,15 +24,15 @@ from pandas._typing import (
)

# placeholder until we can specify np.ndarray[object, ndim=2]
ndarray_obj_2d = np.ndarray
ndarray_obj_2d: TypeAlias = np.ndarray

from enum import Enum

class _NoDefault(Enum):
no_default = ...

no_default: Final = _NoDefault.no_default
NoDefault = Literal[_NoDefault.no_default]
no_default: Final[TypeAlias] = ...
NoDefault: TypeAlias = Literal[_NoDefault.no_default]

i8max: int
u8max: int
Expand Down
5 changes: 3 additions & 2 deletions pandas/_libs/ops.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ from typing import (
)

import numpy as np
from typing_extensions import TypeAlias

from pandas._typing import npt

_BinOp = Callable[[Any, Any], Any]
_BoolOp = Callable[[Any, Any], bool]
_BinOp: TypeAlias = Callable[[Any, Any], Any]
_BoolOp: TypeAlias = Callable[[Any, Any], bool]

def scalar_compare(
values: np.ndarray, # object[:]
Expand Down
4 changes: 3 additions & 1 deletion pandas/_libs/properties.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ from typing import (
overload,
)

from typing_extensions import TypeAlias

from pandas._typing import (
AnyArrayLike,
DataFrame,
Expand All @@ -13,7 +15,7 @@ from pandas._typing import (
# note: this is a lie to make type checkers happy (they special
# case property). cache_readonly uses attribute names similar to
# property (fget) but it does not provide fset and fdel.
cache_readonly = property
cache_readonly: TypeAlias = property

class AxisProperty:
axis: int
Expand Down
5 changes: 4 additions & 1 deletion pandas/_libs/tslibs/nattype.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ from datetime import (
)

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 = datetime | timedelta | Period | np.datetime64 | np.timedelta64
_NaTComparisonTypes: TypeAlias = (
datetime | timedelta | Period | np.datetime64 | np.timedelta64
)

class _NatComparison:
def __call__(self, other: _NaTComparisonTypes) -> bool: ...
Expand Down
13 changes: 7 additions & 6 deletions pandas/_libs/tslibs/offsets.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ from typing import (
)

import numpy as np
from typing_extensions import TypeAlias

from pandas._libs.tslibs.nattype import NaTType
from pandas._typing import (
Expand Down Expand Up @@ -262,12 +263,12 @@ class CustomBusinessMonthBegin(_CustomBusinessMonth): ...
class OffsetMeta(type): ...
class DateOffset(RelativeDeltaOffset, metaclass=OffsetMeta): ...

BDay = BusinessDay
BMonthEnd = BusinessMonthEnd
BMonthBegin = BusinessMonthBegin
CBMonthEnd = CustomBusinessMonthEnd
CBMonthBegin = CustomBusinessMonthBegin
CDay = CustomBusinessDay
BDay: TypeAlias = BusinessDay
BMonthEnd: TypeAlias = BusinessMonthEnd
BMonthBegin: TypeAlias = BusinessMonthBegin
CBMonthEnd: TypeAlias = CustomBusinessMonthEnd
CBMonthBegin: TypeAlias = CustomBusinessMonthBegin
CDay: TypeAlias = CustomBusinessDay

def roll_qtrday(
other: datetime, n: int, month: int, day_opt: str, modby: int
Expand Down
3 changes: 2 additions & 1 deletion pandas/_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 @@ -19,7 +20,7 @@ from pandas._typing import (

# 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
4 changes: 3 additions & 1 deletion pandas/tests/io/excel/test_readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,9 @@ def test_bad_sheetname_raises(self, read_ext, sheet_name):
def test_missing_file_raises(self, read_ext):
bad_file = f"foo{read_ext}"
# CI tests with other languages, translates to "No such file or directory"
match = r"(No such file or directory|没有那个文件或目录|File o directory non esistente)"
match = (
r"(No such file or directory|没有那个文件或目录|" r"File o directory non esistente)"
)
with pytest.raises(FileNotFoundError, match=match):
pd.read_excel(bad_file)

Expand Down
10 changes: 7 additions & 3 deletions pandas/tests/io/formats/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -2205,18 +2205,22 @@ def test_east_asian_unicode_series(self):

# unicode index
s = Series(["a", "bb", "CCC", "D"], index=["あ", "いい", "ううう", "ええええ"])
expected = "あ a\nいい bb\nううう CCC\nええええ D\ndtype: object"
expected = (
"あ a\nいい bb\nううう" " CCC\nええええ D\ndtype: object"
)
assert repr(s) == expected

# unicode values
s = Series(["あ", "いい", "ううう", "ええええ"], index=["a", "bb", "c", "ddd"])
expected = "a あ\nbb いい\nc ううう\nddd ええええ\ndtype: object"
expected = (
"a あ\nbb いい\nc " "ううう\nddd ええええ\ndtype: object"
)
assert repr(s) == expected

# both
s = Series(["あ", "いい", "ううう", "ええええ"], index=["ああ", "いいいい", "う", "えええ"])
expected = (
"ああ あ\nいいいい いい\nう ううう\nえええ ええええ\ndtype: object"
"ああ あ\nいいいい いい\nう " "ううう\nえええ ええええ\ndtype: object"
)
assert repr(s) == expected

Expand Down
8 changes: 6 additions & 2 deletions pandas/tests/io/xml/test_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,11 +454,15 @@ def test_file_charset(datapath, parser):
"問 既破性申假 亦應但破有申無 若有無兩洗 亦應性假雙破耶",
],
"答": [
"答 邪既無量 正亦多途 大略為言不出二種 謂有得與無得 有得是邪須破 無得是正須申\n\t\t故",
("答 邪既無量 正亦多途 大略為言不出二種 " "謂有得與無得 有得是邪須破 無得是正須申\n\t\t故"),
None,
"答 不例 有無皆是性 所以須雙破 既分性假異 故有破不破",
],
"a": [None, "答 性執是有得 假名是無得 今破有得申無得 即是破性執申假名也", None],
"a": [
None,
"答 性執是有得 假名是無得 今破有得申無得 即是破性執申假名也",
None,
],
}
)

Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,8 @@ ignore = [
"PLR2004",
# Consider `elif` instead of `else` then `if` to remove indentation level
"PLR5501",
# Useless statement
"B018",
]

exclude = [
Expand Down
4 changes: 2 additions & 2 deletions typings/numba.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ def jit(
parallel: bool = ...,
) -> Callable[[F], F]: ...

njit = jit
generated_jit = jit
njit = ...
generated_jit = ...
Copy link
Member

Choose a reason for hiding this comment

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

I'm quite sure we want to keep those? can they be a TypeAlias?

Copy link
Contributor

Choose a reason for hiding this comment

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

Sure they can. It seems that TypeAlias is imported from the wrong library. It should be imported from typing_extensions.