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
list is not equivalent to typing.List under combination of generics and futures #6130
Comments
I've found a couple more illustrations - one is a simplified version of the original minimal, and second illustrates the same bug with Dict -> dict List version from typing import Generic, List, TypeVar
from pydantic import BaseModel
from pydantic.generics import GenericModel
ChangingType = TypeVar("ChangingType")
class Outer(GenericModel, Generic[ChangingType]):
test: list[ChangingType]
OuterNew = Outer["Inner"]
class Inner(BaseModel):
new: int
OuterNew.update_forward_refs()
out = OuterNew.parse_obj({"test": [{"new": 1}]})
print(out) Dict version from typing import Dict, Generic, List, TypeVar
from pydantic import BaseModel
from pydantic.generics import GenericModel
ChangingType = TypeVar("ChangingType")
class Outer(GenericModel, Generic[ChangingType]):
test: dict[str, ChangingType]
OuterNew = Outer["Inner"]
class Inner(BaseModel):
new: int
OuterNew.update_forward_refs()
out = OuterNew.parse_obj({"test": {"t": {"new": 1}}})
print(out) |
https://github.com/pydantic/pydantic/blob/1.10.X-fixes/pydantic/typing.py#L218 Think this is relevent - just testing Update: Tested this function - seems to be working ok |
Ok think I might have it: https://github.com/pydantic/pydantic/blob/1.10.X-fixes/pydantic/generics.py#L293 In this line the origins are correctly found as List or list, but the difference is that... List['test'] -> List[ForwardRef('test')] |
This fork fixes the bug - it unfortunately also breaks one of the tests for discriminated union - not sure how to get round this yet: |
This works in Pydantic v2.3.0. from typing import Generic, List, TypeVar
from pydantic import BaseModel
ChangingType = TypeVar("ChangingType")
class Outer(BaseModel, Generic[ChangingType]):
fields_: list[ChangingType] # Swapping list -> List on this line fixes the bug
outer = Outer["Inner"]
class Inner(BaseModel):
new: list[outer]
out = Inner.model_validate({"new": [{"fields_": []}]})
print(out)
# new=[Outer[Inner](fields_=[])] There is even no need to use |
Initial Checks
Description
This is a strange one - under the below conditions, exchanging
typing.List
for the more modernlist
produces the below error. I can't reproduce in a simpler example than this - it looks to me like a bug in pydantic:I've also displayed the minimal example below - thanks!
Example Code
Python, Pydantic & OS Version
Affected Components
.dict()
and.json()
construct()
, pickling, private attributes, ORM modeThe text was updated successfully, but these errors were encountered: