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 inheritance with keyword-only args [3.10] #1626

Closed
cdce8p opened this issue Jul 30, 2021 · 2 comments
Closed

dataclass inheritance with keyword-only args [3.10] #1626

cdce8p opened this issue Jul 30, 2021 · 2 comments
Labels
enhancement New feature or request fixed in next version (main) A fix has been implemented and will appear in an upcoming version

Comments

@cdce8p
Copy link

cdce8p commented Jul 30, 2021

Python 3.10 adds the ability to specify keyword-only args in dataclasses. Especially with inheritance, this allows some combinations that would previously have been an error, and are currently still detected as such.

For example, it's now possible to specify fields without a default in a derived class if only keyword-only args in the base class have a default. This will work since keyword-only args need to come after positional/keyword args. https://docs.python.org/3.10/library/dataclasses.html#re-ordering-of-keyword-only-parameters-in-init

from dataclasses import dataclass, KW_ONLY

@dataclass
class Base:
    x: str
    _: KW_ONLY
    y: int = 0
    w: int = 1

@dataclass
class D(Base):
    z: str  # (1)
    a: str = "a"
>>> D("Hello", "World")
D(x='Hello', y=0, w=1, z='World', a='a')

At the moment, pylance indicates an error for z at position (1):

Fields without default values cannot appear after fields with default values

--
On a related note, the tip for D is also not correct.
Screen Shot 2021-07-30 at 17 45 00

Due to the reordering it should be

D(x: str, z: str, *, y: int = 0, w: int = 1, a: str = "a") -> None
@erictraut
Copy link
Contributor

This functionality will be included in the next release.

@erictraut erictraut added the fixed in next version (main) A fix has been implemented and will appear in an upcoming version label Aug 6, 2021
@heejaechang
Copy link
Contributor

fixed in 2021.8.1

@heejaechang heejaechang removed the fixed in next version (main) A fix has been implemented and will appear in an upcoming version label Aug 11, 2021
@jakebailey jakebailey added the fixed in next version (main) A fix has been implemented and will appear in an upcoming version label Aug 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request fixed in next version (main) A fix has been implemented and will appear in an upcoming version
Projects
None yet
Development

No branches or pull requests

5 participants