From bc83af9d8862efa22da6cfbcd14cb3d6945e8b34 Mon Sep 17 00:00:00 2001 From: Simon Lanzmich <48890288+slanzmich@users.noreply.github.com> Date: Tue, 5 Dec 2023 06:39:25 +0100 Subject: [PATCH] Fix type annotation of `ModelMetaclass.__prepare__` During class creation, the namespace returned by `__prepare__` is populated when the class body is executed. Therefore, it must be mutable. The previous, incorrect annotation was fixed in the typeshed in https://github.com/python/typeshed/pull/11093 --- pydantic/_internal/_model_construction.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pydantic/_internal/_model_construction.py b/pydantic/_internal/_model_construction.py index d72493b0392..318eb54b2ac 100644 --- a/pydantic/_internal/_model_construction.py +++ b/pydantic/_internal/_model_construction.py @@ -8,7 +8,7 @@ from abc import ABCMeta from functools import partial from types import FunctionType -from typing import Any, Callable, Generic, Mapping +from typing import Any, Callable, Generic, MutableMapping import typing_extensions from pydantic_core import PydanticUndefined, SchemaSerializer @@ -216,7 +216,7 @@ def __getattr__(self, item: str) -> Any: raise AttributeError(item) @classmethod - def __prepare__(cls, *args: Any, **kwargs: Any) -> Mapping[str, object]: + def __prepare__(cls, *args: Any, **kwargs: Any) -> MutableMapping[str, object]: return _ModelNamespaceDict() def __instancecheck__(self, instance: Any) -> bool: