Whole file
anikolaienko/py-automapper
The author described this change as “Fix mapping of string enum types”. It counts as a record because the check below fails on the code as it stood at 01c250840 and passes on e658a3d43, with nothing else changed between the two runs.
Projectanikolaienko/py-automapper
Fix saved2022-11-20
Sharing licenceMIT · LICENSE
Change size+7 −1
What the code was meant to do, written into the code itself as a save note
Fix mapping of string enum types
The change
| 1 | 1 | import inspect | |
| 2 | 2 | from copy import deepcopy | |
| 3 | + | from enum import Enum | |
| 3 | 4 | from typing import ( | |
| 4 | 5 | Any, | |
| 5 | 6 | Callable, | |
| ⋯ | |||
| 51 | 52 | return type(obj) in __PRIMITIVE_TYPES | |
| 52 | 53 | ||
| 53 | 54 | ||
| 55 | + | def _is_enum(obj: Any) -> bool: | |
| 56 | + | """Check if object type is enum""" | |
| 57 | + | return issubclass(type(obj), Enum) | |
| 58 | + | ||
| 59 | + | ||
| 54 | 60 | def _try_get_field_value( | |
| 55 | 61 | field_name: str, original_obj: Any, custom_mapping: FieldsMap | |
| 56 | 62 | ) -> Tuple[bool, Any]: | |
| ⋯ | |||
| 267 | 273 | self, obj: S, _visited_stack: Set[int], skip_none_values: bool = False | |
| 268 | 274 | ) -> Any: | |
| 269 | 275 | """Maps subobjects recursively""" | |
| 270 | - | if _is_primitive(obj): | |
| 276 | + | if _is_primitive(obj) or _is_enum(obj): | |
| 271 | 277 | return obj | |
| 272 | 278 | ||
| 273 | 279 | obj_id = id(obj) | |
The check that tells the two apart
fail→pass·tests/test_automapper_enum.py::test_map__enum
Check file tests/test_automapper_enum.py, taken without changes from the fix and copied onto the older code, so the exact same check runs against both versions.
Origin and history
The code before it01c250840d4cb584582f203f75fabdecbaedfa03
Broken version dated2022-11-13
Moduleautomapper.mapper
Units changedMapper
Fingerprint7738ef352132fb26
Checked2026-08-18 by goldset/0.1
Every field above is generated by our program. None of it is written by hand.