Skip to content

added typing IntervalClosedType where needed #52894

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 9 commits into from
Apr 25, 2023
2 changes: 1 addition & 1 deletion pandas/_libs/interval.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class Interval(IntervalMixin, Generic[_OrderableT]):

def intervals_to_interval_bounds(
intervals: np.ndarray, validate_closed: bool = ...
) -> tuple[np.ndarray, np.ndarray, str]: ...
) -> tuple[np.ndarray, np.ndarray, IntervalClosedType]: ...

class IntervalTree(IntervalMixin):
def __init__(
Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/interval.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ def intervals_to_interval_bounds(ndarray intervals, bint validate_closed=True):
tuple of
left : ndarray
right : ndarray
closed: str
closed: IntervalClosedType
"""
cdef:
object closed = None, interval
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ def ndim(self) -> Literal[1]:
def __new__(
cls,
data,
closed=None,
closed: IntervalClosedType | None = None,
dtype: Dtype | None = None,
copy: bool = False,
verify_integrity: bool = True,
Expand Down
5 changes: 3 additions & 2 deletions pandas/core/dtypes/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
from pandas._typing import (
Dtype,
DtypeObj,
IntervalClosedType,
Ordered,
npt,
type_t,
Expand Down Expand Up @@ -1099,7 +1100,7 @@ class IntervalDtype(PandasExtensionDtype):

_cache_dtypes: dict[str_type, PandasExtensionDtype] = {}

def __new__(cls, subtype=None, closed: str_type | None = None):
def __new__(cls, subtype=None, closed=None):
Copy link
Member

Choose a reason for hiding this comment

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

Could you add it here too?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried, but I have an issue with mypy if I do this: it complains about the variable reassignement l.1141:
closed = gd["closed"]

pandas/core/dtypes/dtypes.py:1141: error: Incompatible types in assignment (expression has type "Union[str, Any]", variable has type "Optional[Union[Literal['left', 'right'], Literal['both', 'neither']]]") [assignment]

Do you know if there is a way to fix this?

Copy link
Member

Choose a reason for hiding this comment

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

Adding # type: ignore[assignment] in 1141 would work here. This isn't ideal, but it's better if the function signature is typed more strictly

Copy link
Contributor Author

@gmollard gmollard Apr 25, 2023

Choose a reason for hiding this comment

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

I added what you suggested but I have a check that fails, I don't know why..
Do you have an idea of what's the reason?

from pandas.core.dtypes.common import (
is_string_dtype,
pandas_dtype,
Expand Down Expand Up @@ -1175,7 +1176,7 @@ def _can_hold_na(self) -> bool:
return True

@property
def closed(self):
def closed(self) -> IntervalClosedType:
return self._closed

@property
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ class IntervalIndex(ExtensionIndex):
def __new__(
cls,
data,
closed=None,
closed: IntervalClosedType | None = None,
dtype: Dtype | None = None,
copy: bool = False,
name: Hashable = None,
Expand Down
5 changes: 3 additions & 2 deletions pandas/io/formats/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
AxisInt,
FilePath,
IndexLabel,
IntervalClosedType,
Level,
QuantileInterpolation,
Scalar,
Expand Down Expand Up @@ -3185,7 +3186,7 @@ def highlight_between(
axis: Axis | None = 0,
left: Scalar | Sequence | None = None,
right: Scalar | Sequence | None = None,
inclusive: str = "both",
inclusive: IntervalClosedType = "both",
props: str | None = None,
) -> Styler:
"""
Expand Down Expand Up @@ -3294,7 +3295,7 @@ def highlight_quantile(
q_left: float = 0.0,
q_right: float = 1.0,
interpolation: QuantileInterpolation = "linear",
inclusive: str = "both",
inclusive: IntervalClosedType = "both",
props: str | None = None,
) -> Styler:
"""
Expand Down