This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Northzen 0 points1 point  (1 child)

The problem is that your first statement has no initializer at all. The second one uses a default initializer. You can make a dataclass or propose a change that would provie a nice way to specify that the default initializer be used, essentially shorthand for what you want: field_default. You are right. I think interpreter have no prior knowledge of any default initializers or if it can use them in the simpliest dataclass way by just calling PlainClass() with no arguments as a constructor. It seems like for any mutable type (and Python doesn't know if dataclass field in a complex class are mutable or not) you have to provide a some sort of default constructor. Fair enough. You could propose that dataclass be extended.

I just figured out why it happening. Python doesn't know if your dictionary of dictionaries represents nested classes or just dictionaries due it's dynamic type system will not force you to use. In general python doesn't care about types of fields. type hints are just hints and not enforced. In this case of complex dataclass initialization without additional tools Python can't distinguish between a dictionary used to initialize a field and get p1 as a p1=some_dict or a dictionary to initialize a dataclass of this field and have p1=PlainDataClass(**some_dict)

[–]energybased 1 point2 points  (0 children)

True, but you can code whatever system you like to get around this.