Skip to content

Commit

Permalink
Merge remote-tracking branch 'mypypy/feature/support_deprecated' into…
Browse files Browse the repository at this point in the history
… feature/support_deprecated
  • Loading branch information
tyralla committed Jul 3, 2024
2 parents a88f4b4 + 973bf2d commit 91612c9
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 31 deletions.
40 changes: 23 additions & 17 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,13 +184,13 @@
)
from mypy.types import (
ANY_STRATEGY,
DEPRECATED_TYPE_NAMES,
MYPYC_NATIVE_INT_NAMES,
OVERLOAD_NAMES,
AnyType,
BoolTypeQuery,
CallableType,
DeletedType,
DEPRECATED_TYPE_NAMES,
ErasedType,
FunctionLike,
Instance,
Expand Down Expand Up @@ -660,9 +660,9 @@ def _visit_overloaded_func_def(self, defn: OverloadedFuncDef) -> None:
if defn.impl:
defn.impl.accept(self)
if (
isinstance(impl := defn.impl, Decorator) and
isinstance(ct := impl.func.type, CallableType) and
((deprecated := ct.deprecated) is not None)
isinstance(impl := defn.impl, Decorator)
and isinstance(ct := impl.func.type, CallableType)
and ((deprecated := ct.deprecated) is not None)
):
if isinstance(ct := defn.type, (CallableType, Overloaded)):
ct.deprecated = deprecated
Expand Down Expand Up @@ -2855,9 +2855,8 @@ def visit_import_from(self, node: ImportFrom) -> None:
if (sym := self.globals.get(name)) is not None:
if isinstance(sym.node, TypeInfo) and ((depr := sym.node.deprecated) is not None):
self.warn_deprecated(sym.node, depr, node)
elif (
isinstance(co := get_proper_type(sym.type), (CallableType, Overloaded)) and
((depr := co.deprecated) is not None)
elif isinstance(co := get_proper_type(sym.type), (CallableType, Overloaded)) and (
(depr := co.deprecated) is not None
):
self.warn_deprecated(co, depr, node)
self.check_import(node)
Expand Down Expand Up @@ -2952,9 +2951,9 @@ def visit_assignment_stmt(self, s: AssignmentStmt) -> None:
if isinstance(s.rvalue, TempNode) and s.rvalue.no_rhs:
for lvalue in s.lvalues:
if (
isinstance(lvalue, NameExpr) and
isinstance(var := lvalue.node, Var) and
isinstance(instance := get_proper_type(var.type), Instance)
isinstance(lvalue, NameExpr)
and isinstance(var := lvalue.node, Var)
and isinstance(instance := get_proper_type(var.type), Instance)
):
self.check_deprecated_class(instance.type, s, False)

Expand Down Expand Up @@ -7560,11 +7559,14 @@ def get_expression_type(self, node: Expression, type_context: Type | None = None
def get_deprecation_warning(self, decorators: Iterable[Expression]) -> str | None:
for decorator in decorators:
if (
isinstance(decorator, CallExpr) and
isinstance(callee := decorator.callee, NameExpr) and
((callee.fullname in DEPRECATED_TYPE_NAMES) or (not callee.fullname and callee.name == "deprecated")) and
(len(args := decorator.args) == 1) and
isinstance(arg := args[0], StrExpr)
isinstance(decorator, CallExpr)
and isinstance(callee := decorator.callee, NameExpr)
and (
(callee.fullname in DEPRECATED_TYPE_NAMES)
or (not callee.fullname and callee.name == "deprecated")
)
and (len(args := decorator.args) == 1)
and isinstance(arg := args[0], StrExpr)
):
return arg.value
return None
Expand All @@ -7576,7 +7578,9 @@ def check_deprecated_function(self, typ: Type, context: Context, memberaccess: b
def check_deprecated_class(self, typ: TypeInfo, context: Context, memberaccess: bool) -> None:
self._check_deprecated(typ, context, memberaccess)

def _check_deprecated(self, typ: CallableType | Overloaded | TypeInfo, context: Context, memberaccess: bool) -> None:
def _check_deprecated(
self, typ: CallableType | Overloaded | TypeInfo, context: Context, memberaccess: bool
) -> None:
if (depr := typ.deprecated) is not None:
if memberaccess:
self.warn_deprecated(typ, depr, context)
Expand All @@ -7587,7 +7591,9 @@ def _check_deprecated(self, typ: CallableType | Overloaded | TypeInfo, context:
else:
self.warn_deprecated(typ, depr, context)

def warn_deprecated(self, type_: CallableType | Overloaded | TypeInfo, deprecated: str, context: Context) -> None:
def warn_deprecated(
self, type_: CallableType | Overloaded | TypeInfo, deprecated: str, context: Context
) -> None:
name = type_.name
if isinstance(type_, CallableType):
if (defn := type_.definition) is not None:
Expand Down
22 changes: 10 additions & 12 deletions mypy/checkmember.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,19 +319,17 @@ def analyze_instance_member_access(
getter = method.items[0]
assert isinstance(getter, Decorator)
if (
mx.is_lvalue and
(len(items := method.items) > 1) and
isinstance(setter := items[1], Decorator)
mx.is_lvalue
and (len(items := method.items) > 1)
and isinstance(setter := items[1], Decorator)
):
if (
isinstance(co := setter.func.type, (CallableType, Overloaded)) and
((deprecated := co.deprecated) is not None)
if isinstance(co := setter.func.type, (CallableType, Overloaded)) and (
(deprecated := co.deprecated) is not None
):
mx.chk.warn_deprecated(co, deprecated, mx.context)
return analyze_var(name, getter.var, typ, info, mx)
elif (
isinstance(co := method.type, (CallableType, Overloaded)) and
((deprecated := co.deprecated) is not None)
elif isinstance(co := method.type, (CallableType, Overloaded)) and (
(deprecated := co.deprecated) is not None
):
mx.chk.warn_deprecated(co, deprecated, mx.context)

Expand Down Expand Up @@ -790,9 +788,9 @@ def analyze_var(
typ = get_proper_type(typ)

if (
var.is_property and
isinstance(typ, CallableType) and
((deprecated := typ.deprecated) is not None)
var.is_property
and isinstance(typ, CallableType)
and ((deprecated := typ.deprecated) is not None)
):
mx.chk.warn_deprecated(typ, deprecated, mx.context)

Expand Down
2 changes: 1 addition & 1 deletion mypy/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3379,7 +3379,7 @@ def serialize(self) -> JsonDict:
if self.dataclass_transform_spec is not None
else None
),
"deprecated": self.deprecated,
"deprecated": self.deprecated,
}
return data

Expand Down
2 changes: 1 addition & 1 deletion mypy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2341,7 +2341,7 @@ def serialize(self) -> JsonDict:
return {
".class": "Overloaded",
"items": [t.serialize() for t in self.items],
"deprecated": self.deprecated
"deprecated": self.deprecated,
}

@classmethod
Expand Down

0 comments on commit 91612c9

Please sign in to comment.