Skip to content

TYP: Implicit generic "Any" for builtins #30541

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 13 commits into from
Dec 31, 2019
Merged
Show file tree
Hide file tree
Changes from 12 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
18 changes: 13 additions & 5 deletions pandas/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,29 @@
from pandas.core.indexes.base import Index # noqa: F401
from pandas.core.series import Series # noqa: F401
from pandas.core.generic import NDFrame # noqa: F401
from pandas import Interval # noqa: F401

# array-like

AnyArrayLike = TypeVar("AnyArrayLike", "ExtensionArray", "Index", "Series", np.ndarray)
ArrayLike = TypeVar("ArrayLike", "ExtensionArray", np.ndarray)

# scalars

PythonScalar = Union[str, int, float, bool]
Copy link
Contributor

Choose a reason for hiding this comment

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

would move DatetimeLikeScalar closer to these (also would add some comments about various sections, e.g. these are Scalars )

DatetimeLikeScalar = TypeVar("DatetimeLikeScalar", "Period", "Timestamp", "Timedelta")
PandasScalar = Union[DatetimeLikeScalar, "Interval"]
Scalar = Union[PythonScalar, PandasScalar]
Copy link
Member

Choose a reason for hiding this comment

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

Does it matter that this is a Union of a Union and a TypeVar? Maybe DatetimeLikeScalar should just be a Union?

Copy link
Member Author

Choose a reason for hiding this comment

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

yes, we have the same issue as with FilePathOrBuffer, where IO is unbound in the Union and requires the addition of type parameters when using the alias.

and we don't really want that for Scalar. but we had Scalar as a union (for JSONSerializable) and DatetimeLikeScalar as a TypeVar.

Maybe DatetimeLikeScalar should just be a Union?

rather than change this, maybe should define PandasScalar = Union["Period", "Timestamp", "Timedelta", "Interval"] for now

Copy link
Member

Choose a reason for hiding this comment

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

Yea makes sense


# other

Dtype = Union[str, np.dtype, "ExtensionDtype"]
FilePathOrBuffer = Union[str, Path, IO[AnyStr]]

FrameOrSeries = TypeVar("FrameOrSeries", bound="NDFrame")
Scalar = Union[str, int, float, bool]
Axis = Union[str, int]
Ordered = Optional[bool]
JSONSerializable = Union[Scalar, List, Dict]

JSONSerializable = Union[PythonScalar, List, Dict]
Axes = Collection

# to maintain type information across generic functions and parametrization
_T = TypeVar("_T")
T = TypeVar("T")
12 changes: 6 additions & 6 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import operator
from shutil import get_terminal_size
from typing import Type, Union, cast
from typing import Dict, Hashable, List, Type, Union, cast
from warnings import warn

import numpy as np

from pandas._config import get_option

