Function
custom_pydantic_encoder
(type_encoders: Dict[Any, Callable[[Type[Any]], Any]], obj: Any)
Source from the content-addressed store, hash-verified
| 91 | |
| 92 | |
| 93 | def custom_pydantic_encoder(type_encoders: Dict[Any, Callable[[Type[Any]], Any]], obj: Any) -> Any: |
| 94 | # Check the class type and its superclasses for a matching encoder |
| 95 | for base in obj.__class__.__mro__[:-1]: |
| 96 | try: |
| 97 | encoder = type_encoders[base] |
| 98 | except KeyError: |
| 99 | continue |
| 100 | |
| 101 | return encoder(obj) |
| 102 | else: # We have exited the for loop without finding a suitable encoder |
| 103 | return pydantic_encoder(obj) |
| 104 | |
| 105 | |
| 106 | def timedelta_isoformat(td: datetime.timedelta) -> str: |