Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[flake8-pyi] Improve motivation for custom-type-var-return-type (PYI019) #8766

Merged
merged 1 commit into from
Nov 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading