diff --git a/stdlib/@tests/test_cases/builtins/check_locals.py b/stdlib/@tests/test_cases/builtins/check_locals.py new file mode 100644 index 000000000000..c8a079f5ea8e --- /dev/null +++ b/stdlib/@tests/test_cases/builtins/check_locals.py @@ -0,0 +1,40 @@ +from __future__ import annotations + +from collections import UserDict +from collections.abc import MutableMapping +from typing import Any +from typing_extensions import assert_type + + +def check_locals_is_a_mutable_mapping() -> None: + # `locals()` is not necessarily a `dict`: at class scope it returns whatever the + # metaclass's `__prepare__` returned. It still supports the `MutableMapping` API. + # See #14584. + assert_type(locals(), MutableMapping[str, Any]) + assert_type(dict(locals()), dict[str, Any]) + + +def check_mutating_the_returned_mapping() -> None: + namespace = locals() + namespace["key"] = 1 + namespace.setdefault("other", 2) + namespace.update({"third": 3}) + namespace.pop("key") + assert "other" in namespace + + +def check_unpacking_locals() -> None: + def func(**kwargs: Any) -> None: ... + + func(**locals()) + + +class CustomPrepMeta(type): + @classmethod + def __prepare__(cls, name: str, bases: tuple[type, ...], /, **kwds: Any) -> UserDict[str, Any]: + return UserDict() + + +class WithCustomNamespace(metaclass=CustomPrepMeta): + # In this class body, `locals()` is the `UserDict` returned by `__prepare__`. + namespace = locals() diff --git a/stdlib/@tests/test_cases/check_types.py b/stdlib/@tests/test_cases/check_types.py index a9c2eeddbfdf..6e18459866b1 100644 --- a/stdlib/@tests/test_cases/check_types.py +++ b/stdlib/@tests/test_cases/check_types.py @@ -3,6 +3,7 @@ import sys import types from collections import UserDict +from collections.abc import MutableMapping from typing import Any, Literal, TypeVar from typing_extensions import assert_type @@ -62,6 +63,16 @@ def foo(self) -> None: self._value = None +# test: `types.FrameType.f_locals` (see #14584) +frame_locals = sys._getframe().f_locals +assert_type(dict(frame_locals), dict[str, Any]) +frame_locals["key"] = 1 +if sys.version_info >= (3, 15): + assert_type(frame_locals, types.FrameLocalsProxyType | dict[str, Any]) +else: + assert_type(frame_locals, MutableMapping[str, Any]) + + # check that NotImplemented is treated as an "Any" x: int = NotImplemented diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index da1d9ed6eb4d..6182a4b507d6 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -1939,7 +1939,9 @@ def len(obj: Sized, /) -> int: ... license: _sitebuiltins._Printer -def locals() -> dict[str, Any]: ... +# At class scope, this is the mapping returned by the metaclass's `__prepare__`, +# which need not be a `dict`. See #14584. +def locals() -> MutableMapping[str, Any]: ... @disjoint_base class map(Generic[_S]): diff --git a/stdlib/types.pyi b/stdlib/types.pyi index 39aaa7e9685f..f205c5fbde06 100644 --- a/stdlib/types.pyi +++ b/stdlib/types.pyi @@ -577,12 +577,14 @@ class FrameType: @property def f_lineno(self) -> int | MaybeNone: ... + # In optimized scopes on Python 3.13+, this is a PEP 667 write-through proxy, which is not + # exposed as a public type before 3.15. See #14584. if sys.version_info >= (3, 15): @property def f_locals(self) -> FrameLocalsProxyType | dict[str, Any]: ... else: @property - def f_locals(self) -> dict[str, Any]: ... + def f_locals(self) -> MutableMapping[str, Any]: ... f_trace: Callable[[FrameType, str, Any], Any] | None f_trace_lines: bool