You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When #136 will be fixed, it will be nice to have something that could be used in place of issubclass for "experimentation" with types. Mypy already provides such a function -- reveal_type. I think it works very well and maybe it is a good idea to add a recommendation to PEP 484 that static type checkers should provide a function to reveal the inferred types? Or maybe even add a function
defreveal_type(obj):
print('Runtime type is:', getattr(obj, '__orig_class__', type(obj)))
returnobj
to typing.py so that it can be used for this purpose by other type checkers.
One idea I have in addition to @ilevkivskyi's code above is to use the warnings module instead of just printing the output. This allows filtering, adds special handling by test runners etc.
Hm. In mypy IIRC reveal_type(x) returns x. This makes it possible to wrap arbitrary subexpressions in it without changing their meaning. I would regret losing this.
Activity
srittau commentedon Nov 4, 2021
I wish
reveal_type()was a Python builtin. Often when I test code, I have to temporarily comment out reveal_type calls, which is a bit annoying.sobolevn commentedon Nov 4, 2021
I use this hack:
gvanrossum commentedon Nov 4, 2021
I support making it a builtin. File a bpo issue and write a PR and I will add it.
srittau commentedon Nov 4, 2021
One idea I have in addition to @ilevkivskyi's code above is to use the warnings module instead of just printing the output. This allows filtering, adds special handling by test runners etc.
erictraut commentedon Nov 5, 2021
If
reveal_typeis added as an actual builtin call, its full type signature will need to be formalized.Would you be open to
reveal_typereturning a str (the actual revealed type) rather than returning the value passed as an input argument?Pyright currently implements
reveal_typesuch that the return type of the call is a literal string value of the revealed type.This allows us to use it in test cases, like this:
We use this in thousands of places within pyright's test suites. It would be significant work to change this.
gvanrossum commentedon Nov 5, 2021
Hm. In mypy IIRC reveal_type(x) returns x. This makes it possible to wrap arbitrary subexpressions in it without changing their meaning. I would regret losing this.
gvanrossum commentedon Nov 5, 2021
You could introduce a new helper with the desired meaning in a pyright-specific header.
erictraut commentedon Nov 5, 2021
Oh, that's clever. I hadn't considered that.
OK, we can introduce a new helper.
JelleZijlstra commentedon Jan 22, 2022
I am implementing this by adding
typing.reveal_type:JelleZijlstra commentedon Feb 4, 2022
This will be in Python 3.11 and the next release of typing-extensions.
https://docs.python.org/3.11/library/typing.html#typing.reveal_type