Skip to content

class decorator syntax seems to ignore dataclasses' constructor signatures #12971

@glyph

Description

@glyph

Bug Report

This code sample gives errors on the presence of f in X's constructor but not on DX's

from __future__ import annotations
from typing import Callable, Type
from dataclasses import dataclass

def clsdec(x: Callable[[int, str, object], object]) -> Type:
    return x                    # type: ignore

@clsdec
class X(object):
    def __init__(self, x: int, y: str, z: object, f: float) -> None:
        pass

@clsdec
@dataclass
class DX(object):
    x: int
    y: str
    z: object
    f: float

It seems like it can't "see" the dataclass's fields somehow, despite otherwise reporting sensible errors at the call site.

In fact, clsdec(DX) gives the same error; it's only a problem when it's used in the decorator position.

Your Environment

  • Mypy version used: mypy 0.961 (compiled: yes)
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: Python 3.10.4 (python.org)
  • Operating system and version:
ProductName:	macOS
ProductVersion:	12.4
BuildVersion:	21F79

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions