Skip to content

Commit 016c868

Browse files
authored
TYP: resolve mypy errors in core/arrays/_ranges.py (#52943)
* Cast return values to int * Remove mypy error comment * Add return statement to function * Change function signatures * Add type annotation to variables
1 parent 8ef94a0 commit 016c868

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

pandas/core/arrays/_ranges.py

+11-15
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ def generate_regular_range(
5454
iend = end._value if end is not None else None
5555
freq.nanos # raises if non-fixed frequency
5656
td = Timedelta(freq)
57+
b: int | np.int64 | np.uint64
58+
e: int | np.int64 | np.uint64
5759
try:
5860
td = td.as_unit( # pyright: ignore[reportGeneralTypeIssues]
5961
unit, round_ok=False
@@ -96,7 +98,7 @@ def generate_regular_range(
9698

9799
def _generate_range_overflow_safe(
98100
endpoint: int, periods: int, stride: int, side: str = "start"
99-
) -> int:
101+
) -> np.int64 | np.uint64:
100102
"""
101103
Calculate the second endpoint for passing to np.arange, checking
102104
to avoid an integer overflow. Catch OverflowError and re-raise
@@ -115,7 +117,7 @@ def _generate_range_overflow_safe(
115117
116118
Returns
117119
-------
118-
other_end : int
120+
other_end : np.int64 | np.uint64
119121
120122
Raises
121123
------
@@ -157,13 +159,13 @@ def _generate_range_overflow_safe(
157159
remaining = periods - mid_periods
158160
assert 0 < remaining < periods, (remaining, periods, endpoint, stride)
159161

160-
midpoint = _generate_range_overflow_safe(endpoint, mid_periods, stride, side)
162+
midpoint = int(_generate_range_overflow_safe(endpoint, mid_periods, stride, side))
161163
return _generate_range_overflow_safe(midpoint, remaining, stride, side)
162164

163165

164166
def _generate_range_overflow_safe_signed(
165167
endpoint: int, periods: int, stride: int, side: str
166-
) -> int:
168+
) -> np.int64 | np.uint64:
167169
"""
168170
A special case for _generate_range_overflow_safe where `periods * stride`
169171
can be calculated without overflowing int64 bounds.
@@ -181,9 +183,7 @@ def _generate_range_overflow_safe_signed(
181183
# Putting this into a DatetimeArray/TimedeltaArray
182184
# would incorrectly be interpreted as NaT
183185
raise OverflowError
184-
# error: Incompatible return value type (got "signedinteger[_64Bit]",
185-
# expected "int")
186-
return result # type: ignore[return-value]
186+
return result
187187
except (FloatingPointError, OverflowError):
188188
# with endpoint negative and addend positive we risk
189189
# FloatingPointError; with reversed signed we risk OverflowError
@@ -198,15 +198,11 @@ def _generate_range_overflow_safe_signed(
198198
# exceed implementation bounds, but when passing the result to
199199
# np.arange will get a result slightly within the bounds
200200

201-
# error: Incompatible types in assignment (expression has type
202-
# "unsignedinteger[_64Bit]", variable has type "signedinteger[_64Bit]")
203-
result = np.uint64(endpoint) + np.uint64(addend) # type: ignore[assignment]
201+
uresult = np.uint64(endpoint) + np.uint64(addend)
204202
i64max = np.uint64(i8max)
205-
assert result > i64max
206-
if result <= i64max + np.uint64(stride):
207-
# error: Incompatible return value type (got "unsignedinteger", expected
208-
# "int")
209-
return result # type: ignore[return-value]
203+
assert uresult > i64max
204+
if uresult <= i64max + np.uint64(stride):
205+
return uresult
210206

211207
raise OutOfBoundsDatetime(
212208
f"Cannot generate range with {side}={endpoint} and periods={periods}"

0 commit comments

Comments
 (0)