bpo-36643: Unwrap ForwardRefs and string types in dataclasses.fields() result #27330
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
dataclasses.fields() returns dataclasses.Field instances which can have
Field.type
to be actual types,ForwardRef
orstr
(in case of__future__.annotations
). This behaviour is overall obscure leaking implementation details, and making every user offields()
reimplement the unwrapping of types, mixing betweendataclasses.fields()
andtyping.get_type_hints()
(which does perform unwrapping of types).This PR fixes this behaviour by unwrapping the types during
fields()
execution.Also, see https://bugs.python.org/issue36643
Please let me know if there are any documentation, code style, or implementation changes that need to be performed for this to be merged.
(On a side note: it seems like
Field
is marked as an internal object in documentation — it is said not to be instantiated directly by users. Call tofields()
returnsField
objects, but it seems like no builtins use anything except forField.name
when callingfields()
. Maybe it is better to separate internal/external APIs here in some way?)https://bugs.python.org/issue36643