Skip to content

Commit

Permalink
Don't try to encode unknown types
Browse files Browse the repository at this point in the history
  • Loading branch information
gsakkis committed Dec 26, 2023
1 parent ba7b096 commit 6765e64
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 11 deletions.
12 changes: 1 addition & 11 deletions beanie/odm/utils/encoder.py
Expand Up @@ -135,17 +135,7 @@ def encode(self, obj: Any) -> Any:
if isinstance(obj, Iterable):
return [self.encode(value) for value in obj]

errors = []
try:
data = dict(obj)
except Exception as e:
errors.append(e)
try:
data = vars(obj)
except Exception as e:
errors.append(e)
raise ValueError(errors)
return self.encode(data)
raise ValueError(f"Cannot encode {obj!r}")

def _iter_model_items(
self, obj: pydantic.BaseModel
Expand Down
3 changes: 3 additions & 0 deletions tests/odm/models.py
Expand Up @@ -283,6 +283,9 @@ class DocumentWithCustomFiledsTypes(Document):
tuple_type: Tuple[int, str]
path: Path

class Settings:
bson_encoders = {Color: vars}

if IS_PYDANTIC_V2:
model_config = ConfigDict(
arbitrary_types_allowed=True,
Expand Down

0 comments on commit 6765e64

Please sign in to comment.