Skip to content

TYP: use from __future__ import annotations more - batch 5 #41898

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
Jun 9, 2021
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
9 changes: 4 additions & 5 deletions pandas/core/array_algos/putmask.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
"""
EA-compatible analogue to to np.putmask
"""
from typing import (
Any,
Tuple,
)
from __future__ import annotations

from typing import Any
import warnings

import numpy as np
Expand Down Expand Up @@ -171,7 +170,7 @@ def putmask_without_repeat(values: np.ndarray, mask: np.ndarray, new: Any) -> No
np.putmask(values, mask, new)


def validate_putmask(values: ArrayLike, mask: np.ndarray) -> Tuple[np.ndarray, bool]:
def validate_putmask(values: ArrayLike, mask: np.ndarray) -> tuple[np.ndarray, bool]:
"""
Validate mask and check if this putmask operation is a no-op.
"""
Expand Down
7 changes: 3 additions & 4 deletions pandas/core/arrays/_ranges.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
Helper functions to generate range-like data for DatetimeArray
(and possibly TimedeltaArray/PeriodArray)
"""

from typing import Union
from __future__ import annotations

import numpy as np

Expand All @@ -17,8 +16,8 @@


def generate_regular_range(
start: Union[Timestamp, Timedelta],
end: Union[Timestamp, Timedelta],
start: Timestamp | Timedelta,
end: Timestamp | Timedelta,
periods: int,
freq: BaseOffset,
):
Expand Down
7 changes: 2 additions & 5 deletions pandas/core/computation/engines.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
"""
Engine classes for :func:`~pandas.eval`
"""
from __future__ import annotations

import abc
from typing import (
Dict,
Type,
)

from pandas.core.computation.align import (
align_terms,
Expand Down Expand Up @@ -140,7 +137,7 @@ def _evaluate(self) -> None:
pass


ENGINES: Dict[str, Type[AbstractEngine]] = {
ENGINES: dict[str, type[AbstractEngine]] = {
"numexpr": NumExprEngine,
"python": PythonEngine,
}
8 changes: 4 additions & 4 deletions pandas/core/computation/parsing.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
:func:`~pandas.eval` source string parsing functions
"""
from __future__ import annotations

from io import StringIO
from keyword import iskeyword
Expand All @@ -9,7 +10,6 @@
from typing import (
Hashable,
Iterator,
Tuple,
)

# A token value Python's tokenizer probably will never use.
Expand Down Expand Up @@ -66,7 +66,7 @@ def create_valid_python_identifier(name: str) -> str:
return name


def clean_backtick_quoted_toks(tok: Tuple[int, str]) -> Tuple[int, str]:
def clean_backtick_quoted_toks(tok: tuple[int, str]) -> tuple[int, str]:
"""
Clean up a column name if surrounded by backticks.

Expand Down Expand Up @@ -131,7 +131,7 @@ def clean_column_name(name: Hashable) -> Hashable:

def tokenize_backtick_quoted_string(
token_generator: Iterator[tokenize.TokenInfo], source: str, string_start: int
) -> Tuple[int, str]:
) -> tuple[int, str]:
"""
Creates a token from a backtick quoted string.

