2Tests the warnings.deprecated function.
5# pyright: reportDeprecated=true
7# Specification: https://typing.readthedocs.io/en/latest/spec/directives.html#deprecated
8# See also https://peps.python.org/pep-0702/
10from typing import Protocol, override
11from typing_extensions import deprecated
13# > Type checkers should produce a diagnostic whenever they encounter a usage of an object
14# > marked as deprecated. [...] For deprecated classes and functions, this includes:
18from _directives_deprecated_library import Ham # E: Use of deprecated class Ham
[deprecated] The class `Ham` is deprecated: Use Spam instead
19import _directives_deprecated_library as library
22# > * References through module, class, or instance attributes
24library.norwegian_blue(1) # E: Use of deprecated function norwegian_blue
[deprecated] The function `norwegian_blue` is deprecated: It is pining for the fjords
25map(library.norwegian_blue, [1, 2, 3]) # E: Use of deprecated function norwegian_blue
[deprecated] The function `norwegian_blue` is deprecated: It is pining for the fjords
28# > For deprecated overloads, this includes all calls that resolve to the deprecated overload.
30library.foo(1) # E: Use of deprecated overload for foo
Expected a ty diagnostic for this line
34ham = Ham() # E?: OK (already reported above)
[deprecated] The class `Ham` is deprecated: Use Spam instead
37# > * Any syntax that indirectly triggers a call to the function.
41_ = spam + 1 # E: Use of deprecated method Spam.__add__
Expected a ty diagnostic for this line
42spam += 1 # E: Use of deprecated method Spam.__add__
Expected a ty diagnostic for this line
44spam.greasy # E: Use of deprecated property Spam.greasy
Expected a ty diagnostic for this line
47spam.shape = "cube" # E: Use of deprecated property setter Spam.shape
Expected a ty diagnostic for this line
48spam.shape += "cube" # E: Use of deprecated property setter Spam.shape
Expected a ty diagnostic for this line
52 @deprecated("Deprecated")
53 def __call__(self) -> None:
57invocable = Invocable()
58invocable() # E: Use of deprecated method __call__
Expected a ty diagnostic for this line
61# > * Any usage of deprecated objects in their defining module
64@deprecated("Deprecated")
69lorem() # E: Use of deprecated function lorem
[deprecated] The function `lorem` is deprecated: Deprecated
72# > There are additional scenarios where deprecations could come into play.
73# > For example, an object may implement a `typing.Protocol`,
74# > but one of the methods required for protocol compliance is deprecated.
75# > As scenarios such as this one appear complex and relatively unlikely to come up in practice,
76# > this PEP does not mandate that type checkers detect them.
79class SupportsFoo1(Protocol):
80 @deprecated("Deprecated")
81 def foo(self) -> None:
84 def bar(self) -> None:
88class FooConcrete1(SupportsFoo1):
90 def foo(self) -> None: # E?: Implementation of deprecated method foo
93 def bar(self) -> None:
97def foo_it(f: SupportsFoo1) -> None:
98 f.foo() # E: Use of deprecated method foo
[deprecated] The function `foo` is deprecated: Deprecated
102class SupportsFoo2(Protocol):
103 def foo(self) -> None:
108 @deprecated("Deprecated")
109 def foo(self) -> None:
113def takes_foo(f: SupportsFoo2) -> None:
117def caller(c: FooConcrete2) -> None:
120 ) # E?: FooConcrete2 is a SupportsFoo2, but only because of a deprecated method