python / python/mypy

class decorator syntax seems to ignore dataclasses' constructor signatures

Open
#12,971 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-dataclasses
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the supplied example with mypy 0.961 and compare the diagnostics for the decorator-position case with the direct clsdec(DX) call. Trace the decorator and dataclass-signature handling involved; done means equivalent constructor-signature checking for both forms, covered by a regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.