Expand Down Expand Up @@ -163,7 +163,7 @@ def tokenize_backtick_quoted_string(
return BACKTICK_QUOTED_STRING, source[string_start:string_end]


def tokenize_string(source: str) -> Iterator[Tuple[int, str]]:
def tokenize_string(source: str) -> Iterator[tuple[int, str]]:
"""
Tokenize a Python source code string.

Expand Down
12 changes: 5 additions & 7 deletions pandas/core/indexes/api.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from __future__ import annotations

import textwrap
from typing import (
List,
Set,
)

from pandas._libs import (
NaT,
Expand Down Expand Up @@ -98,12 +96,12 @@ def get_objs_combined_axis(
return _get_combined_index(obs_idxes, intersect=intersect, sort=sort, copy=copy)


def _get_distinct_objs(objs: List[Index]) -> List[Index]:
def _get_distinct_objs(objs: list[Index]) -> list[Index]:
"""
Return a list with distinct elements of "objs" (different ids).
Preserves order.
"""
ids: Set[int] = set()
ids: set[int] = set()
res = []
for obj in objs:
if id(obj) not in ids:
Expand All @@ -113,7 +111,7 @@ def _get_distinct_objs(objs: List[Index]) -> List[Index]:


def _get_combined_index(
indexes: List[Index],
indexes: list[Index],
intersect: bool = False,
sort: bool = False,
copy: bool = False,
Expand Down
25 changes: 12 additions & 13 deletions pandas/core/indexes/extension.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
"""
Shared methods for Index subclasses backed by ExtensionArray.
"""
from __future__ import annotations

from typing import (
Hashable,
List,
Type,
TypeVar,
Union,
)

import numpy as np
Expand Down Expand Up @@ -117,7 +116,7 @@ def method(self, *args, **kwargs):
return method


def inherit_names(names: List[str], delegate, cache: bool = False, wrap: bool = False):
def inherit_names(names: list[str], delegate, cache: bool = False, wrap: bool = False):
"""
Class decorator to pin attributes from an ExtensionArray to a Index subclass.

Expand Down Expand Up @@ -227,20 +226,20 @@ class ExtensionIndex(Index):
# The base class already passes through to _data:
# size, __len__, dtype

_data: Union[IntervalArray, NDArrayBackedExtensionArray]
_data: IntervalArray | NDArrayBackedExtensionArray

_data_cls: Union[
Type[Categorical],
Type[DatetimeArray],
Type[TimedeltaArray],
Type[PeriodArray],
Type[IntervalArray],
]
_data_cls: (
type[Categorical]
| type[DatetimeArray]
| type[TimedeltaArray]
| type[PeriodArray]
| type[IntervalArray]
)

@classmethod
def _simple_new(
cls,
array: Union[IntervalArray, NDArrayBackedExtensionArray],
array: IntervalArray | NDArrayBackedExtensionArray,
name: Hashable = None,
):
"""
Expand Down
15 changes: 8 additions & 7 deletions pandas/tests/api/test_api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import subprocess
import sys
from typing import List

import pytest

Expand Down Expand Up @@ -46,7 +47,7 @@ class TestPDApi(Base):
]

# these are already deprecated; awaiting removal
deprecated_modules: List[str] = ["np", "datetime"]
deprecated_modules: list[str] = ["np", "datetime"]

# misc
misc = ["IndexSlice", "NaT", "NA"]
Expand Down Expand Up @@ -98,13 +99,13 @@ class TestPDApi(Base):
]

# these are already deprecated; awaiting removal
deprecated_classes: List[str] = []
deprecated_classes: list[str] = []

# these should be deprecated in the future
deprecated_classes_in_future: List[str] = ["SparseArray"]
deprecated_classes_in_future: list[str] = ["SparseArray"]

# external modules exposed in pandas namespace
modules: List[str] = []
modules: list[str] = []

# top-level functions
funcs = [
Expand Down Expand Up @@ -181,10 +182,10 @@ class TestPDApi(Base):
funcs_to = ["to_datetime", "to_numeric", "to_pickle", "to_timedelta"]

# top-level to deprecate in the future
deprecated_funcs_in_future: List[str] = []
deprecated_funcs_in_future: list[str] = []

# these are already deprecated; awaiting removal
deprecated_funcs: List[str] = []
deprecated_funcs: list[str] = []

# private modules in pandas namespace
private_modules = [
Expand Down
11 changes: 5 additions & 6 deletions pandas/tests/arithmetic/test_numeric.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
# Arithmetic tests for DataFrame/Series/Index/Array classes that should
# behave identically.
# Specifically for numeric dtypes
from __future__ import annotations

from collections import abc
from decimal import Decimal
from itertools import combinations
import operator
from typing import (
Any,
List,
)
from typing import Any

import numpy as np
import pytest
Expand Down Expand Up @@ -56,8 +55,8 @@ def adjust_negative_zero(zero, expected):
# TODO: remove this kludge once mypy stops giving false positives here
# List comprehension has incompatible type List[PandasObject]; expected List[RangeIndex]
# See GH#29725
ser_or_index: List[Any] = [Series, Index]
lefts: List[Any] = [RangeIndex(10, 40, 10)]
ser_or_index: list[Any] = [Series, Index]
lefts: list[Any] = [RangeIndex(10, 40, 10)]
lefts.extend(
[
cls([10, 20, 30], dtype=dtype)
Expand Down
9 changes: 4 additions & 5 deletions pandas/tests/arrays/masked/test_arithmetic.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from typing import (
Any,
List,
)
from __future__ import annotations

from typing import Any

import numpy as np
import pytest
Expand All @@ -12,7 +11,7 @@

# integer dtypes
arrays = [pd.array([1, 2, 3, None], dtype=dtype) for dtype in tm.ALL_EA_INT_DTYPES]
scalars: List[Any] = [2] * len(arrays)
scalars: list[Any] = [2] * len(arrays)
# floating dtypes
arrays += [pd.array([0.1, 0.2, 0.3, None], dtype=dtype) for dtype in tm.FLOAT_EA_DTYPES]
scalars += [0.2, 0.2]
Expand Down
19 changes: 8 additions & 11 deletions pandas/tests/computation/test_eval.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
from __future__ import annotations

from functools import reduce
from itertools import product
import operator
from typing import (
Dict,
List,
Type,
)
import warnings

import numpy as np
Expand Down Expand Up @@ -147,8 +144,8 @@ def lhs(request):

@td.skip_if_no_ne
class TestEvalNumexprPandas:
exclude_cmp: List[str] = []
exclude_bool: List[str] = []
exclude_cmp: list[str] = []
exclude_bool: list[str] = []

engine = "numexpr"
parser = "pandas"
Expand Down Expand Up @@ -1125,7 +1122,7 @@ def test_performance_warning_for_poor_alignment(self, engine, parser):

@td.skip_if_no_ne
class TestOperationsNumExprPandas:
exclude_arith: List[str] = []
exclude_arith: list[str] = []

engine = "numexpr"
parser = "pandas"
Expand Down Expand Up @@ -1629,7 +1626,7 @@ def test_simple_in_ops(self):

@td.skip_if_no_ne
class TestOperationsNumExprPython(TestOperationsNumExprPandas):
exclude_arith: List[str] = ["in", "not in"]
exclude_arith: list[str] = ["in", "not in"]

engine = "numexpr"
parser = "python"
Expand Down Expand Up @@ -1723,7 +1720,7 @@ class TestOperationsPythonPython(TestOperationsNumExprPython):


class TestOperationsPythonPandas(TestOperationsNumExprPandas):
exclude_arith: List[str] = []
exclude_arith: list[str] = []

engine = "python"
parser = "pandas"
Expand Down Expand Up @@ -1878,7 +1875,7 @@ def test_invalid_parser():
pd.eval("x + y", local_dict={"x": 1, "y": 2}, parser="asdf")


_parsers: Dict[str, Type[BaseExprVisitor]] = {
_parsers: dict[str, type[BaseExprVisitor]] = {
"python": PythonExprVisitor,
"pytables": pytables.PyTablesExprVisitor,
"pandas": PandasExprVisitor,
Expand Down