Skip to content

Commit

Permalink
Improve warning message when a field name shadows a field in a parent…
Browse files Browse the repository at this point in the history
… model (#9105)
  • Loading branch information
chan-vince committed Mar 26, 2024
1 parent af3d335 commit e3b4633
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
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

0 comments on commit e3b4633

Please sign in to comment.