Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dataclass and field alias #9875

Closed
CedricCabessa opened this issue Jan 4, 2021 · 2 comments
Closed

dataclass and field alias #9875

CedricCabessa opened this issue Jan 4, 2021 · 2 comments
Labels
bug mypy got something wrong topic-dataclasses

Comments

@CedricCabessa
Copy link

Hi,

Bug Report

I'm trying to write a simple wrapper around dataclass.field

To Reproduce

Here is a simple snippet that work perfectly under mypy

from dataclasses import field, dataclass

@dataclass
class C:
    some_int: int
    some_str: str = field(metadata={"doc": "foo"})
    another_int: int

c = C(42, "bla", 43)

Now in order to make doc creation easy, I want to write an helper like this

def doc(documentation: str):
    return field(metadata={"doc": documentation})

So, my snippet can be written:

@dataclass
class C:
    some_int: int
    some_str: str = doc("foo")
    another_int: int

Expected Behavior

mypy return no error like in the first snippet

Actual Behavior

error: Attributes without a default cannot follow attributes with one

Your Environment

  • Mypy version used: 0.790
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: python3.8
  • Operating system and version: ubuntu 20.04

Related Info

I believe those bugs are kind of related.

#8853
#5383

There is a few users that need to alias freely assign / alias dataclass method and class.

@CedricCabessa CedricCabessa added the bug mypy got something wrong label Jan 4, 2021
@gvanrossum
Copy link
Member

Fixing this will require some changes to the plugin that's used to deal with dataclass semantics. I'm not sure it is easy -- there would have to be a way to mark specific functions (like your doc()) as having similar semantics as field(). But you're free to look over the source code (start at mypy/plugins/dataclasses.py).

@hauntsaninja
Copy link
Collaborator

Closing, see #5383 (comment)

@hauntsaninja hauntsaninja closed this as not planned Won't fix, can't repro, duplicate, stale Aug 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-dataclasses
Projects
None yet
Development

No branches or pull requests

4 participants