Skip to content
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

stubtest: special case final and deprecated #16457

Merged
merged 1 commit into from
Nov 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions mypy/stubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1215,6 +1215,12 @@ def _resolve_funcitem_from_decorator(dec: nodes.OverloadPart) -> nodes.FuncItem
def apply_decorator_to_funcitem(
decorator: nodes.Expression, func: nodes.FuncItem
) -> nodes.FuncItem | None:
if (
isinstance(decorator, nodes.CallExpr)
and isinstance(decorator.callee, nodes.RefExpr)
and decorator.callee.fullname in mypy.types.DEPRECATED_TYPE_NAMES
):
return func
if not isinstance(decorator, nodes.RefExpr):
return None
if not decorator.fullname:
Expand All @@ -1223,6 +1229,7 @@ def apply_decorator_to_funcitem(
if (
decorator.fullname in ("builtins.staticmethod", "abc.abstractmethod")
or decorator.fullname in mypy.types.OVERLOAD_NAMES
or decorator.fullname in mypy.types.FINAL_DECORATOR_NAMES
):
return func
if decorator.fullname == "builtins.classmethod":
Expand Down
19 changes: 19 additions & 0 deletions mypy/test/teststubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ class Sequence(Iterable[_T_co]): ...
class Tuple(Sequence[_T_co]): ...
class NamedTuple(tuple[Any, ...]): ...
def overload(func: _T) -> _T: ...
def deprecated(__msg: str) -> Callable[[_T], _T]: ...
def final(func: _T) -> _T: ...
"""

stubtest_builtins_stub = """
Expand Down Expand Up @@ -630,6 +632,23 @@ def f5(__b: str) -> str: ...
runtime="def f5(x, /): pass",
error=None,
)
yield Case(
stub="""
from typing import deprecated, final
class Foo:
@overload
@final
def f6(self, __a: int) -> int: ...
@overload
@deprecated("evil")
def f6(self, __b: str) -> str: ...
""",
runtime="""
class Foo:
def f6(self, x, /): pass
""",
error=None,
)

@collect_cases
def test_property(self) -> Iterator[Case]:
Expand Down
3 changes: 3 additions & 0 deletions mypy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@
# Supported Annotated type names.
ANNOTATED_TYPE_NAMES: Final = ("typing.Annotated", "typing_extensions.Annotated")

# Supported @deprecated type names
DEPRECATED_TYPE_NAMES: Final = ("typing.deprecated", "typing_extensions.deprecated")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be warnings.deprecated, I'll send a PR.


# We use this constant in various places when checking `tuple` subtyping:
TUPLE_LIKE_INSTANCE_NAMES: Final = (
"builtins.tuple",
Expand Down