Skip to content

Commit

Permalink
Fix ZoneInfo with various invalid types (#10408)
Browse files Browse the repository at this point in the history
  • Loading branch information
sydney-runkle committed Sep 17, 2024
1 parent ea6115d commit c0a288f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pydantic/_internal/_generate_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -1657,7 +1657,7 @@ def validate_str_is_valid_iana_tz(value: Any, /) -> ZoneInfo:
return value
try:
return ZoneInfo(value)
except (ZoneInfoNotFoundError, ValueError):
except (ZoneInfoNotFoundError, ValueError, TypeError):
raise PydanticCustomError('zoneinfo_str', 'invalid timezone: {value}', {'value': value})

metadata = build_metadata_dict(js_functions=[lambda _1, _2: {'type': 'string', 'format': 'zoneinfo'}])
Expand Down
10 changes: 9 additions & 1 deletion tests/test_types_zoneinfo.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from datetime import timezone
from typing import Union

import pytest

from pydantic import BaseModel, ValidationError
from pydantic import BaseModel, ConfigDict, TypeAdapter, ValidationError

zoneinfo = pytest.importorskip('zoneinfo', reason='zoneinfo requires >=3.9')

Expand Down Expand Up @@ -48,3 +51,8 @@ def test_zoneinfo_json_schema():
'properties': {'tz': {'type': 'string', 'format': 'zoneinfo', 'title': 'Tz'}},
'required': ['tz'],
}


def test_zoneinfo_union() -> None:
ta = TypeAdapter(Union[zoneinfo.ZoneInfo, timezone], config=ConfigDict(arbitrary_types_allowed=True))
assert ta.validate_python(timezone.utc) is timezone.utc

0 comments on commit c0a288f

Please sign in to comment.