feat(config): recursively convert parsed dicts to typed dataclasses in loader#5269
Open
MikeGoldsmith wants to merge 5 commits into
Open
feat(config): recursively convert parsed dicts to typed dataclasses in loader#5269MikeGoldsmith wants to merge 5 commits into
MikeGoldsmith wants to merge 5 commits into
Conversation
Adds `_dict_to_dataclass` in `_conversion.py` which walks each field's type annotation and converts: - nested dicts → typed dataclass instances - lists of dicts → lists of typed dataclasses - string/value → Enum members (e.g. log_level: info) - unknown keys → routed to the @_additional_properties decorator The loader's `_dict_to_model` now produces a fully-typed OpenTelemetryConfiguration tree end-to-end. Factory functions can rely on typed attribute access (config.tracer_provider.processors[0].batch .exporter.otlp_http.endpoint) instead of failing on raw dicts. This closes the gap between load_config_file() and the factory functions — YAML/JSON config → SDK objects now works end-to-end. Closes open-telemetry#5127 Assisted-by: Claude Opus 4.6
25 tasks
- Use TypeVar for _dict_to_dataclass return — callers now get the correct type instead of Any - Use collections.abc.Mapping for input (more permissive than dict) - Add explicit is_dataclass check at entry — raises TypeError with a descriptive message instead of failing later in dataclasses.fields Assisted-by: Claude Opus 4.6
Astroid 3.x (used by pylint 3.x) follows typing.get_type_hints into Python 3.14's annotationlib, which contains t-string literals it can't parse and crashes with AttributeError on 'visit_templatestr'. Wrapping the call in a helper that returns dict[str, Any] stops the inference at the declared return type. Assisted-by: Claude Opus 4.7
Same effect as the prior helper — declaring the local as ``dict[str, Any]`` stops astroid's inference at the annotation rather than tracing into the typing internals. Assisted-by: Claude Opus 4.7
12 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
Closes the gap between
load_config_file()and the factory functions: YAML/JSON config → SDK objects now works end-to-end through the typed model tree.Previously, the loader's
_dict_to_modeldidOpenTelemetryConfiguration(**data)which only constructed the top-level dataclass — nested fields stayed as raw dicts. This meant factory functions likecreate_tracer_provider(config: TracerProviderConfig)would break trying to accessconfig.sampleras an attribute when it was actually a dict.Approach
Added
_dict_to_dataclassin a new_conversion.pymodule. It walks each field's type annotation viatyping.get_type_hintsand recursively converts:dict→TracerProvider→SpanProcessor→BatchSpanProcessor→ ...)list[SpanProcessor])log_level: info→SeverityNumber.info)@_additional_propertiesdecorator (so user-defined plugin names still flow through)Optional[X]/X | Noneis unwrapped before checking the inner type.ClassVarfields are skipped (theadditional_propertiesannotation on decorated classes is correctly ignored).Verified end-to-end
User-defined plugins continue to work — unknown sampler/propagator/exporter names land in
additional_propertiesand are loaded via entry points.Tests
11 new tests in
test_conversion.pycovering: flat dicts, nested dataclasses, lists, optionals, missing fields, unknown keys (additional_properties), enum coercion, primitive pass-through.Closes #5127