Skip to content

Commit 66a0467

Browse files
authored
Improve docs for PYI019 (#16229)
1 parent 4ed5db0 commit 66a0467

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

crates/ruff_linter/src/rules/flake8_pyi/rules/custom_type_var_for_self.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ use crate::settings::types::PythonVersion;
3131
/// ## Example
3232
///
3333
/// ```pyi
34+
/// from typing import TypeVar
35+
///
36+
/// _S = TypeVar("_S", bound="Foo")
37+
///
3438
/// class Foo:
3539
/// def __new__(cls: type[_S], *args: str, **kwargs: int) -> _S: ...
3640
/// def foo(self: _S, arg: bytes) -> _S: ...
@@ -51,13 +55,21 @@ use crate::settings::types::PythonVersion;
5155
/// ```
5256
///
5357
/// ## 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.
5870
///
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.
6173
///
6274
/// ## Preview-mode behaviour
6375
/// This rule's behaviour has several differences when [`preview`] mode is enabled:
@@ -77,6 +89,7 @@ use crate::settings::types::PythonVersion;
7789
/// [type parameter list]: https://docs.python.org/3/reference/compound_stmts.html#type-params
7890
/// [Self]: https://docs.python.org/3/library/typing.html#typing.Self
7991
/// [typing_TypeVar]: https://docs.python.org/3/library/typing.html#typing.TypeVar
92+
/// [typing_extensions]: https://typing-extensions.readthedocs.io/en/latest/
8093
#[derive(ViolationMetadata)]
8194
pub(crate) struct CustomTypeVarForSelf {
8295
typevar_name: String,

0 commit comments

Comments
 (0)