El sáb, 22 ene 2022 a las 14:56, Martin DeMello (<mdemello@google.com>) escribió:
I'm strongly in favour of runtime behaviour being either "do nothing" or "raise an error", on the theory that these are analogous to debug printfs that you want to insert temporarily while debugging and then remove.

Raising an error would be inconvenient, because one of the use cases from the reveal_type() thread is to alternate between running the type checker and your test suite.

In my mind the two reasonable options for runtime behavior are:
- Do nothing at all (current preference)
- Use getframe() hackery to get the locals of the calling frame and print them

But if someone has other ideas, I'm happy to adapt the proposal.

 

martin

On Sat, Jan 22, 2022 at 2:32 PM Jelle Zijlstra <jelle.zijlstra@gmail.com> wrote:
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.

_______________________________________________
Typing-sig mailing list -- typing-sig@python.org
To unsubscribe send an email to typing-sig-leave@python.org
https://mail.python.org/mailman3/lists/typing-sig.python.org/
Member address: mdemello@google.com