Skip to content

Commit 6765e64

Browse files
committedDec 26, 2023
Don't try to encode unknown types
1 parent ba7b096 commit 6765e64

File tree

2 files changed

+4
-11
lines changed

2 files changed

+4
-11
lines changed
 

‎beanie/odm/utils/encoder.py

+1-11
Original file line numberDiff line numberDiff line change
@@ -135,17 +135,7 @@ def encode(self, obj: Any) -> Any:
135135
if isinstance(obj, Iterable):
136136
return [self.encode(value) for value in obj]
137137

138-
errors = []
139-
try:
140-
data = dict(obj)
141-
except Exception as e:
142-
errors.append(e)
143-
try:
144-
data = vars(obj)
145-
except Exception as e:
146-
errors.append(e)
147-
raise ValueError(errors)
148-
return self.encode(data)
138+
raise ValueError(f"Cannot encode {obj!r}")
149139

150140
def _iter_model_items(
151141
self, obj: pydantic.BaseModel

‎tests/odm/models.py

+3
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,9 @@ class DocumentWithCustomFiledsTypes(Document):
283283
tuple_type: Tuple[int, str]
284284
path: Path
285285

286+
class Settings:
287+
bson_encoders = {Color: vars}
288+
286289
if IS_PYDANTIC_V2:
287290
model_config = ConfigDict(
288291
arbitrary_types_allowed=True,

0 commit comments

Comments
 (0)