Skip to content

Callable attribute is considered a method #9489

Description

@NiklasRosenstein

Small example to illustrate the issue:

importtypingastfromdataclassesimportdataclass, field@dataclassclassExample:
a: t.Callable[[], int] =field(default=lambda: 42)
defcall_a(self) ->None:
self.a() # Attribute function "a" with type "Callable[[], int]" does not accept self argument

Curiously, adding Example as the first argument to the type of a makes the call be recognized OK but the default not.

fromtypingimporttfromdataclassesimportdataclass, field@dataclassclassExample:
a: t.Callable[['Example'], int] =field(default=lambda: 42) # Incompatible types in assignment (expression has type "Callable[[], int]", variable has type "Callable[[Example], int]")defcall_a(self):
self.a()

Your Environment

  • Mypy version used: mypy 0.782
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.7
  • Operating system and version: Debian WSL

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