Repository: erhosen-libs/pydjantic (language: python)

Issue:
Pydantic serializer warnings, such as "Expected `multi-host-url` but got `dict` - serialized value may not be as expected"
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


Discussion / hints:
Hey @earshinov 👋🏻 
Can you share your Django version?
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
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]`
````python
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.
> 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! 

Fix patch:
diff --git a/demo/demo/settings.py b/demo/demo/settings.py
index f1c68eb..49c6955 100644
--- a/demo/demo/settings.py
+++ b/demo/demo/settings.py
@@ -11,7 +11,7 @@
 """
 
 from pathlib import Path
-from typing import Dict, List
+from typing import Dict, List, Union
 
 from pydantic import Field
 from pydantic.v1.fields import Undefined
@@ -26,7 +26,7 @@
 
 class DatabaseSettings(BaseDBConfig):
     # https://docs.djangoproject.com/en/3.1/ref/settings/#databases
-    default: str = Field(
+    default: Union[str, Dict] = Field(
         default=str(f"sqlite:///{BASE_DIR}/db.sqlite3"),
         validation_alias="DATABASE_URL",
         conn_max_age=0,


Test patch:
diff --git a/tests/test_db_config.py b/tests/test_db_config.py
index 1b64647..9251322 100644
--- a/tests/test_db_config.py
+++ b/tests/test_db_config.py
@@ -1,4 +1,4 @@
-from typing import Dict, Optional
+from typing import Dict, Optional, Union
 
 from pydantic import Field, PostgresDsn
 from pydantic_settings import BaseSettings, SettingsConfigDict
@@ -9,7 +9,7 @@
 
 def test_empty():
     class DatabaseConfig(BaseDBConfig):
-        default: Optional[PostgresDsn] = None
+        default: Union[Optional[PostgresDsn], Dict] = None
         replica: Optional[Dict] = None
 
     db_settings = DatabaseConfig()
@@ -97,7 +97,7 @@ class DatabaseConfig(BaseDBConfig):
 
 def test_dsn_and_exact_config():
     class DatabaseConfig(BaseDBConfig):
-        default: str = Field(default="postgres://user:password@hostname:5432/dbname")
+        default: Union[str, Dict] = Field(default="postgres://user:password@hostname:5432/dbname")
         replica: PostgresDB = PostgresDB()
 
     db_settings = DatabaseConfig()
diff --git a/tests/test_to_django.py b/tests/test_to_django.py
index 2796c5e..bc0c8cb 100644
--- a/tests/test_to_django.py
+++ b/tests/test_to_django.py
@@ -1,4 +1,4 @@
-from typing import Any, Dict, List
+from typing import Any, Dict, List, Union
 
 from deepdiff import DeepDiff
 from pydantic import Field, PostgresDsn, SecretStr, ValidationInfo, computed_field, field_validator
@@ -9,7 +9,7 @@
 
 def test_to_django_settings():
     class DatabaseConfig(BaseDBConfig):
-        default: PostgresDsn = Field(
+        default: Union[PostgresDsn, Dict] = Field(
             default="postgres://user:password@hostname:5432/dbname",
             validation_alias="DATABASE_URL",
         )