redis / redis/redis-om-python

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

Open
#833 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1.3k
Forks
128
PR merge metrics
No merged PRs in 30d

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in redis_om/model/model.py, especially ModelMeta, RedisModel, JsonModel, and EmbeddedJsonModel, then reproduce the import with Python warnings treated as errors and pydantic 2.14.0a1. Compare the behavior with pydantic 2.13.4; done means the import no longer emits this warning while EmbeddedJsonModel.pk remains excluded from serialization.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, redis
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.