Skip to content

Class with attribute function thinks first argument is self #10353

Description

@spagh-eddie

Bug Report

I find this very similar to issue #5485.

I found myself using this flavor of idiom so as to not repeat myself when implementing comparison operations, etc.

To Reproduce

from operator import itemgetter

class A:
    id_getter = itemgetter('this', 'that', 'other')
    def __init__(self, d):
        self.d = d
    def __hash__(self) -> int:
        return hash(self.id_getter(self.d))

a = A({'this': 1, 'that': 2, 'other': 'a', 'extra': 'b'})
hash(a)

Expected Behavior

No error

Actual Behavior

$ mypy example.py
example.py:8: error: Too many arguments
Found 1 error in 1 file (checked 1 source file)

I tried to annotate id_getter: ClassVar[Callable[[Dict], Tuple[int, int, str]]] but just got back an additional error

examply.py:8: error: Invalid self argument "A" to attribute function "id_getter" with type "Callable[[Dict[Any, Any]], Tuple[Any, ...]]"

Your Environment

$ mypy --version
mypy 0.812

No flags or ini file.

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