from pandas._libs import algos as libalgos, hashtable as htable
from pandas._typing import ArrayLike, Dtype, Ordered
from pandas._typing import ArrayLike, Dtype, Ordered, Scalar
from pandas.compat.numpy import function as nv
from pandas.util._decorators import (
Appender,
Expand Down Expand Up @@ -511,7 +511,7 @@ def itemsize(self) -> int:
"""
return self.categories.itemsize

def tolist(self) -> list:
def tolist(self) -> List[Scalar]:
"""
Return a list of the values.

Expand Down Expand Up @@ -2067,7 +2067,7 @@ def __setitem__(self, key, value):
lindexer = self._maybe_coerce_indexer(lindexer)
self._codes[key] = lindexer

def _reverse_indexer(self):
def _reverse_indexer(self) -> Dict[Hashable, np.ndarray]:
"""
Compute the inverse of a categorical, returning
a dict of categories -> indexers.
Expand Down Expand Up @@ -2097,8 +2097,8 @@ def _reverse_indexer(self):
self.codes.astype("int64"), categories.size
)
counts = counts.cumsum()
result = (r[start:end] for start, end in zip(counts, counts[1:]))
result = dict(zip(categories, result))
_result = (r[start:end] for start, end in zip(counts, counts[1:]))
result = dict(zip(categories, _result))
return result

# reduction ops #
Expand Down
5 changes: 3 additions & 2 deletions pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
from datetime import datetime, timedelta
from functools import partial
import inspect
from typing import Any, Iterable, Union
from typing import Any, Collection, Iterable, Union

import numpy as np

from pandas._libs import lib, tslibs
from pandas._typing import T

from pandas.core.dtypes.cast import construct_1d_object_array_from_listlike
from pandas.core.dtypes.common import (
Expand Down Expand Up @@ -270,7 +271,7 @@ def maybe_make_list(obj):
return obj


def maybe_iterable_to_list(obj: Union[Iterable, Any]) -> Union[list, Any]:
def maybe_iterable_to_list(obj: Union[Iterable[T], T]) -> Union[Collection[T], T]:
"""
If obj is Iterable but not list-like, consume into list.
"""
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/groupby/grouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
split-apply-combine paradigm.
"""

from typing import Hashable, List, Optional, Tuple
from typing import Dict, Hashable, List, Optional, Tuple

import numpy as np

Expand Down Expand Up @@ -419,7 +419,7 @@ def _make_codes(self) -> None:
self._group_index = uniques

@cache_readonly
def groups(self) -> dict:
def groups(self) -> Dict[Hashable, np.ndarray]:
return self.index.groupby(Categorical.from_codes(self.codes, self.group_index))


Expand Down
6 changes: 3 additions & 3 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from datetime import datetime
import operator
from textwrap import dedent
from typing import FrozenSet, Hashable, Optional, Union
from typing import Dict, FrozenSet, Hashable, Optional, Union
import warnings

import numpy as np
Expand Down Expand Up @@ -4594,7 +4594,7 @@ def _maybe_promote(self, other):
return self.astype("object"), other.astype("object")
return self, other

def groupby(self, values):
def groupby(self, values) -> Dict[Hashable, np.ndarray]:
"""
Group the index labels by a given array of values.

Expand All @@ -4605,7 +4605,7 @@ def groupby(self, values):

Returns
-------
groups : dict
dict
{group name -> group labels}
"""

Expand Down
11 changes: 6 additions & 5 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Tuple
from typing import Hashable, List, Tuple, Union

import numpy as np

Expand Down Expand Up @@ -2224,7 +2224,7 @@ def _convert_key(self, key, is_setter: bool = False):
return key


def _tuplify(ndim: int, loc) -> tuple:
def _tuplify(ndim: int, loc: Hashable) -> Tuple[Union[Hashable, slice], ...]:
"""
Given an indexer for the first dimension, create an equivalent tuple
for indexing over all dimensions.
Expand All @@ -2238,9 +2238,10 @@ def _tuplify(ndim: int, loc) -> tuple:
-------
tuple
"""
tup = [slice(None, None) for _ in range(ndim)]
tup[0] = loc
return tuple(tup)
_tup: List[Union[Hashable, slice]]
_tup = [slice(None, None) for _ in range(ndim)]
_tup[0] = loc
return tuple(_tup)


def convert_to_index_sliceable(obj, key):
Expand Down
2 changes: 1 addition & 1 deletion pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -1459,7 +1459,7 @@ def copy(
data = self.select(k)
if isinstance(s, Table):

index: Union[bool, list] = False
index: Union[bool, List[str]] = False
if propindexes:
index = [a.name for a in s.axes if a.is_indexed]
new_store.append(
Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/frame/methods/test_replace.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from datetime import datetime
from io import StringIO
import re
from typing import Dict
from typing import Dict, List, Union

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


@pytest.fixture
def mix_ab() -> Dict[str, list]:
def mix_ab() -> Dict[str, List[Union[int, str]]]:
return {"a": list(range(4)), "b": list("ab..")}


@pytest.fixture
def mix_abc() -> Dict[str, list]:
def mix_abc() -> Dict[str, List[Union[float, str]]]:
return {"a": list(range(4)), "b": list("ab.."), "c": ["a", "b", np.nan, "d"]}


Expand Down