Skip to content

Class with function fields incorrectly thinks first argument is self #5485

@Dentosal

Description

@Dentosal

This is a bug report.

Minimal repro

from dataclasses import dataclass
from typing import Callable

@dataclass
class Repro:
    fn: Callable[[int], str]

r = Repro(fn=repr)
assert "1" == r.fn(1)

Works correctly when dataclass is not used, i.e. when Repro is

class Repro:
    def __init__(self, fn: Callable[[int], str]) -> None:
        self.fn = fn

Actual behavior

repro.py:9: error: Invalid self argument "Repro" to attribute function "fn" with type "Callable[[int], str]"
repro.py:9: error: Too many arguments

When running with Python, works correctly. Doesn't throw any exceptions.

Expected behavior

Successful type check

Environment

Versions

Tested with two versions, 0.620 and current version from GitHub.

  • Python 3.7.0
  • mypy 0.620
  • mypy 0.630+dev-a2ffde90635baf05e264f8d899f5705c52b27c2a

Mypy config

[mypy]
warn_redundant_casts = True
warn_unused_ignores = True
incremental = True

follow_imports = normal
ignore_missing_imports = True
disallow_untyped_calls = True
disallow_untyped_defs = True
check_untyped_defs = True
warn_return_any = True
warn_no_return = True
no_implicit_optional = True
strict_optional = True

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions