There are some data classes where I want to have only keyword arguments, and some where I want only positional. Take for example:
@dataclass() class XYZ: x: int y: int z: int # For this dataclass, it might make sense to force positional-only such that # this works: XYZ(1, 2, 3) # But these don't: XYZ(1, 2, z=3) XYZ(x=1, y=2, z=3)
Similarly, and more importantly, one might want to force keyword arguments:
@dataclass() class Credentials: username: str password: str # With this, we should allow: Credentials(username="mikeholler", password="hunter2") # But maybe not allow for concern of misuse and confusion: Credentials("mikeholler", "hunter2") Credentials("mikeholler", password="hunter2")
dataclasses.py from the 3.8 sources, it seems like there’s no way to get at the constructor parameters to write special handling logic for myself (without using
init=False and writing a custom init function every time, which is an unacceptable solution in my opinion). This means the solution would have to lie within an update to
dataclass itself, some syntax to say “here is where forced positional arguments and” and “here is where forced kwargs end”.
This could be, perhaps, added to the
field(...) function, but implementation details aside, the ability to specify which args in a dataclass’s constructor should be positional and which should be kwargs (or both when both are allowed) would be a great quality of life improvement.
Does anybody have good ideas for a syntax that would be best for this? Anyone also think this is a good idea?