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

Improve warning message when a field name shadows a field in a parent model #9105

Merged
merged 3 commits into from Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion pydantic/_internal/_fields.py
Expand Up @@ -193,7 +193,8 @@ def collect_model_fields( # noqa: C901
# on the class instance.
continue
warnings.warn(
f'Field name "{ann_name}" shadows an attribute in parent "{base.__qualname__}"; ',
f'Field name "{ann_name}" in "{cls.__qualname__}" shadows an attribute in parent '
f'"{base.__qualname__}"',
UserWarning,
)

Expand Down
4 changes: 2 additions & 2 deletions tests/test_main.py
Expand Up @@ -3131,12 +3131,12 @@ def __pydantic_init_subclass__(cls, **kwargs: Any):
class One(Model):
foo: str = 'abc'

with pytest.warns(UserWarning, match=r'"foo" shadows an attribute in parent ".*One"'):
with pytest.warns(UserWarning, match=r'"foo" in ".*Two" shadows an attribute in parent ".*One"'):

class Two(One):
foo: str

with pytest.warns(UserWarning, match=r'"foo" shadows an attribute in parent ".*One"'):
with pytest.warns(UserWarning, match=r'"foo" in ".*Three" shadows an attribute in parent ".*One"'):

class Three(One):
foo: str = 'xyz'
Expand Down