Skip to content

Commit

Permalink
Merge pull request #225 from andlaus/no_dataclass_internals
Browse files Browse the repository at this point in the history
`dataclass_fields_asdict()`: do not use internals of the dataclass implementation
  • Loading branch information
andlaus authored Oct 20, 2023
2 parents b338aed + 3788d66 commit e453456
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions odxtools/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# SPDX-License-Identifier: MIT
import dataclasses
import re
from typing import Any, Dict, Optional
from xml.etree import ElementTree
Expand Down Expand Up @@ -31,10 +32,12 @@ def create_description_from_et(et_element: Optional[ElementTree.Element],) -> Op
def dataclass_fields_asdict(obj: Any) -> Dict[str, Any]:
"""Extract all attributes from a dataclass object that are fields.
This makes hierarchies of dataclasses possible while initializing
the base class using common code.
This is a non-recursive version of `dataclasses.asdict()`. Its
purpose is to make hierarchies of dataclasses possible while
initializing the base class using common code.
"""
return {x.name: getattr(obj, x.name) for x in obj.__dataclass_fields__.values()}
return {x.name: getattr(obj, x.name) for x in dataclasses.fields(obj)}


# ISO 22901 section 7.1.1
Expand Down

0 comments on commit e453456

Please sign in to comment.