dataclasses kw_only doesn't seem to allow mixing defaults and inheritance #637
Replies: 2 comments 1 reply
-
|
Hi @KerryCerqueira from serde import serde, field
from serde.json import to_json, from_json
from dataclasses import dataclass
@serde
@dataclass(kw_only=True)
class Foo:
x: int = field(default=0)
@serde
@dataclass(kw_only=True)
class Bar(Foo):
y: int
if __name__ == "__main__":
y = Bar(y=10)
xy = Bar(y=10, x=20)
print(y)
print(xy)
print(to_json(xy))
print(from_json(Foo, '{"y": 10, "x": 20}'))Bar(x=0, y=10)
Bar(x=20, y=10)
{"x":20,"y":10}
Foo(x=20) |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
You're right, both my example and yours do work. I'd convinced myself somehow that the interpreter was throwing the error but now when I try it only pyright does: That's still a little confusing though. Any idea why the static analysis is failing? |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Since pull request #413 using a dataclass annotation with the kw_only flag "works as expected", but if I declare classes like
python throws a familiar error about the order of constructor arguments. I didn't find any examples in the documentation using inheritance so it's not clear to me how this feature is supported, or what I'm supposed to do to once-and-for-all set all relevant fields to be keyword only. If this works, can anyone help me with a minimal example of correct usage?
Beta Was this translation helpful? Give feedback.
All reactions