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

Setting a default value for ZoneInfo does not work #480

Closed
ac3673 opened this issue Nov 21, 2024 · 3 comments
Closed

Setting a default value for ZoneInfo does not work #480

ac3673 opened this issue Nov 21, 2024 · 3 comments
Assignees

Comments

@ac3673
Copy link

ac3673 commented Nov 21, 2024

When setting a default value for ZoneInfo a different timezone is used.

Python 3.12.1 (main, Jan  8 2024, 05:53:39) [Clang 17.0.6 ] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pydantic_settings
>>> from zoneinfo import ZoneInfo
>>> from pydantic_settings import BaseSettings
>>> class Test(BaseSettings):
...     tz: ZoneInfo = ZoneInfo("Europe/London")
... 
>>> Test()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/acuthbert2/dev/ves/scripts/.venv/lib/python3.12/site-packages/pydantic_settings/main.py", line 167, in __init__
    super().__init__(
  File "/home/acuthbert2/dev/ves/scripts/.venv/lib/python3.12/site-packages/pydantic/main.py", line 212, in __init__
    validated_self = self.__pydantic_validator__.validate_python(data, self_instance=self)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
pydantic_core._pydantic_core.ValidationError: 1 validation error for Test
tz
  invalid timezone: :Asia/Shanghai [type=zoneinfo_str, input_value=':Asia/Shanghai', input_type=str]
>>> pydantic_settings.__version__
'2.6.1'
  • pydantic-settings version: 2.6.1
  • python version: 3.12.1
  • platform: DISTRIB_ID=Ubuntu
    DISTRIB_RELEASE=22.04
    DISTRIB_CODENAME=jammy
    DISTRIB_DESCRIPTION="Ubuntu 22.04.2 LTS"
@hramezani
Copy link
Member

hramezani commented Nov 21, 2024

It seems that you have a tz env variable with the value :Asia/Shanghai. The :Asia/Shanghai is an invalid timezone(because of : at the beginning), which is the reason for the error.

@ac3673
Copy link
Author

ac3673 commented Nov 22, 2024

@hramezani I don't (or not that I know).

>>> import os
>>> os.getenv("tz")
>>>

However naming the variable something other than tz does work correctly, so it does look to be something to do with my specific environment rather than a bug. Thanks

@ac3673
Copy link
Author

ac3673 commented Nov 22, 2024

Likely due to my environment, closing.

@ac3673 ac3673 closed this as completed Nov 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants