mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-01 14:21:24 +00:00
Improve docs for PYI019 (#16229)
This commit is contained in:
parent
4ed5db0d42
commit
66a0467305
1 changed files with 19 additions and 6 deletions
|
@ -31,6 +31,10 @@ use crate::settings::types::PythonVersion;
|
||||||
/// ## Example
|
/// ## Example
|
||||||
///
|
///
|
||||||
/// ```pyi
|
/// ```pyi
|
||||||
|
/// from typing import TypeVar
|
||||||
|
///
|
||||||
|
/// _S = TypeVar("_S", bound="Foo")
|
||||||
|
///
|
||||||
/// class Foo:
|
/// class Foo:
|
||||||
/// def __new__(cls: type[_S], *args: str, **kwargs: int) -> _S: ...
|
/// def __new__(cls: type[_S], *args: str, **kwargs: int) -> _S: ...
|
||||||
/// def foo(self: _S, arg: bytes) -> _S: ...
|
/// def foo(self: _S, arg: bytes) -> _S: ...
|
||||||
|
@ -51,13 +55,21 @@ use crate::settings::types::PythonVersion;
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// ## Fix behaviour and safety
|
/// ## Fix behaviour and safety
|
||||||
/// The fix removes all usages and declarations of the custom type variable.
|
/// The fix replaces all references to the custom type variable in the method's header and body
|
||||||
/// [PEP-695]-style `TypeVar` declarations are also removed from the [type parameter list];
|
/// with references to `Self`. The fix also adds an import of `Self` if neither `Self` nor `typing`
|
||||||
/// however, old-style `TypeVar`s do not have their declarations removed. See
|
/// is already imported in the module. If your [`target-version`] setting is set to Python 3.11 or
|
||||||
/// [`unused-private-type-var`][PYI018] for a rule to clean up unused private type variables.
|
/// newer, the fix imports `Self` from the standard-library `typing` module; otherwise, the fix
|
||||||
|
/// imports `Self` from the third-party [`typing_extensions`][typing_extensions] backport package.
|
||||||
///
|
///
|
||||||
/// If there are any comments within the fix ranges, it will be marked as unsafe.
|
/// If the custom type variable is a [PEP-695]-style `TypeVar`, the fix also removes the `TypeVar`
|
||||||
/// Otherwise, it will be marked as safe.
|
/// declaration from the method's [type parameter list]. However, if the type variable is an
|
||||||
|
/// old-style `TypeVar`, the declaration of the type variable will not be removed by this rule's
|
||||||
|
/// fix, as the type variable could still be used by other functions, methods or classes. See
|
||||||
|
/// [`unused-private-type-var`][PYI018] for a rule that will clean up unused private type
|
||||||
|
/// variables.
|
||||||
|
///
|
||||||
|
/// The fix is only marked as unsafe if there is the possibility that it might delete a comment
|
||||||
|
/// from your code.
|
||||||
///
|
///
|
||||||
/// ## Preview-mode behaviour
|
/// ## Preview-mode behaviour
|
||||||
/// This rule's behaviour has several differences when [`preview`] mode is enabled:
|
/// This rule's behaviour has several differences when [`preview`] mode is enabled:
|
||||||
|
@ -77,6 +89,7 @@ use crate::settings::types::PythonVersion;
|
||||||
/// [type parameter list]: https://docs.python.org/3/reference/compound_stmts.html#type-params
|
/// [type parameter list]: https://docs.python.org/3/reference/compound_stmts.html#type-params
|
||||||
/// [Self]: https://docs.python.org/3/library/typing.html#typing.Self
|
/// [Self]: https://docs.python.org/3/library/typing.html#typing.Self
|
||||||
/// [typing_TypeVar]: https://docs.python.org/3/library/typing.html#typing.TypeVar
|
/// [typing_TypeVar]: https://docs.python.org/3/library/typing.html#typing.TypeVar
|
||||||
|
/// [typing_extensions]: https://typing-extensions.readthedocs.io/en/latest/
|
||||||
#[derive(ViolationMetadata)]
|
#[derive(ViolationMetadata)]
|
||||||
pub(crate) struct CustomTypeVarForSelf {
|
pub(crate) struct CustomTypeVarForSelf {
|
||||||
typevar_name: String,
|
typevar_name: String,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue