Skip to content

dataclasses.replace does not type correctly #1047

@LunarLanding

Description

@LunarLanding

Note: if you are reporting a wrong signature of a function or a class in the standard library, then the typeshed tracker is better suited for this report: https://github.com/python/typeshed/issues.

Describe the bug
Hi, I'm using dataclasses with frozen=True as immutable values.
However the very useful 'replace' function from the same module doesn't type correctly (see docs ).
This allows to create a new value by changing fields specified via keyword args.
To Reproduce
Create a dataclass, use the replace function with incorrect arguments.
Expected behavior
Typecheck fail.
Screenshots or Code

from dataclasses import dataclass,replace
@dataclass
class A:
    x : int
    y : int
a = A(0,1)
b = replace(a,x=1) #types correctly
c = replace(a,not_a_field=1) #should not typecheck

VS Code extension or command-line
VS Code extension.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions