Mypy: Add support for dataclasses

Created on 26 Mar 2018  Â·  5Comments  Â·  Source: python/mypy

I have added preliminary support for dataclasses in https://github.com/python/typeshed/pull/1944.

These seem to work for declarations, but not for instantiation. For example:

@dataclass
class D0:
  x: int

d0 = D0(x=0)

The constructor is erroneously flagged, and points to the __init__ annotation for object:

error: Unexpected keyword argument "x" for "D0"
/Users/gwk/work/python/mypy/typeshed/stdlib/3/builtins.pyi:38:5: note: "D0" defined here

It appears that we need to teach mypy about dataclasses more deeply.

feature priority-1-normal

Most helpful comment

Yes.

All 5 comments

Yes, we will definitely need a mypy plugin to support dataclasses. It can probably be somewhat similar to the existing one for attrs.

It is good that you opened this issue, we definitely wanted to have a dedicated support for this, so now we have a place to track it. (Btw, I wanted to do this.)

I'm not in the position to develop this plugin, so by all means give it a whirl if you want. I am actively using dataclasses though so I can help with testing.

On Mar 26, 2018, at 2:15 PM, Ivan Levkivskyi notifications@github.com wrote:

It is good that you opened this issue, we definitely wanted to have a dedicated support for this, so now we have a place to track it. (Btw, I wanted to do this.)

—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or mute the thread.

One other point: I backported dataclasses to 3.6 as a third party module inside of a general-purpose utility package that I maintain. I wonder, will it be possible/easy to get this dataclass plugin to recognize that module for < 3.7?

Yes.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Stiivi picture Stiivi  Â·  3Comments

ezyang picture ezyang  Â·  3Comments

PeterJCLaw picture PeterJCLaw  Â·  3Comments

takeda picture takeda  Â·  3Comments

arquolo picture arquolo  Â·  3Comments