Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] 'pydantic_core._pydantic_core.Url' object is not iterable #728

Closed
yutongp opened this issue Oct 2, 2023 · 2 comments · Fixed by #730
Closed

[BUG] 'pydantic_core._pydantic_core.Url' object is not iterable #728

yutongp opened this issue Oct 2, 2023 · 2 comments · Fixed by #730
Labels
bug Something isn't working

Comments

@yutongp
Copy link

yutongp commented Oct 2, 2023

Describe the bug
Document.save() throws error on pydnticV2.HttpUrl type fields

To Reproduce

from beanie import Document, init_beanie
from pydantic import HttpUrl
from motor.motor_asyncio import AsyncIOMotorClient
import os

class Data(Document):
    link: HttpUrl

MONGO_URI = os.environ.get("MONGO_URI", "mongodb://localhost:27017/")


async def config_db():
    # Create Motor client
    client = AsyncIOMotorClient(MONGO_URI)

    # Init beanie with the Product document class
    await init_beanie(
        database=client["test"],
        document_models=[
            Data,
        ],
    )

await config_db()

data = Data(link="https://www.google.com")
await data.save()

error message:

File [venv/lib/python3.9/site-packages/beanie/odm/utils/encoder.py:244), in Encoder._encode(self, obj)
    243 try:
--> 244     data = dict(obj)
    245 except Exception as e:

TypeError: 'pydantic_core._pydantic_core.Url' object is not iterable

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
File [venv/lib/python3.9/site-packages/beanie/odm/utils/encoder.py:248](venv/lib/python3.9/site-packages/beanie/odm/utils/encoder.py:248), in Encoder._encode(self, obj)
    247 try:
--> 248     data = vars(obj)
    249 except Exception as e:

TypeError: vars() argument must have __dict__ attribute

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
     [24](line=23) await config_db()
     [26](line=25) data = Data(link="https://www.google.com/")
...
    250         errors.append(e)
--> 251         raise ValueError(errors)
    252 return self._encode(data)

ValueError: [TypeError("'pydantic_core._pydantic_core.Url' object is not iterable"), TypeError('vars() argument must have __dict__ attribute')]

Expected behavior
save runs without error out

Additional context
Add any other context about the problem here.

@roman-right
Copy link
Member

Hi! Thank you for the catch - I'll reproduce and fix it this week.

@roman-right roman-right added the bug Something isn't working label Oct 2, 2023
@yamanahlawat
Copy link

Also, facing the same issue.

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants