Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions stdlib/@tests/test_cases/builtins/check_locals.py
Original file line number Diff line number Diff line change
@@ -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()
11 changes: 11 additions & 0 deletions stdlib/@tests/test_cases/check_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
4 changes: 3 additions & 1 deletion stdlib/builtins.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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]):
Expand Down
4 changes: 3 additions & 1 deletion stdlib/types.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading