Skip to content

Typeinterval part1 #46080

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 16 commits into from
Feb 26, 2022
Merged
Show file tree
Hide file tree
Changes from 11 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 pandas/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
PythonScalar = Union[str, int, float, bool]
DatetimeLikeScalar = Union["Period", "Timestamp", "Timedelta"]
PandasScalar = Union["Period", "Timestamp", "Timedelta", "Interval"]
Scalar = Union[PythonScalar, PandasScalar]
Scalar = Union[PythonScalar, PandasScalar, np.datetime64, np.timedelta64, datetime]
IntStrT = TypeVar("IntStrT", int, str)


Expand Down
2 changes: 1 addition & 1 deletion pandas/core/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,7 @@ def factorize(
else:
dtype = values.dtype
values = _ensure_data(values)
na_value: Scalar
na_value: Scalar | None

if original.dtype.kind in ["m", "M"]:
# Note: factorize_array will cast NaT bc it has a __int__
Expand Down
6 changes: 6 additions & 0 deletions pandas/core/arrays/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
)
import textwrap
from typing import (
Any,
Sequence,
TypeVar,
Union,
Expand Down Expand Up @@ -197,6 +198,11 @@ class IntervalArray(IntervalMixin, ExtensionArray):
can_hold_na = True
_na_value = _fill_value = np.nan

# To make mypy recognize the fields
_left: Any
_right: Any
_dtype: Any

# ---------------------------------------------------------------------
# Constructors

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/masked.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ def to_numpy(
self,
dtype: npt.DTypeLike | None = None,
copy: bool = False,
na_value: Scalar = lib.no_default,
na_value: Scalar | lib.NoDefault | libmissing.NAType = lib.no_default,
) -> np.ndarray:
"""
Convert to a NumPy Array.
Expand Down
6 changes: 4 additions & 2 deletions pandas/core/arrays/string_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,7 @@ def _str_replace(
return type(self)(result)

def _str_match(
self, pat: str, case: bool = True, flags: int = 0, na: Scalar = None
self, pat: str, case: bool = True, flags: int = 0, na: Scalar | None = None
):
if pa_version_under4p0:
return super()._str_match(pat, case, flags, na)
Expand All @@ -771,7 +771,9 @@ def _str_match(
pat = "^" + pat
return self._str_contains(pat, case, flags, na, regex=True)

def _str_fullmatch(self, pat, case: bool = True, flags: int = 0, na: Scalar = None):
def _str_fullmatch(
self, pat, case: bool = True, flags: int = 0, na: Scalar | None = None
):
if pa_version_under4p0:
return super()._str_fullmatch(pat, case, flags, na)

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ def f(x):


def convert_to_list_like(
values: Scalar | Iterable | AnyArrayLike,
values: Scalar | Iterable | AnyArrayLike | Hashable,
) -> list | AnyArrayLike:
"""
Convert list-like or scalar input to list-like. List, numpy and pandas array-like
Expand Down
7 changes: 5 additions & 2 deletions pandas/core/indexes/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from typing import (
Any,
Hashable,
Literal,
)

import numpy as np
Expand Down Expand Up @@ -191,10 +192,12 @@ class IntervalIndex(ExtensionIndex):
_typ = "intervalindex"

# annotate properties pinned via inherit_names
closed: str
closed: Literal["left", "right", "both", "neither"]
is_non_overlapping_monotonic: bool
closed_left: bool
closed_right: bool
open_left: bool
open_right: bool

_data: IntervalArray
_values: IntervalArray
Expand Down Expand Up @@ -543,7 +546,7 @@ def _maybe_convert_i8(self, key):

return key_i8

def _searchsorted_monotonic(self, label, side: str = "left"):
def _searchsorted_monotonic(self, label, side: Literal["left", "right"] = "left"):
if not self.is_non_overlapping_monotonic:
raise KeyError(
"can only get slices from an IntervalIndex if bounds are "
Expand Down
15 changes: 12 additions & 3 deletions pandas/core/tools/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
Hashable,
List,
Tuple,
TypeVar,
Union,
cast,
overload,
)
import warnings
Expand Down Expand Up @@ -66,6 +66,7 @@
)
from pandas.core import algorithms
from pandas.core.algorithms import unique
from pandas.core.arrays.base import ExtensionArray
from pandas.core.arrays.datetimes import (
maybe_convert_dtype,
objects_to_datetime64ns,
Expand All @@ -85,7 +86,8 @@

ArrayConvertible = Union[List, Tuple, AnyArrayLike, "Series"]
Scalar = Union[int, float, str]
DatetimeScalar = TypeVar("DatetimeScalar", Scalar, datetime)
DatetimeScalar = Union[Scalar, datetime]

DatetimeScalarOrArrayConvertible = Union[DatetimeScalar, ArrayConvertible]
start_caching_at = 50

Expand Down Expand Up @@ -638,7 +640,7 @@ def to_datetime(
infer_datetime_format: bool = ...,
origin=...,
cache: bool = ...,
) -> DatetimeScalar | NaTType:
) -> Timestamp | NaTType:
...


