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

Pydantic serializer warnings, such as "Expected multi-host-url but got dict - serialized value may not be as expected" #31

Closed
earshinov opened this issue Feb 13, 2024 · 4 comments · Fixed by #32

Comments

@earshinov
Copy link

earshinov commented Feb 13, 2024

Even when running tests:

tests/test_to_django.py::test_to_django_settings
  D:\NVIDIA\third-party\_python\pydjantic\.venv\Lib\site-packages\pydantic\main.py:308: UserWarning: Pydantic serializer warnings:
    Expected `multi-host-url` but got `dict` - serialized value may not be as expected
    return self.__pydantic_serializer__.to_python(

tests/test_db_config.py::test_dsn_and_exact_config
  D:\NVIDIA\third-party\_python\pydjantic\.venv\Lib\site-packages\pydantic\main.py:308: UserWarning: Pydantic serializer warnings:
    Expected `str` but got `dict` - serialized value may not be as expected
    return self.__pydantic_serializer__.to_python(

Seems to come from database configuration, e.g. (in test_dsn_and_exact_config):

class DatabaseConfig(BaseDBConfig):
    # declared as str, but actually deserialized as dict (Django database configuration)❓
    default: str = Field(default="postgres://user:password@hostname:5432/dbname")
    replica: PostgresDB = PostgresDB()

Applications still run, however:

  • Users (like myself) might think that there is something wrong with their Django settings
  • Application will fail to start under -Werror python interpreter flag
@erhosen
Copy link
Collaborator

erhosen commented Feb 14, 2024

Hey @earshinov 👋🏻
Can you share your Django version?

@earshinov
Copy link
Author

earshinov commented Feb 14, 2024

Hey @erhosen, sure!

Tests above are run with

  • Python 3.11.3
  • Django 4.2.7 (as specified in poetry.lock)

I observe the same errors in my project, running

  • Python 3.10.11
  • Django 4.1.4

@worldworm
Copy link
Contributor

Hi there 👋🏻

This may be a pragmatic approach and may not fix the root cause, but it will get rid of the warnings: Just set the type of your DatabaseConfig to str | dict or str | dict[str, Any]

class DatabaseSettings(BaseDBConfig):
    """https://docs.djangoproject.com/en/dev/ref/settings/#databases"""
    default: str | dict[str, Any] = Field(
        default=str(f"sqlite:///{BASE_DIR}/db.sqlite3"),
        validation_alias="DATABASE_URL",
        conn_max_age=0,
        ssl_require=False,
    )
    model_config = SettingsConfigDict(env_file=BASE_DIR / ".env")

(Tested with Python 3.10.12, Django 5.0.6, pydjantic 1.1.4)

I'm not sure if this might be confusing, but if you want I can do a quick pr to add this to the tests and demo.

@erhosen
Copy link
Collaborator

erhosen commented May 23, 2024

I'm not sure if this might be confusing, but if you want I can do a quick pr to add this to the tests and demo.

Thanks @worldworm, it would be nice to get rid of the warning!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants