← Back to index

directives_deprecated.py

True Positive
False Positive
False Negative
Optional (detected)
Warning or Info
TP: 5
FP: 0
FN: 7
Optional: 1 / 3
1"""
2Tests the warnings.deprecated function.
3"""
4
5# pyright: reportDeprecated=true
6
7# Specification: https://typing.readthedocs.io/en/latest/spec/directives.html#deprecated
8# See also https://peps.python.org/pep-0702/
9
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:
16# > * `from` imports
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
31library.foo("x") # OK
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.
39spam = library.Spam()
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
45spam.shape # OK
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
51class Invocable:
52 @deprecated("Deprecated")
53 def __call__(self) -> None:
54 ...
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")
65def lorem() -> None:
66 ...
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:
82 ...
84 def bar(self) -> None:
85 ...
88class FooConcrete1(SupportsFoo1):
89 @override
90 def foo(self) -> None: # E?: Implementation of deprecated method foo
91 ...
93 def bar(self) -> None:
94 ...
97def foo_it(f: SupportsFoo1) -> None:
98 f.foo() # E: Use of deprecated method foo
[deprecated] The function `foo` is deprecated: Deprecated
99 f.bar()
102class SupportsFoo2(Protocol):
103 def foo(self) -> None:
104 ...
107class FooConcrete2:
108 @deprecated("Deprecated")
109 def foo(self) -> None:
110 ...
113def takes_foo(f: SupportsFoo2) -> None:
114 ...
117def caller(c: FooConcrete2) -> None:
118 takes_foo(
119 c
120 ) # E?: FooConcrete2 is a SupportsFoo2, but only because of a deprecated method