Skip to content

Commit 06c5d24

Browse files
simonjayhawkinsjreback
authored andcommitted
TYP: enable strict_equality to prohibit comparisons of non-overlappin… (#30619)
1 parent 9c730c1 commit 06c5d24

File tree

7 files changed

+16
-33
lines changed

7 files changed

+16
-33
lines changed

pandas/core/arrays/boolean.py

+2-13
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ class BooleanDtype(ExtensionDtype):
6262
BooleanDtype
6363
"""
6464

65+
name = "boolean"
66+
6567
@property
6668
def na_value(self) -> "Scalar":
6769
"""
@@ -81,19 +83,6 @@ def type(self) -> Type:
8183
def kind(self) -> str:
8284
return "b"
8385

84-
@property
85-
def name(self) -> str:
86-
"""
87-
The alias for BooleanDtype is ``'boolean'``.
88-
"""
89-
return "boolean"
90-
91-
@classmethod
92-
def construct_from_string(cls, string: str) -> ExtensionDtype:
93-
if string == "boolean":
94-
return cls()
95-
return super().construct_from_string(string)
96-
9786
@classmethod
9887
def construct_array_type(cls) -> "Type[BooleanArray]":
9988
return BooleanArray

pandas/core/arrays/period.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ def _check_compatible_with(self, other):
332332
def dtype(self):
333333
return self._dtype
334334

335-
# read-only property overwriting read/write
335+
# error: Read-only property cannot override read-write property [misc]
336336
@property # type: ignore
337337
def freq(self):
338338
"""
@@ -654,7 +654,7 @@ def _sub_period(self, other):
654654
return new_data
655655

656656
def _addsub_int_array(
657-
self, other: np.ndarray, op: Callable[[Any], Any],
657+
self, other: np.ndarray, op: Callable[[Any, Any], Any],
658658
) -> "PeriodArray":
659659
"""
660660
Add or subtract array of integers; equivalent to applying

pandas/core/arrays/string_.py

+2-13
Original file line numberDiff line numberDiff line change
@@ -47,26 +47,15 @@ class StringDtype(ExtensionDtype):
4747
StringDtype
4848
"""
4949

50+
name = "string"
51+
5052
#: StringDtype.na_value uses pandas.NA
5153
na_value = libmissing.NA
5254

5355
@property
5456
def type(self) -> Type:
5557
return str
5658

57-
@property
58-
def name(self) -> str:
59-
"""
60-
The alias for StringDtype is ``'string'``.
61-
"""
62-
return "string"
63-
64-
@classmethod
65-
def construct_from_string(cls, string: str) -> ExtensionDtype:
66-
if string == "string":
67-
return cls()
68-
return super().construct_from_string(string)
69-
7059
@classmethod
7160
def construct_array_type(cls) -> "Type[StringArray]":
7261
return StringArray

pandas/core/dtypes/base.py

+4
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,10 @@ def construct_from_string(cls, string: str):
236236
"""
237237
if not isinstance(string, str):
238238
raise TypeError(f"Expects a string, got {type(string).__name__}")
239+
240+
# error: Non-overlapping equality check (left operand type: "str", right
241+
# operand type: "Callable[[ExtensionDtype], str]") [comparison-overlap]
242+
assert isinstance(cls.name, str), (cls, type(cls.name))
239243
if string != cls.name:
240244
raise TypeError(f"Cannot construct a '{cls.__name__}' from '{string}'")
241245
return cls()

pandas/io/excel/_odfreader.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def _get_cell_value(self, cell, convert_float: bool) -> Scalar:
156156
# GH5394
157157
cell_value = float(cell.attributes.get((OFFICENS, "value")))
158158

159-
if cell_value == 0.0 and str(cell) != cell_value: # NA handling
159+
if cell_value == 0.0: # NA handling
160160
return str(cell)
161161

162162
if convert_float:

pandas/io/formats/format.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ def __init__(
231231
self,
232232
series: "Series",
233233
buf: Optional[IO[str]] = None,
234-
length: bool = True,
234+
length: Union[bool, str] = True,
235235
header: bool = True,
236236
index: bool = True,
237237
na_rep: str = "NaN",
@@ -450,7 +450,7 @@ def _get_adjustment() -> TextAdjustment:
450450

451451
class TableFormatter:
452452

453-
show_dimensions: bool
453+
show_dimensions: Union[bool, str]
454454
is_truncated: bool
455455
formatters: formatters_type
456456
columns: Index
@@ -554,7 +554,7 @@ def __init__(
554554
max_rows: Optional[int] = None,
555555
min_rows: Optional[int] = None,
556556
max_cols: Optional[int] = None,
557-
show_dimensions: bool = False,
557+
show_dimensions: Union[bool, str] = False,
558558
decimal: str = ".",
559559
table_id: Optional[str] = None,
560560
render_links: bool = False,
@@ -1276,7 +1276,7 @@ class FloatArrayFormatter(GenericArrayFormatter):
12761276
"""
12771277

12781278
def __init__(self, *args, **kwargs):
1279-
GenericArrayFormatter.__init__(self, *args, **kwargs)
1279+
super().__init__(*args, **kwargs)
12801280

12811281
# float_format is expected to be a string
12821282
# formatter should be used to pass a function

setup.cfg

+1
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ skip = pandas/__init__.py,pandas/core/api.py
123123
ignore_missing_imports=True
124124
no_implicit_optional=True
125125
check_untyped_defs=True
126+
strict_equality=True
126127

127128
[mypy-pandas.tests.*]
128129
check_untyped_defs=False

0 commit comments

Comments
 (0)