-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Unwanted and incorrect type coercion in list #3189
Comments
Use a validator to check each list item - remember pydantic is a parsing library first and a validation library second; i.e. its aim is to guarantee output data. Try this: class RustStatementNode(RustAstNode):
nodes: t.List[RustAstNode] = Field(default_factory=list)
@validator('nodes', each_item=True)
def check_node_type(cls, v):
assert isinstance(v, RustAstNode)
return v adjust the validator as necessary if you want to silently ignore non-conforming types. |
Would you accept a pull request if I change the behavior so that it raises an exception here? |
I don't see a bug here, or even unexpected behaviour
|
you wouldn't expect a BulkNode to survive validation
BulkNode shouldn’t survive validation, but I expect an Exception, not a silent conversion.
…On 21 Apr 2022, 10:40 PM +0800, samuelcolvin/pydantic ***@***.***>, wrote:
you wouldn't expect a BulkNode to survive validation
|
Use validators. Pydantic looks at attributes primarily, not attributes. |
Checks
Bug
Output of
python -c "import pydantic.utils; print(pydantic.utils.version_info())"
:Basically, I have
list[Foo]
, but I then pass[Foo(), Bar('something'), Foo()]
and got[Foo(), BaseFoo(), Foo()]
. The validation fails silently and did a wanted coercion. This behavior took me a long time to catch caused bug.Here is a relative question from stackoverflow
https://stackoverflow.com/questions/66019853/pydantic-prevent-conversion-of-incorrect-type
#1423
#1098
The text was updated successfully, but these errors were encountered: