-
-
Notifications
You must be signed in to change notification settings - Fork 70
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.Json[dict[str, str]] | None raise error (BUG) #135
Comments
Thanks @aleksey925 for reporting this issue 🙏
You can solve the error by changing the |
Thanks for the help. Yes, I use almost this approach now. Maybe describe this behavior in the documentation? I think this will make life much easier. Another solution: from pydantic import Json, Field
from pydantic_settings import BaseSettings, SettingsConfigDict
class Settings(BaseSettings):
data: Json[dict[str, str] | None] = Field(default='null')
model_config = SettingsConfigDict(
env_file='.env',
env_file_encoding='utf-8',
)
s = Settings()
print(s.data)
print(type(s.data)) |
Yeah, I think we can document this behavior. I would be happy to review a PR if you want to work on it |
@hramezani it might be reasonable to make |
@hramezani I rechecked the code with hint like this As a result, the current implementation does not allow you to define a field with different possible schemas. I think this is a really important thing. In development, it is quite common to declare that a field can contain either a dictionary or None. |
@aleksey925 does the following not work for you? from pydantic import Json, Field
from pydantic_settings import BaseSettings, SettingsConfigDict
class Settings(BaseSettings):
data: dict[str, str] | None = Field(default=None)
model_config = SettingsConfigDict(
env_file='.env',
env_file_encoding='utf-8',
)
s = Settings()
print(s.data)
#> {'key': 'value'}
print(type(s.data))
#> <class 'dict'> As @hramezani said, pydantic-settings is already automatically treating the type as JSON-encoded, so applying the I'll also note that you can put the following in the
and loads as I think you expected. Obviously that's not great as a general solution though. |
@aleksey925 I've created a PR to fix the problem. |
Description
The following code raise an error
.env file
Example Code
Selected Assignee: @dmontagu
The text was updated successfully, but these errors were encountered: