python / python/mypy

Not finding type specifications in make_dataclass

Open
#6,063 11 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature priority-1-normal topic-dataclasses
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Given the following script:

from typing import NamedTuple
from dataclasses import make_dataclass, dataclass

Emp1 = NamedTuple('Emp1', [('name', str), ('id', int)])
r1 = Emp1('raymond', 'xyz')

Emp2 = make_dataclass('Emp2', [('name', str), ('id', int)])
r2 = Emp2('raymond', 'xyz')

class Emp3(NamedTuple):
    name: str
    id: int

r3 = Emp3('raymond', 'xyz')

@dataclass
class Emp4:
    name: str
    id: int

r4 = Emp4('raymond', 'xyz')

mypy only finds three of the four type errors:

$ mypy --version
mypy 0.650
$ mypy nt_example.py
nt_example.py:5: error: Argument 2 to "Emp1" has incompatible type "str"; expected "int"
nt_example.py:14: error: Argument 2 to "Emp3" has incompatible type "str"; expected "int"
nt_example.py:21: error: Argument 2 to "Emp4" has incompatible type "str"; expected "int"

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

Reproduce the example with mypy 0.650, focusing on the make_dataclass call and the constructor it generates. Trace how mypy handles dataclasses.make_dataclass, then add a regression test based on the script; done means the invalid string argument for Emp2 is reported along with the other three errors.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.