Skip to content
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

copy_on_model_validation doesn't work for validate_arguments config #3284

Closed
assaf-orca opened this issue Oct 3, 2021 · 3 comments
Closed
Assignees
Labels
bug V1 Bug related to Pydantic V1.X

Comments

@assaf-orca
Copy link

Bug

Output of python -c "import pydantic.utils; print(pydantic.utils.version_info())":

pydantic version: 1.8.2
pydantic compiled: True
install path: /usr/local/lib/python3.8/site-packages/pydantic
python version: 3.8.12 (default, Sep  3 2021, 02:24:44)  [GCC 10.2.1 20210110]
platform: Linux-5.10.25-linuxkit-x86_64-with-glibc2.2.5
optional deps. installed: ['typing-extensions']
from typing import Set

from pydantic import validate_arguments


my_set = {"assaf"}
print(f"{id(my_set)=}")

def a(b: Set[str]):
    print(f"first {id(b)=}")


a(my_set)


@validate_arguments
def a(b: Set[str]):
    print(f"second {id(b)=}")


a(my_set)


@validate_arguments(config=dict(copy_on_model_validation=False))
def a(b: Set[str]):
    print(f"third {id(b)=}")


a(my_set)

""" 
... ... Model Config

copy_on_model_validation
whether or not inherited models used as fields should be reconstructed (copied) on validation instead of being kept untouched (default: True)
"""

output:

id(my_set)=139856697296704
first id(b)=139856697296704
second id(b)=139856688179456
third id(b)=139856688107584

I think with the decorator validate_arguments it's need to be same as the original python behavior, but if it doesn't the config should support it.

@hb2638
Copy link

hb2638 commented Feb 19, 2023

This bug is still present in 1.10.5. Thx for reporting!!

@hramezani
Copy link
Member

@assaf-orca When we validate a set, we always re-create it - same as we do with pydantic models, so the id changes
it's really a fundamental limitation/feature of Pydantic

@hb2638
Copy link

hb2638 commented Jun 27, 2023

Why was this bug closed?
The bug is still present in the code.

"When we validate a set, we always re-create it" contradicts copy_on_model_validation being set to 'none'.

The code/validator should fault at when it's unable to honor the copy_on_model_validation of 'none'. (E.x.: It's given an iterator - or - you would need to convert the items in the sequence to make the contract work) instead of silently breaking the contract/requirement.

Without that type of enforcement, you'll just have people shooting themselves in the foot left and right because changes to the collection or object are not being propagated to the caller.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V1 Bug related to Pydantic V1.X
Projects
None yet
Development

No branches or pull requests

3 participants