pydantic-extra-types
pydantic-extra-types copied to clipboard
CountryAlpha2 class example use-case results in mypy error
When the documented example use-case of CountryAlpha2 is used with Mypy, it results in a type error.
Code:
from pydantic import BaseModel
from pydantic_extra_types.country import CountryAlpha2
class Product(BaseModel):
made_in: CountryAlpha2
product = Product(made_in='ES')
print(product)
# > made_in='ES'
Results in:
Argument "made_in" to "Product" has incompatible type "str"; expected "CountryAlpha2" [arg-type]
Getting the same behavior with TimeZoneName. Both mypy and ty raise a type error.
I am using pydantic_extra_types.TimeZoneName in the normal way, per the docs.
Code
# model_test.py
from pydantic import BaseModel
from pydantic_extra_types.timezone_name import TimeZoneName
class TimeZonePolicyData(BaseModel):
time_zone_name: TimeZoneName
class AmericaNewYorkTimeZonePolicy:
def get_policy_data(self) -> TimeZonePolicyData:
return TimeZonePolicyData(
time_zone_name="America/New_York",
)
mypy
Running uv run mypy model_test.py gives:
model_test.py:14: error: Argument "time_zone_name" to "TimeZonePolicyData" has incompatible type "str"; expected "TimeZoneName" [arg-type]
Found 1 error in 1 file (checked 1 source file)
ty
Running uv run ty check model_test.py (tested on ty versions 0.0.1a6 and 0.0.1a7) gives
error[invalid-argument-type]: Argument is incorrect
--> model_test.py:14:13
|
12 | def get_policy_data(self) -> TimeZonePolicyData:
13 | return TimeZonePolicyData(
14 | time_zone_name="America/New_York",
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Expected `TimeZoneName`, found `Literal["America/New_York"]`
15 | )
|
info: rule `invalid-argument-type` is enabled by default