I just opened https://github.com/python/cpython/pull/30839 with a draft implementation. Feedback (especially on the documentation) is welcome.

El sáb, 22 ene 2022 a las 14:32, Jelle Zijlstra (<jelle.zijlstra@gmail.com>) escribió:
In the previous thread about reveal_type(), several people proposed also adding `typing.reveal_locals()`. In this thread I'd like to iron out any details and make sure we're all in agreement.

# Specification

We will add a new function `typing.reveal_locals()` with the following signature:

    def reveal_locals() -> None: ...

At runtime, this function will do nothing.

When a type checker encounters a call to reveal_locals(), it will emit the types of all variables in the local scope. For example:

    def f() -> None:
        a = 1
        b = "x"
        reveal_locals()

When typechecked, this may produce:

    Revealed local types are:
        a: builtins.int
        b: builtins.str

# Motivation

`reveal_locals()` is already implemented in mypy (https://mypy.readthedocs.io/en/stable/common_issues.html#displaying-the-type-of-an-expression) and pyright (undocumented). It is not as commonly used as reveal_type(), but can be useful for debugging type checker behavior.

# Open questions

What should the runtime behavior be? We could do something like sys._getframe(1).f_locals to get the calling function's locals and print them, but that isn't code I'd like to write.