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
Using a default Field
will crash when the type annotation references a class of the same name as the field
#9093
Comments
@lmmx My suggestion is to follow the alternatives mentioned here. I know this isn't perfect but Pydantic is limited by the annotation evaluation of Python (assignments are evaluated before annotations, meaning when Python tries to evaluate To catch this kind of issues, I wrote a Flake8 plugin (rule can be found here). This detection could be implemented in Pydantic but it would require parsing the AST which isn't an ideal solution considering it would slow down model creation. |
Thanks for the prompt triage @sydney-runkle and explanation @Viicos ! Best team 💪 Due to the specific [imposed] requirements I have in this project:
mean that I can't use the alternatives but good to know! I've opted to go with an AliasGenerator for now which seems to do the trick for my needs, still wanted to report it back to base :-) Plugin looks great too, I use flake8 all the time (even though my pre-commit is in ruff now so cool to see the plans for that!) I went back and checked your warning that a stringified annotation - one example is the class Identifier(BaseModel):
Crowding: Crowding = None Indeed you were right 😮💨 >>> from tubeulator.generated.Journey import Identifier
>>> Identifier.model_validate({})
Identifier(Id=None, Name=None, Uri=None, FullName=None, Type=None, Crowding=None, RouteType=None, Status=None)
>>> Identifier.model_validate({"crowding": {"passengerFlows": [], "trainLoadings": []}})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/louis/miniconda3/envs/tubeulator/lib/python3.10/site-packages/pydantic/main.py", line 509, in model_validate
return cls.__pydantic_validator__.validate_python(
pydantic_core._pydantic_core.ValidationError: 1 validation error for Identifier
crowding
Input should be None [type=none_required, input_value={}, input_type=dict]
For further information visit https://errors.pydantic.dev/2.6/v/none_required Thanks for the specific warning there, I thought I was good to go ahead ! I'll have a think about the other workarounds Can confirm that the simple approach (giving the model type another name after declaring it, and then refer to it by that name in the field annotation) works for me! 🎉 >>> from tubeulator.generated.Journey import Identifier
>>> Identifier.model_validate({"crowding": {"passengerFlows": [], "trainLoadings": []}})
Identifier(Id=None, Name=None, Uri=None, FullName=None, Type=None, Crowding=Crowding(PassengerFlows=[], TrainLoadings=[]), RouteType=None, Status=None) |
My plan is to implement these rules at Ruff at some point (but still need to a bit more familiar with Rust first). But yeah aliases seems like a good option |
Initial Checks
Description
This is a very odd and specific bug, I've tried changing aspects of it and have whittled it down to the reproducible essence, as in this issue's title.
Field
, even when equivalent to not usingField
(i.e.= Field(None)
rather than= None
)This isn't some contrived example, it came up when trying to codegen for the TfL API.
I think I can work around this, it's a shame that I can't use aliases in this way though.
Example Code
Python, Pydantic & OS Version
The text was updated successfully, but these errors were encountered: