core[patch]: check for model_fields attribute (#25108)

`__fields__` raises a warning in pydantic v2
This commit is contained in:
ccurme 2024-08-07 16:32:56 -04:00 committed by GitHub
parent 6e9a8b188f
commit 803eba3163
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 5 additions and 1 deletions

View File

@ -1666,7 +1666,10 @@ def _get_all_basemodel_annotations(
for name, param in inspect.signature(cls).parameters.items():
# Exclude hidden init args added by pydantic Config. For example if
# BaseModel(extra="allow") then "extra_data" will part of init sig.
if (fields := getattr(cls, "__fields__", {})) and name not in fields:
if (
fields := getattr(cls, "model_fields", {}) # pydantic v2+
or getattr(cls, "__fields__", {}) # pydantic v1
) and name not in fields:
continue
annotations[name] = param.annotation
orig_bases: Tuple = getattr(cls, "__orig_bases__", tuple())

View File

@ -1746,6 +1746,7 @@ def test__is_message_content_type(obj: Any, expected: bool) -> None:
@pytest.mark.skipif(PYDANTIC_MAJOR_VERSION != 2, reason="Testing pydantic v2.")
@pytest.mark.parametrize("use_v1_namespace", [True, False])
@pytest.mark.filterwarnings("error")
def test__get_all_basemodel_annotations_v2(use_v1_namespace: bool) -> None:
A = TypeVar("A")