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

Fix issue with in-place modification of FieldInfo #4067

Merged
merged 4 commits into from
May 13, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions changes/4067-adriangb.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fix in-place modification of FieldInfo that made it impossible to use PEP 593 type aliases
adriangb marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 2 additions & 0 deletions pydantic/fields.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import copy
from collections import Counter as CollectionCounter, defaultdict, deque
from collections.abc import Hashable as CollectionsHashable, Iterable as CollectionsIterable
from typing import (
Expand Down Expand Up @@ -446,6 +447,7 @@ def _get_field_info(
raise ValueError(f'cannot specify multiple `Annotated` `Field`s for {field_name!r}')
field_info = next(iter(field_infos), None)
if field_info is not None:
field_info = copy.copy(field_info)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any chance we might need deepcopy just to be sure?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

e.g. perhaps if we had nested annoated?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so. The only time we'd need deepcopy is if we were modifying an attribute of FieldInfo in place, e.g. field_info.default.foobar = 123, but I don't see that happening anywhere.

field_info.update_from_config(field_info_from_config)
if field_info.default is not Undefined:
raise ValueError(f'`Field` default cannot be set in `Annotated` for {field_name!r}')
Expand Down
20 changes: 20 additions & 0 deletions tests/test_annotated.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import List

import pytest
from typing_extensions import Annotated

Expand Down Expand Up @@ -132,3 +134,21 @@ class Config:
assert Foo.schema(by_alias=True)['properties'] == {
'a': {'title': 'A', 'description': 'descr', 'foobar': 'hello', 'type': 'integer'},
}


def test_annotated_alias() -> None:
# https://github.com/samuelcolvin/pydantic/issues/2971

StrAlias = Annotated[str, Field(max_length=3)]
IntAlias = Annotated[int, Field(default_factory=lambda: 2)]

Nested = Annotated[List[StrAlias], Field(description='foo')]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an attempt to test #3714 without bringing in all of the code there, I think it emulates the issue but 👀 on this would be good


class MyModel(BaseModel):
a: StrAlias = 'abc'
b: StrAlias
c: IntAlias
d: IntAlias
e: Nested

assert MyModel(b='def', e=['xyz']) == MyModel(a='abc', b='def', c=2, d=2, e=['xyz'])