Skip to content

Commit

Permalink
stubtest: special case final and deprecated (#16457)
Browse files Browse the repository at this point in the history
We should probably lean into the type checker harder here

Fixes #14950
Fixes
python/typeshed#11009 (comment)
  • Loading branch information
hauntsaninja authored Nov 11, 2023
1 parent a7c53d3 commit 8ae84ed
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
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")

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

0 comments on commit 8ae84ed

Please sign in to comment.