Skip to content

New check: consider using Self #8333

Open
@nickdrozd

Description

@nickdrozd

Current problem

As of version 1.0, Mypy supports the Self type. So, I would like a checker that suggests using Self where it is possible.

Desired solution

This means checking the return annotations for class methods and seeing if any can be Self.

It's not as simple as checking whether the return annotation is the same as the class. I don't understand the details exactly, but I think the method has to return the object itself, i.e. self. In this example, the increment method is a candidate for using Self, but the copy method is not:

from dataclasses import dataclass

@dataclass
class Counter:
    count: int = 0

    def increment(self) -> Counter:  # consider-using-self
        self.count += 1
        return self

    def copy(self) -> Counter:  # not Self
        return Counter(self.count)

Additional context

PEP 673 – Self Type

Metadata

Metadata

Assignees

No one assigned

    Labels

    Enhancement ✨Improvement to a componentNeeds PRThis issue is accepted, sufficiently specified and now needs an implementationtyping

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions