From 952283c33214e246554c24efb7e28bc6c5718875 Mon Sep 17 00:00:00 2001 From: Rob Kimball Date: Tue, 15 Jun 2021 20:37:28 -0400 Subject: [PATCH 1/7] Replace assert statements with argument validation --- pandas/core/apply.py | 3 ++- pandas/core/arrays/_ranges.py | 8 +++++--- pandas/core/arrays/datetimes.py | 3 ++- pandas/core/groupby/ops.py | 3 ++- pandas/core/indexes/base.py | 12 ++++++++---- pandas/core/indexes/datetimes.py | 3 ++- pandas/core/indexes/numeric.py | 6 ++++-- pandas/core/indexes/period.py | 3 ++- pandas/core/indexes/timedeltas.py | 3 ++- pandas/core/internals/blocks.py | 3 ++- pandas/core/missing.py | 3 ++- pandas/io/excel/_util.py | 3 ++- 12 files changed, 35 insertions(+), 18 deletions(-) diff --git a/pandas/core/apply.py b/pandas/core/apply.py index 49e06825617bd..3d82f70c1abdd 100644 --- a/pandas/core/apply.py +++ b/pandas/core/apply.py @@ -527,7 +527,8 @@ def normalize_dictlike_arg( that a nested renamer is not passed. Also normalizes to all lists when values consists of a mix of list and non-lists. """ - assert how in ("apply", "agg", "transform") + if how not in ("apply", "agg", "transform"): + raise ValueError('Value for how argument must be one of : apply, agg, transform') # Can't use func.values(); wouldn't work for a Series if ( diff --git a/pandas/core/arrays/_ranges.py b/pandas/core/arrays/_ranges.py index 3909875e5660a..6364a75a71ee1 100644 --- a/pandas/core/arrays/_ranges.py +++ b/pandas/core/arrays/_ranges.py @@ -102,7 +102,8 @@ def _generate_range_overflow_safe( OutOfBoundsDatetime """ # GH#14187 raise instead of incorrectly wrapping around - assert side in ["start", "end"] + if side not in ["start", "end"]: + raise ValueError('Value for side argument must be one of: start, end') i64max = np.uint64(i8max) msg = f"Cannot generate range with {side}={endpoint} and periods={periods}" @@ -148,8 +149,9 @@ def _generate_range_overflow_safe_signed( A special case for _generate_range_overflow_safe where `periods * stride` can be calculated without overflowing int64 bounds. """ - assert side in ["start", "end"] - if side == "end": + if side not in ['start', 'end']: + raise ValueError('Value for side argument must be one of: start, end') + if side == 'end': stride *= -1 with np.errstate(over="raise"): diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index 92a906e9fd8b0..73eb0f14f7b2a 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -2164,7 +2164,8 @@ def objects_to_datetime64ns( ------ ValueError : if data cannot be converted to datetimes """ - assert errors in ["raise", "ignore", "coerce"] + if errors not in ["raise", "ignore", "coerce"]: + raise ValueError('Value for errors argument must be one of: raise, coerce, ignore') # if str-dtype, convert data = np.array(data, copy=False, dtype=np.object_) diff --git a/pandas/core/groupby/ops.py b/pandas/core/groupby/ops.py index b65f26c7174fc..74d2c4cd1b6b5 100644 --- a/pandas/core/groupby/ops.py +++ b/pandas/core/groupby/ops.py @@ -967,7 +967,8 @@ def _cython_operation( """ Returns the values of a cython operation. """ - assert kind in ["transform", "aggregate"] + if kind not in ["transform", "aggregate"]: + raise ValueError('Value for kind argument must be one of: transform, aggregate') cy_op = WrappedCythonOp(kind=kind, how=how) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index b092bd6666fc0..a6f929d7cc584 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -3651,7 +3651,8 @@ def _convert_slice_indexer(self, key: slice, kind: str_t): key : label of the slice bound kind : {'loc', 'getitem'} """ - assert kind in ["loc", "getitem"], kind + if kind not in ["loc", "getitem"]: + raise ValueError('Value for kind argument must be one of: loc, getitem') # potentially cast the bounds to integers start, stop, step = key.start, key.stop, key.step @@ -5678,7 +5679,8 @@ def _validate_indexer(self, form: str_t, key, kind: str_t): If we are positional indexer, validate that we have appropriate typed bounds must be an integer. """ - assert kind in ["getitem", "iloc"] + if kind not in ["getitem", "iloc"]: + raise ValueError('Value for kind argument must be one of: getitem, iloc') if key is not None and not is_integer(key): raise self._invalid_indexer(form, key) @@ -5703,7 +5705,8 @@ def _maybe_cast_slice_bound(self, label, side: str_t, kind=no_default): ----- Value of `side` parameter should be validated in caller. """ - assert kind in ["loc", "getitem", None, no_default] + if kind not in ["loc", "getitem", None, no_default]: + raise ValueError('Value for kind argument must be one of: loc, getitem or None') self._deprecated_arg(kind, "kind", "_maybe_cast_slice_bound") # We are a plain index here (sub-class override this method if they @@ -5747,7 +5750,8 @@ def get_slice_bound(self, label, side: str_t, kind=None) -> int: int Index of label. """ - assert kind in ["loc", "getitem", None] + if kind not in ["loc", "getitem", None]: + raise ValueError('Value for kind argument must be one of: loc, getitem or None') if side not in ("left", "right"): raise ValueError( diff --git a/pandas/core/indexes/datetimes.py b/pandas/core/indexes/datetimes.py index d9eaaa763fde8..b4b849151ef56 100644 --- a/pandas/core/indexes/datetimes.py +++ b/pandas/core/indexes/datetimes.py @@ -728,7 +728,8 @@ def _maybe_cast_slice_bound(self, label, side: str, kind=lib.no_default): ----- Value of `side` parameter should be validated in caller. """ - assert kind in ["loc", "getitem", None, lib.no_default] + if kind not in ["loc", "getitem", None, lib.no_default]: + raise ValueError('Value for kind argument must be one of: loc, getitem or None') self._deprecated_arg(kind, "kind", "_maybe_cast_slice_bound") if isinstance(label, str): diff --git a/pandas/core/indexes/numeric.py b/pandas/core/indexes/numeric.py index 181451603efa4..7c4de647abf4f 100644 --- a/pandas/core/indexes/numeric.py +++ b/pandas/core/indexes/numeric.py @@ -236,7 +236,8 @@ def _should_fallback_to_positional(self) -> bool: @doc(Index._convert_slice_indexer) def _convert_slice_indexer(self, key: slice, kind: str): if is_float_dtype(self.dtype): - assert kind in ["loc", "getitem"] + if kind not in ["loc", "getitem"]: + raise ValueError('Value for kind argument must be one of: loc, getitem') # We always treat __getitem__ slicing as label-based # translate to locations @@ -246,7 +247,8 @@ def _convert_slice_indexer(self, key: slice, kind: str): @doc(Index._maybe_cast_slice_bound) def _maybe_cast_slice_bound(self, label, side: str, kind=lib.no_default): - assert kind in ["loc", "getitem", None, lib.no_default] + if kind not in ["loc", "getitem", None, lib.no_default]: + raise ValueError('Value for kind argument must be one of: loc, getitem or None') self._deprecated_arg(kind, "kind", "_maybe_cast_slice_bound") # we will try to coerce to integers diff --git a/pandas/core/indexes/period.py b/pandas/core/indexes/period.py index 731958e363b40..21c7f405d58bb 100644 --- a/pandas/core/indexes/period.py +++ b/pandas/core/indexes/period.py @@ -480,7 +480,8 @@ def _maybe_cast_slice_bound(self, label, side: str, kind=lib.no_default): Value of `side` parameter should be validated in caller. """ - assert kind in ["loc", "getitem", None, lib.no_default] + if kind not in ["loc", "getitem", None, lib.no_default]: + raise ValueError('Value for kind argument must be one of: loc, getitem or None') self._deprecated_arg(kind, "kind", "_maybe_cast_slice_bound") if isinstance(label, datetime): diff --git a/pandas/core/indexes/timedeltas.py b/pandas/core/indexes/timedeltas.py index 6599d2d44eee5..39418c96f8b77 100644 --- a/pandas/core/indexes/timedeltas.py +++ b/pandas/core/indexes/timedeltas.py @@ -192,7 +192,8 @@ def _maybe_cast_slice_bound(self, label, side: str, kind=lib.no_default): ------- label : object """ - assert kind in ["loc", "getitem", None, lib.no_default] + if kind not in ["loc", "getitem", None, lib.no_default]: + raise ValueError('Value for kind argument must be one of: loc, getitem or None') self._deprecated_arg(kind, "kind", "_maybe_cast_slice_bound") if isinstance(label, str): diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index 237d06402a0ee..294038ae089f6 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -1196,7 +1196,8 @@ def where(self, other, cond, errors="raise") -> list[Block]: assert cond.ndim == self.ndim assert not isinstance(other, (ABCIndex, ABCSeries, ABCDataFrame)) - assert errors in ["raise", "ignore"] + if errors not in ["raise", "ignore"]: + raise ValueError('Value for errors argument must be one of: raise, ignore') transpose = self.ndim == 2 values = self.values diff --git a/pandas/core/missing.py b/pandas/core/missing.py index 424173ccc69f0..648cb0771cc50 100644 --- a/pandas/core/missing.py +++ b/pandas/core/missing.py @@ -178,7 +178,8 @@ def find_valid_index(values, *, how: str) -> int | None: ------- int or None """ - assert how in ["first", "last"] + if how not in ["first", "last"]: + raise ValueError(f'Value for how argument must be one of : first, last') if len(values) == 0: # early stop return None diff --git a/pandas/io/excel/_util.py b/pandas/io/excel/_util.py index 7d8028de23257..d8b2a58218f58 100644 --- a/pandas/io/excel/_util.py +++ b/pandas/io/excel/_util.py @@ -59,7 +59,8 @@ def get_default_engine(ext, mode="reader"): "xls": "xlwt", "ods": "odf", } - assert mode in ["reader", "writer"] + if mode not in ["reader", "writer"]: + raise ValueError('File mode must be either "reader" or "writer".') if mode == "writer": # Prefer xlsxwriter over openpyxl if installed xlsxwriter = import_optional_dependency("xlsxwriter", errors="warn") From 6a3efb9b5fb775b13ba6256a8526a60862c65ac9 Mon Sep 17 00:00:00 2001 From: Rob Kimball Date: Tue, 22 Jun 2021 08:39:05 -0400 Subject: [PATCH 2/7] rebase to master --- pandas/_typing.py | 2 ++ pandas/core/apply.py | 6 +++- pandas/core/arrays/_ranges.py | 10 ++++-- pandas/core/arrays/datetimes.py | 20 +++++------ pandas/core/frame.py | 2 +- pandas/core/indexes/base.py | 2 +- pandas/core/missing.py | 3 +- pandas/core/reshape/merge.py | 16 +++++---- pandas/tseries/frequencies.py | 6 ++-- pandas/tseries/holiday.py | 61 +++++++++++++++++++++++++-------- 10 files changed, 86 insertions(+), 42 deletions(-) diff --git a/pandas/_typing.py b/pandas/_typing.py index a9852dd4b13cf..54440e2d83531 100644 --- a/pandas/_typing.py +++ b/pandas/_typing.py @@ -123,6 +123,8 @@ Frequency = Union[str, "DateOffset"] Axes = Collection[Any] RandomState = Union[int, ArrayLike, np.random.Generator, np.random.RandomState] +MergeTypes = Literal['inner', 'outer', 'left', 'right', 'cross'] +ConcatTypes = Literal['inner', 'outer'] # dtypes NpDtype = Union[str, np.dtype] diff --git a/pandas/core/apply.py b/pandas/core/apply.py index 3d82f70c1abdd..274cdb365ef84 100644 --- a/pandas/core/apply.py +++ b/pandas/core/apply.py @@ -9,6 +9,7 @@ Hashable, Iterator, List, + Literal, cast, ) import warnings @@ -518,7 +519,10 @@ def apply_multiple(self) -> FrameOrSeriesUnion: return self.obj.aggregate(self.f, self.axis, *self.args, **self.kwargs) def normalize_dictlike_arg( - self, how: str, obj: FrameOrSeriesUnion, func: AggFuncTypeDict + self, + how: Literal['apply', 'agg', 'transform'], + obj: FrameOrSeriesUnion, + func: AggFuncTypeDict, ) -> AggFuncTypeDict: """ Handler for dict-like argument. diff --git a/pandas/core/arrays/_ranges.py b/pandas/core/arrays/_ranges.py index 6364a75a71ee1..36e4be9d932ec 100644 --- a/pandas/core/arrays/_ranges.py +++ b/pandas/core/arrays/_ranges.py @@ -4,6 +4,10 @@ """ from __future__ import annotations +from typing import ( + Literal, +) + import numpy as np from pandas._libs.lib import i8max @@ -75,7 +79,7 @@ def generate_regular_range( def _generate_range_overflow_safe( - endpoint: int, periods: int, stride: int, side: str = "start" + endpoint: int, periods: int, stride: int, side: Literal['start', 'end'] = "start" ) -> int: """ Calculate the second endpoint for passing to np.arange, checking @@ -143,7 +147,7 @@ def _generate_range_overflow_safe( def _generate_range_overflow_safe_signed( - endpoint: int, periods: int, stride: int, side: str + endpoint: int, periods: int, stride: int, side: Literal['start', 'end'] ) -> int: """ A special case for _generate_range_overflow_safe where `periods * stride` @@ -154,7 +158,7 @@ def _generate_range_overflow_safe_signed( if side == 'end': stride *= -1 - with np.errstate(over="raise"): + with np.errstate(over='raise'): addend = np.int64(periods) * np.int64(stride) try: # easy case with no overflows diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index 73eb0f14f7b2a..1159e5ae5c3d9 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -1967,12 +1967,12 @@ def sequence_to_datetimes( def sequence_to_dt64ns( data, - dtype=None, - copy=False, - tz=None, - dayfirst=False, - yearfirst=False, - ambiguous="raise", + dtype: str = None, + copy: bool = False, + tz: tzinfo | str = None, + dayfirst: bool = False, + yearfirst: bool = False, + ambiguous: str | bool = "raise", *, allow_object: bool = False, allow_mixed: bool = False, @@ -2126,10 +2126,10 @@ def sequence_to_dt64ns( def objects_to_datetime64ns( data: np.ndarray, - dayfirst, - yearfirst, - utc=False, - errors="raise", + dayfirst: bool, + yearfirst: bool, + utc: bool = False, + errors: Literal["raise", "coerce", "ignore"] = "raise", require_iso8601: bool = False, allow_object: bool = False, allow_mixed: bool = False, diff --git a/pandas/core/frame.py b/pandas/core/frame.py index ec7de0440f482..6b3277cf3a948 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -9193,7 +9193,7 @@ def merge( sort: bool = False, suffixes: Suffixes = ("_x", "_y"), copy: bool = True, - indicator: bool = False, + indicator: bool | str = False, validate: str | None = None, ) -> DataFrame: from pandas.core.reshape.merge import merge diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index a6f929d7cc584..e30b0cc82d941 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -5685,7 +5685,7 @@ def _validate_indexer(self, form: str_t, key, kind: str_t): if key is not None and not is_integer(key): raise self._invalid_indexer(form, key) - def _maybe_cast_slice_bound(self, label, side: str_t, kind=no_default): + def _maybe_cast_slice_bound(self, label, side: str_t, kind: Literal["loc", "getitem"] = no_default): """ This function should be overloaded in subclasses that allow non-trivial casting on label-slice bounds, e.g. datetime-like indices allowing diff --git a/pandas/core/missing.py b/pandas/core/missing.py index 648cb0771cc50..1f1539b7af284 100644 --- a/pandas/core/missing.py +++ b/pandas/core/missing.py @@ -11,6 +11,7 @@ TYPE_CHECKING, Any, cast, + Literal, ) import numpy as np @@ -164,7 +165,7 @@ def clean_interp_method(method: str, index: Index, **kwargs) -> str: return method -def find_valid_index(values, *, how: str) -> int | None: +def find_valid_index(values, *, how: Literal['first', 'last']) -> int | None: """ Retrieves the index of the first valid value. diff --git a/pandas/core/reshape/merge.py b/pandas/core/reshape/merge.py index 143999a4677b3..29e593e3b2d83 100644 --- a/pandas/core/reshape/merge.py +++ b/pandas/core/reshape/merge.py @@ -29,6 +29,8 @@ FrameOrSeries, IndexLabel, Suffixes, + MergeTypes, + TimedeltaConvertibleTypes, ) from pandas.errors import MergeError from pandas.util._decorators import ( @@ -92,7 +94,7 @@ def merge( left: DataFrame | Series, right: DataFrame | Series, - how: str = "inner", + how: MergeTypes = "inner", on: IndexLabel | None = None, left_on: IndexLabel | None = None, right_on: IndexLabel | None = None, @@ -101,7 +103,7 @@ def merge( sort: bool = False, suffixes: Suffixes = ("_x", "_y"), copy: bool = True, - indicator: bool = False, + indicator: bool | str = False, validate: str | None = None, ) -> DataFrame: op = _MergeOperation( @@ -331,11 +333,11 @@ def merge_asof( right_on: IndexLabel | None = None, left_index: bool = False, right_index: bool = False, - by=None, - left_by=None, - right_by=None, + by: IndexLabel | None = None, + left_by: Hashable | None = None, + right_by: Hashable | None = None, suffixes: Suffixes = ("_x", "_y"), - tolerance=None, + tolerance: None | TimedeltaConvertibleTypes = None, allow_exact_matches: bool = True, direction: str = "backward", ) -> DataFrame: @@ -622,7 +624,7 @@ def __init__( sort: bool = True, suffixes: Suffixes = ("_x", "_y"), copy: bool = True, - indicator: bool = False, + indicator: bool | str = False, validate: str | None = None, ): _left = _validate_operand(left) diff --git a/pandas/tseries/frequencies.py b/pandas/tseries/frequencies.py index c55ac6ce228bf..7b84dbb635133 100644 --- a/pandas/tseries/frequencies.py +++ b/pandas/tseries/frequencies.py @@ -445,7 +445,7 @@ def _maybe_add_count(base: str, count: float) -> str: # Frequency comparison -def is_subperiod(source, target) -> bool: +def is_subperiod(source: str | DateOffset, target: str | DateOffset) -> bool: """ Returns True if downsampling is possible between source and target frequencies @@ -501,7 +501,7 @@ def is_subperiod(source, target) -> bool: return False -def is_superperiod(source, target) -> bool: +def is_superperiod(source: str | DateOffset, target: str | DateOffset) -> bool: """ Returns True if upsampling is possible between source and target frequencies @@ -559,7 +559,7 @@ def is_superperiod(source, target) -> bool: return False -def _maybe_coerce_freq(code) -> str: +def _maybe_coerce_freq(code: str | DateOffset) -> str: """we might need to coerce a code to a rule_code and uppercase it diff --git a/pandas/tseries/holiday.py b/pandas/tseries/holiday.py index 54ac116afe3cf..b2b014e84e37d 100644 --- a/pandas/tseries/holiday.py +++ b/pandas/tseries/holiday.py @@ -1,4 +1,9 @@ from __future__ import annotations +from typing import ( + Any, + Callable, + Tuple, +) from datetime import ( datetime, @@ -33,6 +38,10 @@ Easter, ) +from pandas._typing import ( + TimestampConvertibleTypes +) + def next_monday(dt: datetime) -> datetime: """ @@ -151,27 +160,39 @@ class Holiday: def __init__( self, - name, - year=None, - month=None, - day=None, - offset=None, - observance=None, - start_date=None, - end_date=None, - days_of_week=None, + name: str, + year: int | None = None, + month: int | None = None, + day: int | None = None, + offset: list[Any] | Any = None, + observance: Callable[..., DatetimeIndex] = None, + start_date: TimestampConvertibleTypes = None, + end_date: TimestampConvertibleTypes = None, + days_of_week: Tuple[int, ...] = None, ): """ Parameters ---------- name : str Name of the holiday , defaults to class name + year : int, optional + the year in which the holiday occurs + month : int, optional + the month in which the holiday occurs + day : int, optional + the day on which the holiday occurs offset : array of pandas.tseries.offsets or class from pandas.tseries.offsets computes offset from date observance: function computes when holiday is given a pandas Timestamp - days_of_week: + start_date : datetime-like + optionally constrain the period in which the holdiday occurs + can be any valid value for a pandas Timestamp + end_date : datetime-like + optionally constrain the period in which the holdiday occurs + can be any valid value for a pandas Timestamp + days_of_week: tuple(int, ...) provide a tuple of days e.g (0,1,2,3,) for Monday Through Thursday Monday=0,..,Sunday=6 @@ -239,7 +260,12 @@ def __repr__(self) -> str: repr = f"Holiday: {self.name} ({info})" return repr - def dates(self, start_date, end_date, return_name=False): + def dates( + self, + start_date: TimestampConvertibleTypes, + end_date: TimestampConvertibleTypes, + return_name: bool = False, + ): """ Calculate holidays observed between start date and end date @@ -286,7 +312,7 @@ def dates(self, start_date, end_date, return_name=False): return Series(self.name, index=holiday_dates) return holiday_dates - def _reference_dates(self, start_date, end_date): + def _reference_dates(self, start_date: TimestampConvertibleTypes, end_date: TimestampConvertibleTypes): """ Get reference dates for the holiday. @@ -319,7 +345,7 @@ def _reference_dates(self, start_date, end_date): return dates - def _apply_rule(self, dates): + def _apply_rule(self, dates: DatetimeIndex): """ Apply the given offset/observance to a DatetimeIndex of dates. @@ -390,7 +416,7 @@ class AbstractHolidayCalendar(metaclass=HolidayCalendarMetaClass): end_date = Timestamp(datetime(2200, 12, 31)) _cache = None - def __init__(self, name=None, rules=None): + def __init__(self, name: str = None, rules: list[Holiday] = None): """ Initializes holiday object with a given set a rules. Normally classes just have the rules defined within them. @@ -417,7 +443,12 @@ def rule_from_name(self, name): return None - def holidays(self, start=None, end=None, return_name=False): + def holidays( + self, + start: TimestampConvertibleTypes = None, + end: TimestampConvertibleTypes = None, + return_name: bool = False + ) -> DatetimeIndex: """ Returns a curve with holidays between start_date and end_date From bca799d97771338babdc4dfad9dcf3eb35824fda Mon Sep 17 00:00:00 2001 From: Rob Kimball Date: Tue, 15 Jun 2021 20:58:50 -0400 Subject: [PATCH 3/7] Fix line length guidance for value check error messages --- pandas/core/apply.py | 3 ++- pandas/core/arrays/datetimes.py | 3 ++- pandas/core/groupby/ops.py | 3 ++- pandas/core/indexes/base.py | 13 ++++++++++--- pandas/core/indexes/datetimes.py | 3 ++- pandas/core/indexes/numeric.py | 3 ++- pandas/core/indexes/period.py | 3 ++- pandas/core/indexes/timedeltas.py | 3 ++- pandas/tseries/holiday.py | 6 +++++- 9 files changed, 29 insertions(+), 11 deletions(-) diff --git a/pandas/core/apply.py b/pandas/core/apply.py index 274cdb365ef84..6038327c80e9d 100644 --- a/pandas/core/apply.py +++ b/pandas/core/apply.py @@ -532,7 +532,8 @@ def normalize_dictlike_arg( when values consists of a mix of list and non-lists. """ if how not in ("apply", "agg", "transform"): - raise ValueError('Value for how argument must be one of : apply, agg, transform') + raise ValueError('Value for how argument must be one of : ' + 'apply, agg, transform') # Can't use func.values(); wouldn't work for a Series if ( diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index 1159e5ae5c3d9..8731566ea0412 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -2165,7 +2165,8 @@ def objects_to_datetime64ns( ValueError : if data cannot be converted to datetimes """ if errors not in ["raise", "ignore", "coerce"]: - raise ValueError('Value for errors argument must be one of: raise, coerce, ignore') + raise ValueError('Value for errors argument must be one of: ' + 'raise, coerce, ignore') # if str-dtype, convert data = np.array(data, copy=False, dtype=np.object_) diff --git a/pandas/core/groupby/ops.py b/pandas/core/groupby/ops.py index 74d2c4cd1b6b5..7c7e96d7e5817 100644 --- a/pandas/core/groupby/ops.py +++ b/pandas/core/groupby/ops.py @@ -968,7 +968,8 @@ def _cython_operation( Returns the values of a cython operation. """ if kind not in ["transform", "aggregate"]: - raise ValueError('Value for kind argument must be one of: transform, aggregate') + raise ValueError('Value for kind argument must be one of: ' + 'transform, aggregate') cy_op = WrappedCythonOp(kind=kind, how=how) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index e30b0cc82d941..ec303e5d2dd50 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -5685,7 +5685,12 @@ def _validate_indexer(self, form: str_t, key, kind: str_t): if key is not None and not is_integer(key): raise self._invalid_indexer(form, key) - def _maybe_cast_slice_bound(self, label, side: str_t, kind: Literal["loc", "getitem"] = no_default): + def _maybe_cast_slice_bound( + self, + label, + side: str_t, + kind: Literal["loc", "getitem"] = no_default + ): """ This function should be overloaded in subclasses that allow non-trivial casting on label-slice bounds, e.g. datetime-like indices allowing @@ -5706,7 +5711,8 @@ def _maybe_cast_slice_bound(self, label, side: str_t, kind: Literal["loc", "geti Value of `side` parameter should be validated in caller. """ if kind not in ["loc", "getitem", None, no_default]: - raise ValueError('Value for kind argument must be one of: loc, getitem or None') + raise ValueError('Value for kind argument must be one of: ' + 'loc, getitem or None') self._deprecated_arg(kind, "kind", "_maybe_cast_slice_bound") # We are a plain index here (sub-class override this method if they @@ -5751,7 +5757,8 @@ def get_slice_bound(self, label, side: str_t, kind=None) -> int: Index of label. """ if kind not in ["loc", "getitem", None]: - raise ValueError('Value for kind argument must be one of: loc, getitem or None') + raise ValueError('Value for kind argument must be one of: ' + 'loc, getitem or None') if side not in ("left", "right"): raise ValueError( diff --git a/pandas/core/indexes/datetimes.py b/pandas/core/indexes/datetimes.py index b4b849151ef56..21be470a2adaf 100644 --- a/pandas/core/indexes/datetimes.py +++ b/pandas/core/indexes/datetimes.py @@ -729,7 +729,8 @@ def _maybe_cast_slice_bound(self, label, side: str, kind=lib.no_default): Value of `side` parameter should be validated in caller. """ if kind not in ["loc", "getitem", None, lib.no_default]: - raise ValueError('Value for kind argument must be one of: loc, getitem or None') + raise ValueError('Value for kind argument must be one of: ' + 'loc, getitem or None') self._deprecated_arg(kind, "kind", "_maybe_cast_slice_bound") if isinstance(label, str): diff --git a/pandas/core/indexes/numeric.py b/pandas/core/indexes/numeric.py index 7c4de647abf4f..998fad3bb5ec1 100644 --- a/pandas/core/indexes/numeric.py +++ b/pandas/core/indexes/numeric.py @@ -248,7 +248,8 @@ def _convert_slice_indexer(self, key: slice, kind: str): @doc(Index._maybe_cast_slice_bound) def _maybe_cast_slice_bound(self, label, side: str, kind=lib.no_default): if kind not in ["loc", "getitem", None, lib.no_default]: - raise ValueError('Value for kind argument must be one of: loc, getitem or None') + raise ValueError('Value for kind argument must be one of: ' + 'loc, getitem or None') self._deprecated_arg(kind, "kind", "_maybe_cast_slice_bound") # we will try to coerce to integers diff --git a/pandas/core/indexes/period.py b/pandas/core/indexes/period.py index 21c7f405d58bb..5b86264b8a6f6 100644 --- a/pandas/core/indexes/period.py +++ b/pandas/core/indexes/period.py @@ -481,7 +481,8 @@ def _maybe_cast_slice_bound(self, label, side: str, kind=lib.no_default): """ if kind not in ["loc", "getitem", None, lib.no_default]: - raise ValueError('Value for kind argument must be one of: loc, getitem or None') + raise ValueError('Value for kind argument must be one of: ' + 'loc, getitem or None') self._deprecated_arg(kind, "kind", "_maybe_cast_slice_bound") if isinstance(label, datetime): diff --git a/pandas/core/indexes/timedeltas.py b/pandas/core/indexes/timedeltas.py index 39418c96f8b77..e743cc381bc2d 100644 --- a/pandas/core/indexes/timedeltas.py +++ b/pandas/core/indexes/timedeltas.py @@ -193,7 +193,8 @@ def _maybe_cast_slice_bound(self, label, side: str, kind=lib.no_default): label : object """ if kind not in ["loc", "getitem", None, lib.no_default]: - raise ValueError('Value for kind argument must be one of: loc, getitem or None') + raise ValueError('Value for kind argument must be one of: ' + 'loc, getitem or None') self._deprecated_arg(kind, "kind", "_maybe_cast_slice_bound") if isinstance(label, str): diff --git a/pandas/tseries/holiday.py b/pandas/tseries/holiday.py index b2b014e84e37d..883eb39c4029b 100644 --- a/pandas/tseries/holiday.py +++ b/pandas/tseries/holiday.py @@ -312,7 +312,11 @@ def dates( return Series(self.name, index=holiday_dates) return holiday_dates - def _reference_dates(self, start_date: TimestampConvertibleTypes, end_date: TimestampConvertibleTypes): + def _reference_dates( + self, + start_date: TimestampConvertibleTypes, + end_date: TimestampConvertibleTypes + ): """ Get reference dates for the holiday. From 1270474d8b23bf33f0957a7cf568dc3d386a665e Mon Sep 17 00:00:00 2001 From: Rob Kimball Date: Tue, 22 Jun 2021 08:39:28 -0400 Subject: [PATCH 4/7] rebase to master --- pandas/_typing.py | 4 ++-- pandas/core/apply.py | 2 +- pandas/core/arrays/_ranges.py | 11 +++++------ pandas/core/arrays/datetimes.py | 5 ++++- pandas/core/groupby/ops.py | 4 +--- pandas/core/indexes/base.py | 10 +++------- pandas/core/indexes/datetimes.py | 4 +--- pandas/core/indexes/numeric.py | 7 ++----- pandas/core/indexes/period.py | 4 +--- pandas/core/indexes/timedeltas.py | 4 +--- pandas/core/missing.py | 2 +- 11 files changed, 22 insertions(+), 35 deletions(-) diff --git a/pandas/_typing.py b/pandas/_typing.py index 54440e2d83531..8ad3a4f37a050 100644 --- a/pandas/_typing.py +++ b/pandas/_typing.py @@ -123,8 +123,8 @@ Frequency = Union[str, "DateOffset"] Axes = Collection[Any] RandomState = Union[int, ArrayLike, np.random.Generator, np.random.RandomState] -MergeTypes = Literal['inner', 'outer', 'left', 'right', 'cross'] -ConcatTypes = Literal['inner', 'outer'] +MergeTypes = Literal["inner", "outer", "left", "right", "cross"] +ConcatTypes = Literal["inner", "outer"] # dtypes NpDtype = Union[str, np.dtype] diff --git a/pandas/core/apply.py b/pandas/core/apply.py index 6038327c80e9d..77828c03ab039 100644 --- a/pandas/core/apply.py +++ b/pandas/core/apply.py @@ -520,7 +520,7 @@ def apply_multiple(self) -> FrameOrSeriesUnion: def normalize_dictlike_arg( self, - how: Literal['apply', 'agg', 'transform'], + how: Literal["apply", "agg", "transform"], obj: FrameOrSeriesUnion, func: AggFuncTypeDict, ) -> AggFuncTypeDict: diff --git a/pandas/core/arrays/_ranges.py b/pandas/core/arrays/_ranges.py index 36e4be9d932ec..9d40bc1dd99da 100644 --- a/pandas/core/arrays/_ranges.py +++ b/pandas/core/arrays/_ranges.py @@ -79,7 +79,7 @@ def generate_regular_range( def _generate_range_overflow_safe( - endpoint: int, periods: int, stride: int, side: Literal['start', 'end'] = "start" + endpoint: int, periods: int, stride: int, side: Literal["start", "end"] = "start" ) -> int: """ Calculate the second endpoint for passing to np.arange, checking @@ -106,8 +106,7 @@ def _generate_range_overflow_safe( OutOfBoundsDatetime """ # GH#14187 raise instead of incorrectly wrapping around - if side not in ["start", "end"]: - raise ValueError('Value for side argument must be one of: start, end') + assert side in ["start", "end"] i64max = np.uint64(i8max) msg = f"Cannot generate range with {side}={endpoint} and periods={periods}" @@ -147,14 +146,14 @@ def _generate_range_overflow_safe( def _generate_range_overflow_safe_signed( - endpoint: int, periods: int, stride: int, side: Literal['start', 'end'] + endpoint: int, periods: int, stride: int, side: Literal["start", "end"] ) -> int: """ A special case for _generate_range_overflow_safe where `periods * stride` can be calculated without overflowing int64 bounds. """ - if side not in ['start', 'end']: - raise ValueError('Value for side argument must be one of: start, end') + assert side in ['start', 'end'] + if side == 'end': stride *= -1 diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index 8731566ea0412..68cf65f7fe5cf 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -19,6 +19,9 @@ lib, tslib, ) +from pandas._typing import ( + Dtype, +) from pandas._libs.arrays import NDArrayBacked from pandas._libs.tslibs import ( BaseOffset, @@ -1967,7 +1970,7 @@ def sequence_to_datetimes( def sequence_to_dt64ns( data, - dtype: str = None, + dtype: Dtype = None, copy: bool = False, tz: tzinfo | str = None, dayfirst: bool = False, diff --git a/pandas/core/groupby/ops.py b/pandas/core/groupby/ops.py index 7c7e96d7e5817..b65f26c7174fc 100644 --- a/pandas/core/groupby/ops.py +++ b/pandas/core/groupby/ops.py @@ -967,9 +967,7 @@ def _cython_operation( """ Returns the values of a cython operation. """ - if kind not in ["transform", "aggregate"]: - raise ValueError('Value for kind argument must be one of: ' - 'transform, aggregate') + assert kind in ["transform", "aggregate"] cy_op = WrappedCythonOp(kind=kind, how=how) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index ec303e5d2dd50..4d4604bbeb468 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -3651,8 +3651,7 @@ def _convert_slice_indexer(self, key: slice, kind: str_t): key : label of the slice bound kind : {'loc', 'getitem'} """ - if kind not in ["loc", "getitem"]: - raise ValueError('Value for kind argument must be one of: loc, getitem') + assert kind in ["loc", "getitem"], kind # potentially cast the bounds to integers start, stop, step = key.start, key.stop, key.step @@ -5679,8 +5678,7 @@ def _validate_indexer(self, form: str_t, key, kind: str_t): If we are positional indexer, validate that we have appropriate typed bounds must be an integer. """ - if kind not in ["getitem", "iloc"]: - raise ValueError('Value for kind argument must be one of: getitem, iloc') + assert kind in ["getitem", "iloc"] if key is not None and not is_integer(key): raise self._invalid_indexer(form, key) @@ -5710,9 +5708,7 @@ def _maybe_cast_slice_bound( ----- Value of `side` parameter should be validated in caller. """ - if kind not in ["loc", "getitem", None, no_default]: - raise ValueError('Value for kind argument must be one of: ' - 'loc, getitem or None') + assert kind in ["loc", "getitem", None, no_default] self._deprecated_arg(kind, "kind", "_maybe_cast_slice_bound") # We are a plain index here (sub-class override this method if they diff --git a/pandas/core/indexes/datetimes.py b/pandas/core/indexes/datetimes.py index 21be470a2adaf..d9eaaa763fde8 100644 --- a/pandas/core/indexes/datetimes.py +++ b/pandas/core/indexes/datetimes.py @@ -728,9 +728,7 @@ def _maybe_cast_slice_bound(self, label, side: str, kind=lib.no_default): ----- Value of `side` parameter should be validated in caller. """ - if kind not in ["loc", "getitem", None, lib.no_default]: - raise ValueError('Value for kind argument must be one of: ' - 'loc, getitem or None') + assert kind in ["loc", "getitem", None, lib.no_default] self._deprecated_arg(kind, "kind", "_maybe_cast_slice_bound") if isinstance(label, str): diff --git a/pandas/core/indexes/numeric.py b/pandas/core/indexes/numeric.py index 998fad3bb5ec1..181451603efa4 100644 --- a/pandas/core/indexes/numeric.py +++ b/pandas/core/indexes/numeric.py @@ -236,8 +236,7 @@ def _should_fallback_to_positional(self) -> bool: @doc(Index._convert_slice_indexer) def _convert_slice_indexer(self, key: slice, kind: str): if is_float_dtype(self.dtype): - if kind not in ["loc", "getitem"]: - raise ValueError('Value for kind argument must be one of: loc, getitem') + assert kind in ["loc", "getitem"] # We always treat __getitem__ slicing as label-based # translate to locations @@ -247,9 +246,7 @@ def _convert_slice_indexer(self, key: slice, kind: str): @doc(Index._maybe_cast_slice_bound) def _maybe_cast_slice_bound(self, label, side: str, kind=lib.no_default): - if kind not in ["loc", "getitem", None, lib.no_default]: - raise ValueError('Value for kind argument must be one of: ' - 'loc, getitem or None') + assert kind in ["loc", "getitem", None, lib.no_default] self._deprecated_arg(kind, "kind", "_maybe_cast_slice_bound") # we will try to coerce to integers diff --git a/pandas/core/indexes/period.py b/pandas/core/indexes/period.py index 5b86264b8a6f6..731958e363b40 100644 --- a/pandas/core/indexes/period.py +++ b/pandas/core/indexes/period.py @@ -480,9 +480,7 @@ def _maybe_cast_slice_bound(self, label, side: str, kind=lib.no_default): Value of `side` parameter should be validated in caller. """ - if kind not in ["loc", "getitem", None, lib.no_default]: - raise ValueError('Value for kind argument must be one of: ' - 'loc, getitem or None') + assert kind in ["loc", "getitem", None, lib.no_default] self._deprecated_arg(kind, "kind", "_maybe_cast_slice_bound") if isinstance(label, datetime): diff --git a/pandas/core/indexes/timedeltas.py b/pandas/core/indexes/timedeltas.py index e743cc381bc2d..6599d2d44eee5 100644 --- a/pandas/core/indexes/timedeltas.py +++ b/pandas/core/indexes/timedeltas.py @@ -192,9 +192,7 @@ def _maybe_cast_slice_bound(self, label, side: str, kind=lib.no_default): ------- label : object """ - if kind not in ["loc", "getitem", None, lib.no_default]: - raise ValueError('Value for kind argument must be one of: ' - 'loc, getitem or None') + assert kind in ["loc", "getitem", None, lib.no_default] self._deprecated_arg(kind, "kind", "_maybe_cast_slice_bound") if isinstance(label, str): diff --git a/pandas/core/missing.py b/pandas/core/missing.py index 1f1539b7af284..030e30f2699d8 100644 --- a/pandas/core/missing.py +++ b/pandas/core/missing.py @@ -165,7 +165,7 @@ def clean_interp_method(method: str, index: Index, **kwargs) -> str: return method -def find_valid_index(values, *, how: Literal['first', 'last']) -> int | None: +def find_valid_index(values, *, how: Literal["first", "last"]) -> int | None: """ Retrieves the index of the first valid value. From ae6f764eac895683ec9de3d278f82d237afe99d2 Mon Sep 17 00:00:00 2001 From: Rob Kimball Date: Tue, 22 Jun 2021 08:42:04 -0400 Subject: [PATCH 5/7] Adds None default for dtype argument --- pandas/core/arrays/datetimes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index 68cf65f7fe5cf..43d056d9a72c3 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -1970,7 +1970,7 @@ def sequence_to_datetimes( def sequence_to_dt64ns( data, - dtype: Dtype = None, + dtype: Dtype | None = None, copy: bool = False, tz: tzinfo | str = None, dayfirst: bool = False, From 346210f68dd199e56ab3e63a52fe00342d65eb1c Mon Sep 17 00:00:00 2001 From: Rob Kimball Date: Tue, 22 Jun 2021 10:32:27 -0400 Subject: [PATCH 6/7] Move Literal import out of type checking block --- pandas/_typing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/_typing.py b/pandas/_typing.py index 8ad3a4f37a050..21ea5ce5e4a83 100644 --- a/pandas/_typing.py +++ b/pandas/_typing.py @@ -21,6 +21,7 @@ Dict, Hashable, List, + Literal, Mapping, Optional, Sequence, @@ -37,7 +38,6 @@ # https://mypy.readthedocs.io/en/latest/common_issues.html#import-cycles if TYPE_CHECKING: from typing import ( - Literal, TypedDict, final, ) From d051c48a80012c9fabd6dc5dc41b936b0c657448 Mon Sep 17 00:00:00 2001 From: Rob Kimball Date: Tue, 22 Jun 2021 10:39:43 -0400 Subject: [PATCH 7/7] Addresses flake8 issues --- pandas/core/apply.py | 5 +++-- pandas/core/arrays/_ranges.py | 10 ++++----- pandas/core/arrays/datetimes.py | 9 ++++----- pandas/core/indexes/base.py | 10 ++++----- pandas/core/internals/blocks.py | 2 +- pandas/core/missing.py | 4 ++-- pandas/core/reshape/merge.py | 2 +- pandas/tseries/holiday.py | 36 +++++++++++++++------------------ 8 files changed, 35 insertions(+), 43 deletions(-) diff --git a/pandas/core/apply.py b/pandas/core/apply.py index 77828c03ab039..39c5ef30fb351 100644 --- a/pandas/core/apply.py +++ b/pandas/core/apply.py @@ -532,8 +532,9 @@ def normalize_dictlike_arg( when values consists of a mix of list and non-lists. """ if how not in ("apply", "agg", "transform"): - raise ValueError('Value for how argument must be one of : ' - 'apply, agg, transform') + raise ValueError( + "Value for how argument must be one of : apply, agg, transform" + ) # Can't use func.values(); wouldn't work for a Series if ( diff --git a/pandas/core/arrays/_ranges.py b/pandas/core/arrays/_ranges.py index 9d40bc1dd99da..3018219219e58 100644 --- a/pandas/core/arrays/_ranges.py +++ b/pandas/core/arrays/_ranges.py @@ -4,9 +4,7 @@ """ from __future__ import annotations -from typing import ( - Literal, -) +from typing import Literal import numpy as np @@ -152,12 +150,12 @@ def _generate_range_overflow_safe_signed( A special case for _generate_range_overflow_safe where `periods * stride` can be calculated without overflowing int64 bounds. """ - assert side in ['start', 'end'] + assert side in ["start", "end"] - if side == 'end': + if side == "end": stride *= -1 - with np.errstate(over='raise'): + with np.errstate(over="raise"): addend = np.int64(periods) * np.int64(stride) try: # easy case with no overflows diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index 43d056d9a72c3..62554a067d1a0 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -19,9 +19,6 @@ lib, tslib, ) -from pandas._typing import ( - Dtype, -) from pandas._libs.arrays import NDArrayBacked from pandas._libs.tslibs import ( BaseOffset, @@ -40,6 +37,7 @@ to_offset, tzconversion, ) +from pandas._typing import Dtype from pandas.errors import PerformanceWarning from pandas.core.dtypes.cast import astype_dt64_to_dt64tz @@ -2168,8 +2166,9 @@ def objects_to_datetime64ns( ValueError : if data cannot be converted to datetimes """ if errors not in ["raise", "ignore", "coerce"]: - raise ValueError('Value for errors argument must be one of: ' - 'raise, coerce, ignore') + raise ValueError( + "Value for errors argument must be one of: raise, coerce, ignore" + ) # if str-dtype, convert data = np.array(data, copy=False, dtype=np.object_) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 4d4604bbeb468..29a0379edded5 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -5684,10 +5684,7 @@ def _validate_indexer(self, form: str_t, key, kind: str_t): raise self._invalid_indexer(form, key) def _maybe_cast_slice_bound( - self, - label, - side: str_t, - kind: Literal["loc", "getitem"] = no_default + self, label, side: str_t, kind: Literal["loc", "getitem"] = no_default ): """ This function should be overloaded in subclasses that allow non-trivial @@ -5753,8 +5750,9 @@ def get_slice_bound(self, label, side: str_t, kind=None) -> int: Index of label. """ if kind not in ["loc", "getitem", None]: - raise ValueError('Value for kind argument must be one of: ' - 'loc, getitem or None') + raise ValueError( + "Value for kind argument must be one of: loc, getitem or None" + ) if side not in ("left", "right"): raise ValueError( diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index 294038ae089f6..c8db8519e277f 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -1197,7 +1197,7 @@ def where(self, other, cond, errors="raise") -> list[Block]: assert not isinstance(other, (ABCIndex, ABCSeries, ABCDataFrame)) if errors not in ["raise", "ignore"]: - raise ValueError('Value for errors argument must be one of: raise, ignore') + raise ValueError("Value for errors argument must be one of: raise, ignore") transpose = self.ndim == 2 values = self.values diff --git a/pandas/core/missing.py b/pandas/core/missing.py index 030e30f2699d8..fc99d9a9bca26 100644 --- a/pandas/core/missing.py +++ b/pandas/core/missing.py @@ -10,8 +10,8 @@ from typing import ( TYPE_CHECKING, Any, - cast, Literal, + cast, ) import numpy as np @@ -180,7 +180,7 @@ def find_valid_index(values, *, how: Literal["first", "last"]) -> int | None: int or None """ if how not in ["first", "last"]: - raise ValueError(f'Value for how argument must be one of : first, last') + raise ValueError("Value for how argument must be one of : first, last") if len(values) == 0: # early stop return None diff --git a/pandas/core/reshape/merge.py b/pandas/core/reshape/merge.py index 29e593e3b2d83..8a3c6985b1f08 100644 --- a/pandas/core/reshape/merge.py +++ b/pandas/core/reshape/merge.py @@ -28,8 +28,8 @@ DtypeObj, FrameOrSeries, IndexLabel, - Suffixes, MergeTypes, + Suffixes, TimedeltaConvertibleTypes, ) from pandas.errors import MergeError diff --git a/pandas/tseries/holiday.py b/pandas/tseries/holiday.py index 883eb39c4029b..165f3fcb62bd5 100644 --- a/pandas/tseries/holiday.py +++ b/pandas/tseries/holiday.py @@ -1,14 +1,14 @@ from __future__ import annotations -from typing import ( - Any, - Callable, - Tuple, -) from datetime import ( datetime, timedelta, ) +from typing import ( + Any, + Callable, + Tuple, +) import warnings from dateutil.relativedelta import ( # noqa @@ -38,9 +38,7 @@ Easter, ) -from pandas._typing import ( - TimestampConvertibleTypes -) +from pandas._typing import TimestampConvertibleTypes def next_monday(dt: datetime) -> datetime: @@ -168,7 +166,7 @@ def __init__( observance: Callable[..., DatetimeIndex] = None, start_date: TimestampConvertibleTypes = None, end_date: TimestampConvertibleTypes = None, - days_of_week: Tuple[int, ...] = None, + days_of_week: tuple[int, ...] = None, ): """ Parameters @@ -261,10 +259,10 @@ def __repr__(self) -> str: return repr def dates( - self, - start_date: TimestampConvertibleTypes, - end_date: TimestampConvertibleTypes, - return_name: bool = False, + self, + start_date: TimestampConvertibleTypes, + end_date: TimestampConvertibleTypes, + return_name: bool = False, ): """ Calculate holidays observed between start date and end date @@ -313,9 +311,7 @@ def dates( return holiday_dates def _reference_dates( - self, - start_date: TimestampConvertibleTypes, - end_date: TimestampConvertibleTypes + self, start_date: TimestampConvertibleTypes, end_date: TimestampConvertibleTypes ): """ Get reference dates for the holiday. @@ -448,10 +444,10 @@ def rule_from_name(self, name): return None def holidays( - self, - start: TimestampConvertibleTypes = None, - end: TimestampConvertibleTypes = None, - return_name: bool = False + self, + start: TimestampConvertibleTypes = None, + end: TimestampConvertibleTypes = None, + return_name: bool = False ) -> DatetimeIndex: """ Returns a curve with holidays between start_date and end_date