Skip to content

TYP: type all arguments with str default values #48508

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
Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from 8 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 @@ -258,7 +258,7 @@ repos:
|/_testing/
- id: autotyping
name: autotyping
entry: python -m libcst.tool codemod autotyping.AutotypeCommand --none-return --scalar-return --annotate-magics --annotate-imprecise-magics
entry: python -m libcst.tool codemod autotyping.AutotypeCommand --none-return --scalar-return --annotate-magics --annotate-imprecise-magics --bytes-param --str-param --float-param
types_or: [python, pyi]
files: ^pandas
exclude: ^(pandas/tests|pandas/io/clipboard)
Expand Down
25 changes: 16 additions & 9 deletions pandas/_testing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@
set_locale,
)

from pandas._typing import Dtype
from pandas._typing import (
Dtype,
Frequency,
)
from pandas.compat import pa_version_under1p01

from pandas.core.dtypes.common import (
Expand Down Expand Up @@ -401,13 +404,17 @@ def makeFloatIndex(k=10, name=None) -> Float64Index:
return Float64Index(base_idx)


def makeDateIndex(k: int = 10, freq="B", name=None, **kwargs) -> DatetimeIndex:
def makeDateIndex(
k: int = 10, freq: Frequency = "B", name=None, **kwargs
) -> DatetimeIndex:
dt = datetime(2000, 1, 1)
dr = bdate_range(dt, periods=k, freq=freq, name=name)
return DatetimeIndex(dr, name=name, **kwargs)


def makeTimedeltaIndex(k: int = 10, freq="D", name=None, **kwargs) -> TimedeltaIndex:
def makeTimedeltaIndex(
k: int = 10, freq: Frequency = "D", name=None, **kwargs
) -> TimedeltaIndex:
return pd.timedelta_range(start="1 day", periods=k, freq=freq, name=name, **kwargs)


Expand Down Expand Up @@ -483,7 +490,7 @@ def getSeriesData() -> dict[str, Series]:
return {c: Series(np.random.randn(_N), index=index) for c in getCols(_K)}


def makeTimeSeries(nper=None, freq="B", name=None) -> Series:
def makeTimeSeries(nper=None, freq: Frequency = "B", name=None) -> Series:
if nper is None:
nper = _N
return Series(
Expand All @@ -497,7 +504,7 @@ def makePeriodSeries(nper=None, name=None) -> Series:
return Series(np.random.randn(nper), index=makePeriodIndex(nper), name=name)


def getTimeSeriesData(nper=None, freq="B") -> dict[str, Series]:
def getTimeSeriesData(nper=None, freq: Frequency = "B") -> dict[str, Series]:
return {c: makeTimeSeries(nper, freq) for c in getCols(_K)}


Expand All @@ -506,7 +513,7 @@ def getPeriodData(nper=None) -> dict[str, Series]:


# make frame
def makeTimeDataFrame(nper=None, freq="B") -> DataFrame:
def makeTimeDataFrame(nper=None, freq: Frequency = "B") -> DataFrame:
data = getTimeSeriesData(nper, freq)
return DataFrame(data)

Expand Down Expand Up @@ -541,7 +548,7 @@ def makePeriodFrame(nper=None) -> DataFrame:
def makeCustomIndex(
nentries,
nlevels,
prefix="#",
prefix: str = "#",
names: bool | str | list[str] | None = False,
ndupe_l=None,
idx_type=None,
Expand Down Expand Up @@ -759,7 +766,7 @@ def makeCustomDataframe(
return DataFrame(data, index, columns, dtype=dtype)


def _create_missing_idx(nrows, ncols, density, random_state=None):
def _create_missing_idx(nrows, ncols, density: float, random_state=None):
if random_state is None:
random_state = np.random
else:
Expand All @@ -786,7 +793,7 @@ def _gen_unique_rand(rng, _extra_size):
return i.tolist(), j.tolist()


def makeMissingDataframe(density=0.9, random_state=None) -> DataFrame:
def makeMissingDataframe(density: float = 0.9, random_state=None) -> DataFrame:
df = makeDataFrame()
i, j = _create_missing_idx(*df.shape, density=density, random_state=random_state)
df.values[i, j] = np.nan
Expand Down
7 changes: 4 additions & 3 deletions pandas/_testing/_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,11 @@ def dec(f):
return wrapper


@optional_args
# error: Untyped decorator makes function "network" untyped
@optional_args # type: ignore[misc]
def network(
t,
url="https://www.google.com",
url: str = "https://www.google.com",
raise_on_error=False,
check_before_test=False,
error_classes=None,
Expand Down Expand Up @@ -368,7 +369,7 @@ def round_trip_localpath(writer, reader, path: str | None = None):
return obj


def write_to_compressed(compression, path, data, dest="test"):
def write_to_compressed(compression, path, data, dest: str = "test"):
"""
Write data to a compressed file.

Expand Down
4 changes: 3 additions & 1 deletion pandas/_testing/_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import numpy as np

from pandas._typing import NpDtype


def randbool(size=(), p: float = 0.5):
return np.random.rand(*size) <= p
Expand All @@ -14,7 +16,7 @@ def randbool(size=(), p: float = 0.5):
)


def rands_array(nchars, size, dtype="O", replace=True) -> np.ndarray:
def rands_array(nchars, size, dtype: NpDtype = "O", replace=True) -> np.ndarray:
"""
Generate an array of byte strings.
"""
Expand Down
32 changes: 17 additions & 15 deletions pandas/_testing/asserters.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ def assert_index_equal(
"""
__tracebackhide__ = True

def _check_types(left, right, obj="Index") -> None:
def _check_types(left, right, obj: str = "Index") -> None:
if not exact:
return

Expand Down Expand Up @@ -429,7 +429,9 @@ def _get_ilevel_values(index, level):
assert_categorical_equal(left._values, right._values, obj=f"{obj} category")


def assert_class_equal(left, right, exact: bool | str = True, obj="Input") -> None:
def assert_class_equal(
left, right, exact: bool | str = True, obj: str = "Input"
) -> None:
"""
Checks classes are equal.
"""
Expand Down Expand Up @@ -523,7 +525,7 @@ def assert_is_sorted(seq) -> None:


def assert_categorical_equal(
left, right, check_dtype=True, check_category_order=True, obj="Categorical"
left, right, check_dtype=True, check_category_order=True, obj: str = "Categorical"
) -> None:
"""
Test that Categoricals are equivalent.
Expand Down Expand Up @@ -580,7 +582,7 @@ def assert_categorical_equal(


def assert_interval_array_equal(
left, right, exact="equiv", obj="IntervalArray"
left, right, exact: bool | Literal["equiv"] = "equiv", obj: str = "IntervalArray"
) -> None:
"""
Test that two IntervalArrays are equivalent.
Expand Down Expand Up @@ -610,15 +612,15 @@ def assert_interval_array_equal(
assert_attr_equal("closed", left, right, obj=obj)


def assert_period_array_equal(left, right, obj="PeriodArray") -> None:
def assert_period_array_equal(left, right, obj: str = "PeriodArray") -> None:
_check_isinstance(left, right, PeriodArray)

assert_numpy_array_equal(left._data, right._data, obj=f"{obj}._data")
assert_attr_equal("freq", left, right, obj=obj)


def assert_datetime_array_equal(
left, right, obj="DatetimeArray", check_freq=True
left, right, obj: str = "DatetimeArray", check_freq=True
) -> None:
__tracebackhide__ = True
_check_isinstance(left, right, DatetimeArray)
Expand All @@ -630,7 +632,7 @@ def assert_datetime_array_equal(


def assert_timedelta_array_equal(
left, right, obj="TimedeltaArray", check_freq=True
left, right, obj: str = "TimedeltaArray", check_freq=True
) -> None:
__tracebackhide__ = True
_check_isinstance(left, right, TimedeltaArray)
Expand Down Expand Up @@ -689,7 +691,7 @@ def assert_numpy_array_equal(
check_dtype: bool | Literal["equiv"] = True,
err_msg=None,
check_same=None,
obj="numpy array",
obj: str = "numpy array",
index_values=None,
) -> None:
"""
Expand Down Expand Up @@ -881,9 +883,9 @@ def assert_series_equal(
check_category_order=True,
check_freq=True,
check_flags=True,
rtol=1.0e-5,
atol=1.0e-8,
obj="Series",
rtol: float = 1.0e-5,
atol: float = 1.0e-8,
obj: str = "Series",
*,
check_index=True,
check_like=False,
Expand Down Expand Up @@ -1140,7 +1142,7 @@ def assert_frame_equal(
right,
check_dtype: bool | Literal["equiv"] = True,
check_index_type: bool | Literal["equiv"] = "equiv",
check_column_type="equiv",
check_column_type: bool | Literal["equiv"] = "equiv",
check_frame_type=True,
check_less_precise=no_default,
check_names=True,
Expand All @@ -1151,9 +1153,9 @@ def assert_frame_equal(
check_like=False,
check_freq=True,
check_flags=True,
rtol=1.0e-5,
atol=1.0e-8,
obj="DataFrame",
rtol: float = 1.0e-5,
atol: float = 1.0e-8,
obj: str = "DataFrame",
) -> None:
"""
Check that left and right DataFrame are equal.
Expand Down
14 changes: 14 additions & 0 deletions pandas/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,3 +331,17 @@ def closed(self) -> bool:

# dropna
AnyAll = Literal["any", "all"]

MatplotlibColor = Union[str, Sequence[float]]
TimeGrouperOrigin = Union[
"Timestamp", Literal["epoch", "start", "start_day", "end", "end_day"]
]
TimeAmbiguous = Union[Literal["infer", "NaT", "raise"], "npt.NDArray[np.bool_]"]
TimeNonexistent = Union[
Literal["shift_forward", "shift_backward", "NaT", "raise"], timedelta
]
DropKeep = Literal["first", "last", False]
CorrelationMethod = Union[
Literal["pearson", "kendall", "spearman"], Callable[[np.ndarray, np.ndarray], float]
]
AlignJoin = Literal["outer", "inner", "left", "right"]
3 changes: 2 additions & 1 deletion pandas/core/arrays/arrow/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from pandas._typing import (
Dtype,
PositionalIndexer,
SortKind,
TakeIndexer,
npt,
)
Expand Down Expand Up @@ -472,7 +473,7 @@ def isna(self) -> npt.NDArray[np.bool_]:
def argsort(
self,
ascending: bool = True,
kind: str = "quicksort",
kind: SortKind = "quicksort",
na_position: str = "last",
*args,
**kwargs,
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/arrays/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
ScalarIndexer,
SequenceIndexer,
Shape,
SortKind,
TakeIndexer,
npt,
)
Expand Down Expand Up @@ -670,7 +671,7 @@ def _values_for_argsort(self) -> np.ndarray:
def argsort(
self,
ascending: bool = True,
kind: str = "quicksort",
kind: SortKind = "quicksort",
na_position: str = "last",
*args,
**kwargs,
Expand Down
12 changes: 9 additions & 3 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
NpDtype,
Ordered,
Shape,
SortKind,
npt,
type_t,
)
Expand Down Expand Up @@ -1821,8 +1822,11 @@ def check_for_ordered(self, op) -> None:
"Categorical to an ordered one\n"
)

# error: Signature of "argsort" incompatible with supertype "ExtensionArray"
@deprecate_nonkeyword_arguments(version=None, allowed_args=["self"])
def argsort(self, ascending=True, kind="quicksort", **kwargs):
def argsort( # type: ignore[override]
self, ascending: bool = True, kind: SortKind = "quicksort", **kwargs
) -> npt.NDArray[np.intp]:
"""
Return the indices that would sort the Categorical.

Expand Down Expand Up @@ -2194,7 +2198,9 @@ def _repr_footer(self) -> str:
info = self._repr_categories_info()
return f"Length: {len(self)}\n{info}"

def _get_repr(self, length: bool = True, na_rep="NaN", footer: bool = True) -> str:
def _get_repr(
self, length: bool = True, na_rep: str = "NaN", footer: bool = True
) -> str:
from pandas.io.formats import format as fmt

formatter = fmt.CategoricalFormatter(
Expand Down Expand Up @@ -2710,7 +2716,7 @@ def _str_map(
result = PandasArray(categories.to_numpy())._str_map(f, na_value, dtype)
return take_nd(result, codes, fill_value=na_value)

def _str_get_dummies(self, sep="|"):
def _str_get_dummies(self, sep: str = "|"):
# sep may not be in categories. Just bail on this.
from pandas.core.arrays import PandasArray

Expand Down
27 changes: 22 additions & 5 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@
PositionalIndexerTuple,
ScalarIndexer,
SequenceIndexer,
TimeAmbiguous,
TimeNonexistent,
npt,
)
from pandas.compat.numpy import function as nv
Expand Down Expand Up @@ -311,7 +313,7 @@ def asi8(self) -> npt.NDArray[np.int64]:
# Rendering Methods

def _format_native_types(
self, *, na_rep="NaT", date_format=None
self, *, na_rep: str | float = "NaT", date_format=None
) -> npt.NDArray[np.object_]:
"""
Helper method for astype when converting to strings.
Expand Down Expand Up @@ -559,7 +561,7 @@ def _concat_same_type(
new_obj._freq = new_freq
return new_obj

def copy(self: DatetimeLikeArrayT, order="C") -> DatetimeLikeArrayT:
def copy(self: DatetimeLikeArrayT, order: str = "C") -> DatetimeLikeArrayT:
# error: Unexpected keyword argument "order" for "copy"
new_obj = super().copy(order=order) # type: ignore[call-arg]
new_obj._freq = self.freq
Expand Down Expand Up @@ -2067,15 +2069,30 @@ def _round(self, freq, mode, ambiguous, nonexistent):
return self._simple_new(result, dtype=self.dtype)

@Appender((_round_doc + _round_example).format(op="round"))
def round(self, freq, ambiguous="raise", nonexistent="raise"):
def round(
self,
freq,
ambiguous: TimeAmbiguous = "raise",
nonexistent: TimeNonexistent = "raise",
):
return self._round(freq, RoundTo.NEAREST_HALF_EVEN, ambiguous, nonexistent)

@Appender((_round_doc + _floor_example).format(op="floor"))
def floor(self, freq, ambiguous="raise", nonexistent="raise"):
def floor(
self,
freq,
ambiguous: TimeAmbiguous = "raise",
nonexistent: TimeNonexistent = "raise",
):
return self._round(freq, RoundTo.MINUS_INFTY, ambiguous, nonexistent)

@Appender((_round_doc + _ceil_example).format(op="ceil"))
def ceil(self, freq, ambiguous="raise", nonexistent="raise"):
def ceil(
self,
freq,
ambiguous: TimeAmbiguous = "raise",
nonexistent: TimeNonexistent = "raise",
):
return self._round(freq, RoundTo.PLUS_INFTY, ambiguous, nonexistent)

# --------------------------------------------------------------
Expand Down
Loading