Skip to content

Commit

Permalink
[flake8-pyi] Improve motivation for custom-type-var-return-type (…
Browse files Browse the repository at this point in the history
…`PYI019`) (#8766)

Closes #8765.
  • Loading branch information
charliermarsh authored Nov 19, 2023
1 parent 415e808 commit 7165f8f
Showing 1 changed file with 9 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ use crate::checkers::ast::Checker;
/// annotation instead of using `typing_extensions.Self`.
///
/// ## Why is this bad?
/// If certain methods are annotated with a custom `TypeVar` type, and the
/// class is subclassed, type checkers will not be able to infer the correct
/// return type.
/// While the semantics are often identical, using `typing_extensions.Self` is
/// more intuitive and succinct (per [PEP 673]) than a custom `TypeVar`. For
/// example, the use of `Self` will typically allow for the omission of type
/// parameters on the `self` and `cls` arguments.
///
/// This check currently applies to instance methods that return `self`, class
/// methods that return an instance of `cls`, and `__new__` methods.
Expand All @@ -42,16 +43,18 @@ use crate::checkers::ast::Checker;
///
///
/// class Foo:
/// def __new__(cls: type[Self], *args: str, **kwargs: int) -> Self:
/// def __new__(cls, *args: str, **kwargs: int) -> Self:
/// ...
///
/// def foo(self: Self, arg: bytes) -> Self:
/// def foo(self, arg: bytes) -> Self:
/// ...
///
/// @classmethod
/// def bar(cls: type[Self], arg: int) -> Self:
/// def bar(cls, arg: int) -> Self:
/// ...
/// ```
///
/// [PEP 673]: https://peps.python.org/pep-0673/#motivation
#[violation]
pub struct CustomTypeVarReturnType {
method_name: String,
Expand Down

0 comments on commit 7165f8f

Please sign in to comment.