Skip to content

Position of double-starred kwargs affects the type errors when instantiating subclasses attr objects#9395

Description

@achimnol

馃悰 Bug Report

I'm not sure this is the issue for typeshed or mypy, but with attrs, unpacking a dict to initialize a subclassed attr instance generates a weird type error or not depending on the position of the double-starred kwargs, where the runtime behavior is completely fine for both cases.

To Reproduce

Run mypy with the following script:

import attr
from typing import List

@attr.s(auto_attribs=True, slots=True)
class Point:
    x: int
    y: int

@attr.s(auto_attribs=True, slots=True)
class Point3D(Point):
    z: int
    opts: List[str]

p = Point(x=1, y=2)
q = Point3D(
    **attr.asdict(p),
    z=3,
    opts=['a', 'b'],
)

Expected Behavior

There should be no type errors. (Or at least, some different error saying that it cannot validate keys from an arbitrary dict..)

Actual Behavior

When ** comes first:

p = Point(x=1, y=2)
q = Point3D(  # <-- error: "Point3D" gets multiple values for keyword argument "opts"
    **attr.asdict(p),
    z=3,
    opts=['a', 'b'],
)

When ** comes last:

p = Point(x=1, y=2)
q = Point3D(  # no errors!
    z=3,
    opts=['a', 'b'],
    **attr.asdict(p),
)

where the runtime results are same.

Q: Are there other ways to initialize a subclassed attr instance from a super-attr instance with its values, instead of using **attr.asdict()?

Your Environment

  • Mypy version used: 0.783
  • Mypy command-line flags: python -m mypy test-attr.py
  • Mypy configuration options from mypy.ini (and other config files):
[mypy]
ignore_missing_imports = true
namespace_packages = true
  • Python version used: 3.8.5
  • Operating system and version: Ubuntu 18.04

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions