Skip to content

False positive for class method without Self annotation  #15771

Open

Description

@tyralla

Mypy reports an error for method save1 (without annotation) but no errors for save2 and save3 (with different Self annotations), despite their (almost) identical function bodies:

from copy import deepcopy
from typing import Self

class A:
    states: list[Self]

    def save1(self) -> None:
        self.states.append(deepcopy(self))  # error: Argument 1 to "append" of "list" has incompatible type "A"; expected "Self"  [arg-type]

    def save2(self: Self) -> None:
        self.states.append(deepcopy(self))

    def save3(self) -> Self:
        self.states.append(deepcopy(self))
        return self

This bug might be related to #15529 but is not fixed by #15541.

### Tasks

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions