Skip to content

UserWarning: Field name "pk" in "EmbeddedJsonModel" shadows an attribute in parent "JsonModel" #833

Description

@woutdenolf

Reproduce:

pip install redis-om "pydantic==2.14.0a1"
python -W error -c "import redis_om"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File ".../python3.12/site-packages/redis_om/__init__.py", line 4, in <module>
    from .model.migrations import MigrationError, SchemaMigrator
  File ".../python3.12/site-packages/redis_om/model/__init__.py", line 2, in <module>
    from .model import (
  File ".../python3.12/site-packages/redis_om/model/model.py", line 3861, in <module>
    class EmbeddedJsonModel(JsonModel, abc.ABC):
  File ".../python3.12/site-packages/redis_om/model/model.py", line 2427, in __new__
    new_class: RedisModel = super().__new__(
                            ^^^^^^^^^^^^^^^^
  File ".../python3.12/site-packages/pydantic/_internal/_model_construction.py", line 249, in __new__
    set_model_fields(cls, config_wrapper=config_wrapper, ns_resolver=ns_resolver)
  File ".../python3.12/site-packages/pydantic/_internal/_model_construction.py", line 585, in set_model_fields
    fields, pydantic_extra_info, class_vars = collect_model_fields(
                                              ^^^^^^^^^^^^^^^^^^^^^
  File ".../python3.12/site-packages/pydantic/_internal/_fields.py", line 334, in collect_model_fields
    warnings.warn(
UserWarning: Field name "pk" in "EmbeddedJsonModel" shadows an attribute in parent "JsonModel"

This causes the UserWarning:

class ModelMeta(ModelMetaclass):

    def __new__(cls, name, bases, attrs, **kwargs):  # noqa C901
        ...

        new_class: RedisModel = super().__new__(cls, name, bases, attrs, **config_kwargs)   # >>> emits UserWarning


class RedisModel(BaseModel, abc.ABC, metaclass=ModelMeta):
    pk: Optional[str] = Field(
        default=None,
        primary_key=True,
        validate_default=True,
        index=True,
    )


class JsonModel(RedisModel, abc.ABC):
    ...


class EmbeddedJsonModel(JsonModel, abc.ABC):

    # Override pk to exclude it from serialization - embedded models don't need pks
    pk: Optional[str] = Field(default=None, exclude=True)  #  >>> causes UserWarning

I don't see anything related to shadowing parent attributes in the pydantic v2.14.0a1 changelog. This UserWarning does not get emitted for the current production release v2.13.4.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions