Skip to content

Add basic support for PEP 702 (@deprecated). #17476

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 40 commits into from
Oct 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
f04f4e3
Add basic support for PEP 702 (@deprecated).
tyralla Jul 3, 2024
973bf2d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 3, 2024
a88f4b4
use `type: ignore[deprecated]` when importing `abstractproperty`
tyralla Jul 3, 2024
91612c9
Merge remote-tracking branch 'mypypy/feature/support_deprecated' into…
tyralla Jul 3, 2024
faa4911
accept deprecated.args >= 1
tyralla Jul 4, 2024
101e9b8
": " instead of " - "
tyralla Jul 4, 2024
e3dfacb
only consider `warnings.deprecated` and `typing_extensions.deprecated…
tyralla Jul 4, 2024
cbf7574
note instead of error
tyralla Jul 4, 2024
9a947a5
remove walrusses
tyralla Jul 4, 2024
6d92318
document the new option
tyralla Jul 4, 2024
afa0336
motivate the semantic analyzer
tyralla Jul 4, 2024
7bfb534
`report-deprecated-as-error` instead of `warn-deprecated` and three n…
tyralla Jul 4, 2024
1042c65
fix a typo in error_code_list2.rst
tyralla Jul 4, 2024
978b1a2
additional note when PEP 702 is unsatisfied with the order of `@depre…
tyralla Jul 4, 2024
b0ced07
mention the affected overload
tyralla Jul 4, 2024
a07fc64
Merge remote-tracking branch 'mypypy/feature/support_deprecated' into…
tyralla Jul 4, 2024
b527250
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 4, 2024
f636024
add an annotation required by mypyc
tyralla Jul 4, 2024
63a725e
Merge remote-tracking branch 'mypypy/feature/support_deprecated' into…
tyralla Jul 4, 2024
966ac8b
refactor: create the whole deprecation warning in one place
tyralla Jul 5, 2024
6a7dfe0
refactor: get rid of the `memberaccess` parameter
tyralla Jul 5, 2024
1a40953
refactor: merge `check_deprecated_function` and `check_deprecated_cla…
tyralla Jul 5, 2024
1372e66
refactor: convert `get_deprecation_warning` to `create_deprecation_wa…
tyralla Jul 5, 2024
286371f
prefix the warnings with `class ...`, `function ...`, or `overload ..…
tyralla Jul 5, 2024
6f54dab
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 5, 2024
8c0260e
Consider `CallableType.deprecated` in `__hash__`, `__eq__`, `serializ…
tyralla Aug 27, 2024
cf2dcaf
Add a few fine-grained tests (of which `testAddFunctionDeprecationInd…
tyralla Aug 27, 2024
6a93d6a
Merge branch 'feature/support_deprecated' of https://github.com/tyral…
tyralla Aug 27, 2024
a6d0e59
Move the complete creation process of warning notes from `checker.py`…
tyralla Sep 22, 2024
6163787
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 22, 2024
49ee7a8
Move the `deprecation` attribute from the nodes `CallableType` and `O…
tyralla Sep 28, 2024
250e171
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 28, 2024
09a53d5
Modify function`snapshot_symbol_table` by removing the "CrossRef" sp…
tyralla Sep 29, 2024
31c4296
`typ: SymbolNode` -> `node: SymbolNode`
tyralla Sep 29, 2024
90fb06d
`type_` -> typ
tyralla Sep 29, 2024
beed6a5
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 29, 2024
8eb2e77
Revert commit 09a53d5b (regarding `astdiff.py` but not `fine-grained.…
tyralla Oct 6, 2024
988bf5f
Merge branch 'master' into feature/support_deprecated
tyralla Oct 6, 2024
80ddc68
Add `testDeprecateFunctionAlreadyDecorated`.
tyralla Oct 6, 2024
d104f26
Add `deprecated` to the `Func` snapshot and adjust test `testDeprecat…
tyralla Oct 6, 2024
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
Prev Previous commit
Next Next commit
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Jul 3, 2024
commit 973bf2d1e5257977a3915dc578fda96c66f37c00
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
Loading