Skip to content

Commit d3957d0

Browse files
committed
turn the is_notimplemented and erase_notimplemented into normal functions
1 parent c04458c commit d3957d0

File tree

3 files changed

+22
-20
lines changed

3 files changed

+22
-20
lines changed

mypy/checker.py

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@
178178
coerce_to_literal,
179179
custom_special_method,
180180
erase_def_to_union_or_bound,
181+
erase_notimplemented,
181182
erase_to_bound,
182183
erase_to_union_or_bound,
183184
false_only,
@@ -4894,21 +4895,6 @@ def infer_context_dependent(
48944895
self.store_types(original_type_map)
48954896
return typ
48964897

4897-
@staticmethod
4898-
def is_notimplemented(t: ProperType) -> bool:
4899-
return isinstance(t, Instance) and t.type.fullname == "builtins._NotImplementedType"
4900-
4901-
@classmethod
4902-
def erase_notimplemented(cls, t: Type) -> Type:
4903-
t = get_proper_type(t)
4904-
if cls.is_notimplemented(t):
4905-
return AnyType(TypeOfAny.special_form)
4906-
if isinstance(t, UnionType):
4907-
return UnionType.make_union(
4908-
[i for i in t.items if not cls.is_notimplemented(get_proper_type(i))]
4909-
)
4910-
return t
4911-
49124898
def check_return_stmt(self, s: ReturnStmt) -> None:
49134899

49144900
defn = self.scope.current_function()
@@ -4991,7 +4977,7 @@ def check_return_stmt(self, s: ReturnStmt) -> None:
49914977
else:
49924978
typ_: Type = typ
49934979
if defn.name in BINARY_MAGIC_METHODS or defn.name == "__subclasshook__":
4994-
typ_ = self.erase_notimplemented(typ)
4980+
typ_ = erase_notimplemented(typ)
49954981
self.check_subtype(
49964982
subtype_label="got",
49974983
subtype=typ_,

mypy/checkexpr.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@
132132
from mypy.typeops import (
133133
callable_type,
134134
custom_special_method,
135+
erase_notimplemented,
135136
erase_to_union_or_bound,
136137
false_only,
137138
fixup_partial_type,
@@ -3554,7 +3555,7 @@ def visit_op_expr(self, e: OpExpr) -> Type:
35543555
else:
35553556
assert_never(use_reverse)
35563557
e.method_type = method_type
3557-
return self.chk.erase_notimplemented(result)
3558+
return erase_notimplemented(result)
35583559
else:
35593560
raise RuntimeError(f"Unknown operator {e.op}")
35603561

@@ -3705,7 +3706,7 @@ def visit_comparison_expr(self, e: ComparisonExpr) -> Type:
37053706
result = join.join_types(result, sub_result)
37063707

37073708
assert result is not None
3708-
return self.chk.erase_notimplemented(result)
3709+
return erase_notimplemented(result)
37093710

37103711
def find_partial_type_ref_fast_path(self, expr: Expression) -> Type | None:
37113712
"""If expression has a partial generic type, return it without additional checks.
@@ -4228,7 +4229,7 @@ def check_op(
42284229
# callable types.
42294230
results_final = make_simplified_union(all_results)
42304231
inferred_final = self.combine_function_signatures(get_proper_types(all_inferred))
4231-
return self.chk.erase_notimplemented(results_final), inferred_final
4232+
return erase_notimplemented(results_final), inferred_final
42324233
else:
42334234
result, inferred = self.check_method_call_by_name(
42344235
method=method,
@@ -4237,7 +4238,7 @@ def check_op(
42374238
arg_kinds=[ARG_POS],
42384239
context=context,
42394240
)
4240-
return self.chk.erase_notimplemented(result), inferred
4241+
return erase_notimplemented(result), inferred
42414242

42424243
def check_boolean_op(self, e: OpExpr) -> Type:
42434244
"""Type check a boolean operation ('and' or 'or')."""

mypy/typeops.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -999,6 +999,21 @@ def is_singleton_type(typ: Type) -> bool:
999999
return typ.is_singleton_type()
10001000

10011001

1002+
def is_notimplemented(t: ProperType) -> bool:
1003+
return isinstance(t, Instance) and t.type.fullname == "builtins._NotImplementedType"
1004+
1005+
1006+
def erase_notimplemented(t: Type) -> Type:
1007+
t = get_proper_type(t)
1008+
if is_notimplemented(t):
1009+
return AnyType(TypeOfAny.special_form)
1010+
if isinstance(t, UnionType):
1011+
return UnionType.make_union(
1012+
[i for i in t.items if not is_notimplemented(get_proper_type(i))]
1013+
)
1014+
return t
1015+
1016+
10021017
def try_expanding_sum_type_to_union(typ: Type, target_fullname: str) -> Type:
10031018
"""Attempts to recursively expand any enum Instances with the given target_fullname
10041019
into a Union of all of its component LiteralTypes.

0 commit comments

Comments
 (0)