Skip to content

mypy should reject non-existent fields with dataclasses.replace #14793

@OlegAlexander

Description

@OlegAlexander

This issue was originally reported by @LunarLanding here:

microsoft/pyright#1047

I believe mypy should reject non-existent fields when dataclasses.replace is used.

To Reproduce

from dataclasses import dataclass, replace

@dataclass(frozen=True) 
class A:
    x : int
    y : int
    
a = A(0, 1)
b = replace(a, x=1) # ok
c = replace(a, not_a_field=1) # should be: error: "A" has no attribute "not_a_field"  [attr-defined]

Mypy Playground

Expected Behavior

Error: "A" has no attribute "not_a_field" [attr-defined]

Actual Behavior

No error

Related Issue

#14525

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions