Skip to content

Commit 1a40953

Browse files
committed
refactor: merge check_deprecated_function and check_deprecated_class into one method (check_deprecated)
1 parent 6a7dfe0 commit 1a40953

File tree

2 files changed

+11
-14
lines changed

2 files changed

+11
-14
lines changed

mypy/checker.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2954,7 +2954,7 @@ def visit_assignment_stmt(self, s: AssignmentStmt) -> None:
29542954
and isinstance(var := lvalue.node, Var)
29552955
and isinstance(instance := get_proper_type(var.type), Instance)
29562956
):
2957-
self.check_deprecated_class(instance.type, s)
2957+
self.check_deprecated(instance.type, s)
29582958

29592959
# Avoid type checking type aliases in stubs to avoid false
29602960
# positives about modern type syntax available in stubs such
@@ -7596,14 +7596,8 @@ def get_deprecation_warning(
75967596
return f"{name} is deprecated: {deprecation}"
75977597
return f"{name} is deprecated [overload {typ}]: {deprecation}"
75987598

7599-
def check_deprecated_function(self, typ: Type, context: Context) -> None:
7600-
if isinstance(typ := get_proper_type(typ), (CallableType, Overloaded)):
7601-
self._check_deprecated(typ, context)
7602-
7603-
def check_deprecated_class(self, typ: TypeInfo, context: Context) -> None:
7604-
self._check_deprecated(typ, context)
7605-
7606-
def _check_deprecated(self, typ: CallableType | Overloaded | TypeInfo, context: Context) -> None:
7599+
def check_deprecated(self, typ: CallableType | Overloaded | TypeInfo, context: Context) -> None:
7600+
"""Warn if deprecated and not directly imported with a `from` statement."""
76077601
if typ.deprecated is not None:
76087602
for imp in self.tree.imports:
76097603
if isinstance(imp, ImportFrom) and any(typ.name == n[0] for n in imp.names):
@@ -7614,6 +7608,7 @@ def _check_deprecated(self, typ: CallableType | Overloaded | TypeInfo, context:
76147608
def warn_deprecated(
76157609
self, typ: CallableType | Overloaded | TypeInfo, context: Context
76167610
) -> None:
7611+
"""Warn if deprecated."""
76177612
if (deprecated := typ.deprecated) is not None:
76187613
warn = self.msg.fail if self.options.report_deprecated_as_error else self.msg.note
76197614
warn(deprecated, context, code=codes.DEPRECATED)

mypy/checkexpr.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -356,9 +356,9 @@ def visit_name_expr(self, e: NameExpr) -> Type:
356356
result = self.analyze_ref_expr(e)
357357
narrowed = self.narrow_type_from_binder(e, result)
358358
if isinstance(e.node, TypeInfo):
359-
self.chk.check_deprecated_class(e.node, e)
360-
else:
361-
self.chk.check_deprecated_function(narrowed, e)
359+
self.chk.check_deprecated(e.node, e)
360+
elif isinstance(typ := get_proper_type(narrowed), (CallableType, Overloaded)):
361+
self.chk.check_deprecated(typ, e)
362362
return narrowed
363363

364364
def analyze_ref_expr(self, e: RefExpr, lvalue: bool = False) -> Type:
@@ -1479,8 +1479,9 @@ def check_call_expr_with_callee_type(
14791479
callable_name=callable_name,
14801480
object_type=object_type,
14811481
)
1482-
self.chk.check_deprecated_function(callee_type, e)
14831482
proper_callee = get_proper_type(callee_type)
1483+
if isinstance(proper_callee, (CallableType, Overloaded)):
1484+
self.chk.check_deprecated(proper_callee, e)
14841485
if isinstance(e.callee, RefExpr) and isinstance(proper_callee, CallableType):
14851486
# Cache it for find_isinstance_check()
14861487
if proper_callee.type_guard is not None:
@@ -3488,7 +3489,8 @@ def visit_op_expr(self, e: OpExpr) -> Type:
34883489
else:
34893490
assert_never(use_reverse)
34903491
e.method_type = method_type
3491-
self.chk.check_deprecated_function(method_type, e)
3492+
if isinstance(mt := get_proper_type(method_type), (CallableType, Overloaded)):
3493+
self.chk.check_deprecated(mt, e)
34923494
return result
34933495
else:
34943496
raise RuntimeError(f"Unknown operator {e.op}")

0 commit comments

Comments
 (0)