Expand Down Expand Up @@ -1061,6 +1063,13 @@ def to_datetime(
result = convert_listlike(arg, format, name=arg.name)
elif is_list_like(arg):
try:
# error: Argument 1 to "_maybe_cache" has incompatible type
# "Union[float, str, datetime, List[Any], Tuple[Any, ...], ExtensionArray,
# ndarray[Any, Any], Series]"; expected "Union[List[Any], Tuple[Any, ...],
# Union[Union[ExtensionArray, ndarray[Any, Any]], Index, Series], Series]"
arg = cast(
Union[list, tuple, ExtensionArray, np.ndarray, "Series", Index], arg
)
cache_array = _maybe_cache(arg, format, cache, convert_listlike)
except OutOfBoundsDatetime:
# caching attempts to create a DatetimeIndex, which may raise
Expand Down
20 changes: 13 additions & 7 deletions pandas/io/excel/_odfreader.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import numpy as np

from pandas._libs.tslibs.nattype import NaTType
from pandas._typing import (
FilePath,
ReadBuffer,
Expand Down Expand Up @@ -81,7 +82,9 @@ def get_sheet_by_name(self, name: str):
self.close()
raise ValueError(f"sheet {name} not found")

def get_sheet_data(self, sheet, convert_float: bool) -> list[list[Scalar]]:
def get_sheet_data(
self, sheet, convert_float: bool
) -> list[list[Scalar | NaTType]]:
"""
Parse an ODF Table into a list of lists
"""
Expand All @@ -99,12 +102,12 @@ def get_sheet_data(self, sheet, convert_float: bool) -> list[list[Scalar]]:
empty_rows = 0
max_row_len = 0

table: list[list[Scalar]] = []
table: list[list[Scalar | NaTType]] = []

for sheet_row in sheet_rows:
sheet_cells = [x for x in sheet_row.childNodes if x.qname in cell_names]
empty_cells = 0
table_row: list[Scalar] = []
table_row: list[Scalar | NaTType] = []

for sheet_cell in sheet_cells:
if sheet_cell.qname == table_cell_name:
Expand Down Expand Up @@ -167,7 +170,7 @@ def _is_empty_row(self, row) -> bool:

return True

def _get_cell_value(self, cell, convert_float: bool) -> Scalar:
def _get_cell_value(self, cell, convert_float: bool) -> Scalar | NaTType:
from odf.namespaces import OFFICENS

if str(cell) == "#N/A":
Expand Down Expand Up @@ -200,9 +203,12 @@ def _get_cell_value(self, cell, convert_float: bool) -> Scalar:
cell_value = cell.attributes.get((OFFICENS, "date-value"))
return pd.to_datetime(cell_value)
elif cell_type == "time":
stamp = pd.to_datetime(str(cell))
# error: Item "str" of "Union[float, str, NaTType]" has no attribute "time"
return stamp.time() # type: ignore[union-attr]
stamp: pd.Timestamp | NaTType = pd.to_datetime(str(cell))
if not isinstance(stamp, NaTType):
return stamp.time()
else:
self.close()
raise ValueError(f"Unrecognized time {str(cell)}")
Copy link
Member

Choose a reason for hiding this comment

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

If changing code in a typing PR, I have some questions...

does it change behavior?
has typing found a latent bug?
is a release note needed?
do we need some additional tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Given that it is a time, the other solution is to just do a cast to Timestamp since we know the result won't be a NaTType . I will revert to that with a comment

else:
self.close()
raise ValueError(f"Unrecognized type {cell_type}")
Expand Down
Loading