-
Notifications
You must be signed in to change notification settings - Fork 53
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
Which fields to expose in dataclasses.fields()? #75
Comments
I think it should be only regular fields, since the main use case would be to actually access those fields on an instance. I think having to use |
Well you could do [0] Or whatever a check for the kind of field would look like. |
I'm going to leave |
As pointed out in #17 (comment), there are now 3 kinds of fields: regular fields,
InitVar
fields, andClassVar
fields.With the addition of
InitVar
s (see #17), the internal__dataclass_fields__
class variable now needs to store theInitVar
fields in order to correctly compute derived class__init__()
params. I keep track of which type each field is with an undocumentedField._field_type
member. I don't think I even keep track of theClassVar
fields, since I don't need them for anything.The question comes up: which fields are returned by
dataclasses.fields()
? As I currently have it implemented, it returns just the regular fields, not theInitVar
orClassVar
fields. If we decide it needs to return the latter two, I'll have to expose the_field_type
by renaming it, and give it better values that can be more easily consumed.I'm leaning toward only returning the regular fields. We can always expose the other field types in the future, maybe by a new param for
fields()
indicating which types to return, or by a new API likedataclasses.all_fields()
.I see the main reason for
fields()
is for serializers and the like to see what data is in each instance, and for that they only need the regular fields. But maybe I'm not thinking big enough, and there's some use forInitVar
s in particular.The text was updated successfully, but these errors were encountered: