-
Notifications
You must be signed in to change notification settings - Fork 27
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
field "model_flags" conflicts with Pydantic 2 protected namespaces #267
Comments
I just tripped over this as well. FWIW, I was able to suppress the warnings by placing this above the #
# Suppress pydantic warnings
import warnings
warnings.filterwarnings( # pylint: disable=wrong-import-position
action="ignore",
category=UserWarning,
module="pydantic",
)
#
# Normal code resumes
from diffsync import Adapter Obvs this should be fixed Correctly™ in |
Hi. With v3.0.0, nautobot-app-ssot now moves to diffsync 2.0, making this warning appear and slowing down the process A LOT in nautobot. Any fix in perspective? |
Can you elaborate what you mean with this? The warning itself shouldn't slow down anything. Can you provide an example with timings? |
This might be an illusion, but I felt like the very verbose output in the debug console slowed down the process. I do not have any data to back this up, sorry. |
@Kircheneer this is related to this error message:
|
I've tripped over this as well. IMO:
|
I did a quick POC of how DiffSync could rename model_flags, keep backwards compatibility, but also generate a useful deprecation warning for DiffSync users. import warnings
from pydantic import BaseModel, model_validator
class DiffSyncModel(BaseModel):
instance_flags: str # NB: use real-type, this is just a place-holder for the proof of concept
@model_validator(mode="before")
@classmethod
def _translate_model_flags(cls, data:any) -> any:
if isinstance(data, dict) and "model_flags" in data:
data["instance_flags"] = data["model_flags"]
del data["model_flags"]
warnings.warn("model_flags is deprecated, use instance_flags instead", DeprecationWarning,)
return data
@property
def model_flags(self):
warnings.warn("model_flags is deprecated, use instance_flags instead", DeprecationWarning,)
return self.instance_flags
@model_flags.setter
def model_flags(self, value):
warnings.warn("model_flags is deprecated, use instance_flags instead", DeprecationWarning,)
self.instance_flags = value
print("=== No warnings ===")
obj1 = DiffSyncModel(instance_flags="foo")
print(obj1.instance_flags)
obj1.instance_flags = "bar"
print("=== Generates warning at instantiation time ===")
obj2 = DiffSyncModel(model_flags="asdf")
print("=== Generates warning on read ===")
obj3 = DiffSyncModel(instance_flags="foo")
print(obj3.model_flags)
print("=== Generates warning on write ===")
obj4 = DiffSyncModel(instance_flags="foo")
obj1.model_flags = "bar" This is what's generated when I run the example code:
The only thing I'm not wild about is using a model_validator in a library like this. |
Environment
Observed Behavior
Getting warning logs from Pydantic about protected namespace violations within the DiffSync library
Expected Behavior
No logging output at the warning level
Steps to Reproduce
python3 -i
from diffsync import DiffSyncModel
Extra Information
Pydantic introduces protected namespaces in their new documentation that places warning logs for things like
model_
fields documentation herepotential workaround is to use the ConfigDict to set protected_namespaces to an empty value.
However, this does not prevent the log entries from the diffsync BaseModel here:
diffsync/diffsync/__init__.py
Line 110 in 13f5150
The text was updated successfully, but these errors were encountered: