@@ -31,6 +31,10 @@ use crate::settings::types::PythonVersion;
31
31
/// ## Example
32
32
///
33
33
/// ```pyi
34
+ /// from typing import TypeVar
35
+ ///
36
+ /// _S = TypeVar("_S", bound="Foo")
37
+ ///
34
38
/// class Foo:
35
39
/// def __new__(cls: type[_S], *args: str, **kwargs: int) -> _S: ...
36
40
/// def foo(self: _S, arg: bytes) -> _S: ...
@@ -51,13 +55,21 @@ use crate::settings::types::PythonVersion;
51
55
/// ```
52
56
///
53
57
/// ## Fix behaviour and safety
54
- /// The fix removes all usages and declarations of the custom type variable.
55
- /// [PEP-695]-style `TypeVar` declarations are also removed from the [type parameter list];
56
- /// however, old-style `TypeVar`s do not have their declarations removed. See
57
- /// [`unused-private-type-var`][PYI018] for a rule to clean up unused private type variables.
58
+ /// The fix replaces all references to the custom type variable in the method's header and body
59
+ /// with references to `Self`. The fix also adds an import of `Self` if neither `Self` nor `typing`
60
+ /// is already imported in the module. If your [`target-version`] setting is set to Python 3.11 or
61
+ /// newer, the fix imports `Self` from the standard-library `typing` module; otherwise, the fix
62
+ /// imports `Self` from the third-party [`typing_extensions`][typing_extensions] backport package.
63
+ ///
64
+ /// If the custom type variable is a [PEP-695]-style `TypeVar`, the fix also removes the `TypeVar`
65
+ /// declaration from the method's [type parameter list]. However, if the type variable is an
66
+ /// old-style `TypeVar`, the declaration of the type variable will not be removed by this rule's
67
+ /// fix, as the type variable could still be used by other functions, methods or classes. See
68
+ /// [`unused-private-type-var`][PYI018] for a rule that will clean up unused private type
69
+ /// variables.
58
70
///
59
- /// If there are any comments within the fix ranges, it will be marked as unsafe.
60
- /// Otherwise, it will be marked as safe .
71
+ /// The fix is only marked as unsafe if there is the possibility that it might delete a comment
72
+ /// from your code .
61
73
///
62
74
/// ## Preview-mode behaviour
63
75
/// This rule's behaviour has several differences when [`preview`] mode is enabled:
@@ -77,6 +89,7 @@ use crate::settings::types::PythonVersion;
77
89
/// [type parameter list]: https://docs.python.org/3/reference/compound_stmts.html#type-params
78
90
/// [Self]: https://docs.python.org/3/library/typing.html#typing.Self
79
91
/// [typing_TypeVar]: https://docs.python.org/3/library/typing.html#typing.TypeVar
92
+ /// [typing_extensions]: https://typing-extensions.readthedocs.io/en/latest/
80
93
#[ derive( ViolationMetadata ) ]
81
94
pub ( crate ) struct CustomTypeVarForSelf {
82
95
typevar_name : String ,
0 commit comments