Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-46414: Add typing.reveal_type #30646

Merged
merged 6 commits into from Feb 2, 2022
Merged

Conversation

JelleZijlstra
Copy link
Member

@JelleZijlstra JelleZijlstra commented Jan 17, 2022

Doc/library/typing.rst Outdated Show resolved Hide resolved
Lib/test/test_typing.py Outdated Show resolved Hide resolved
@sobolevn
Copy link
Member

@sobolevn sobolevn commented Jan 17, 2022

Thank you! This solves my old pain with redefining reveal_type as:

from typing import TYPE_CHECKING
if not TYPE_CHECKING:
    reveal_type = print

@gvanrossum
Copy link
Member

@gvanrossum gvanrossum commented Jan 17, 2022

Hold on. This feels awkward, since previously this was a builtin. I don’t want to have to import it.

@JelleZijlstra
Copy link
Member Author

@JelleZijlstra JelleZijlstra commented Jan 17, 2022

Hold on. This feels awkward, since previously this was a builtin. I don’t want to have to import it.

Type checkers would still allow using it without the import. You'll have the option to import it if you want to run your code or document to readers what reveal_type() means.

@gvanrossum
Copy link
Member

@gvanrossum gvanrossum commented Jan 17, 2022

It needs more discussion. Please wait until I have a keyboard.

@JelleZijlstra
Copy link
Member Author

@JelleZijlstra JelleZijlstra commented Jan 17, 2022

No hurry! I also opened a thread on typing-sig about this, that's probably the best place to discuss how it should work.

@JelleZijlstra
Copy link
Member Author

@JelleZijlstra JelleZijlstra commented Jan 22, 2022

@gvanrossum based on the discussion on typing-sig, are you satisfied with this change now?

I'm planning to also add reveal_locals() in a separate PR.

@JelleZijlstra
Copy link
Member Author

@JelleZijlstra JelleZijlstra commented Jan 22, 2022

Wait, I didn't address Nikita's review yet.

@gvanrossum
Copy link
Member

@gvanrossum gvanrossum commented Jan 23, 2022

Did we bikeshed yet whether it should write to stdout or stderr?

@JelleZijlstra
Copy link
Member Author

@JelleZijlstra JelleZijlstra commented Jan 23, 2022

Did we bikeshed yet whether it should write to stdout or stderr?

No, though there have been some suggestions about the output format; I just sent a typing-sig email about that too. Personally I don't care much but I'd like to keep it simple.

@gvanrossum
Copy link
Member

@gvanrossum gvanrossum commented Jan 23, 2022

Did we bikeshed yet whether it should write to stdout or stderr?

No, though there have been some suggestions about the output format; I just sent a typing-sig email about that too. Personally I don't care much but I'd like to keep it simple.

I'm just thinking that sending the output to stderr would be more in line with the debug nature of the API.

@JelleZijlstra
Copy link
Member Author

@JelleZijlstra JelleZijlstra commented Jan 23, 2022

Pushed a change to make it write to stderr instead.

Doc/library/typing.rst Outdated Show resolved Hide resolved
Co-authored-by: Guido van Rossum <guido@python.org>
@@ -2667,3 +2668,23 @@ class re(metaclass=_DeprecatedType):

re.__name__ = __name__ + '.re'
sys.modules[re.__name__] = re


def reveal_type(obj: T, /) -> T:
Copy link
Member

@sobolevn sobolevn Jan 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we use type hints in typing.py? 🤔

Copy link
Member Author

@JelleZijlstra JelleZijlstra Jan 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is merged, we will :). I don't see any reason not to, especially in cases like this where the type is simple and helps document the behavior.

Copy link
Member

@AlexWaygood AlexWaygood Jan 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are type annotations in a few lines of typing.py:

def read(self, n: int = -1) -> AnyStr:

It's just about the only module in the stdlib in which I've spotted any type annotations other than importlib :)

@JelleZijlstra
Copy link
Member Author

@JelleZijlstra JelleZijlstra commented Feb 2, 2022

@gvanrossum do you think this is ready to merge now? The discussion on typing-sig has died down. (You added the "Do not merge" label a while ago.)

I think the other new function PRs are also ready, but I can ping you on those later.

Copy link
Member

@gvanrossum gvanrossum left a comment

LGTM.

@gvanrossum gvanrossum merged commit abcc3d7 into python:main Feb 2, 2022
12 checks passed
@bedevere-bot
Copy link

@bedevere-bot bedevere-bot commented Feb 2, 2022

@gvanrossum: Please replace # with GH- in the commit message next time. Thanks!

@gvanrossum
Copy link
Member

@gvanrossum gvanrossum commented Feb 2, 2022

Should probably post the resolution to typing-sig.

@JelleZijlstra JelleZijlstra deleted the revealtype branch Feb 2, 2022
erlend-aasland pushed a commit to erlend-aasland/cpython that referenced this issue Feb 11, 2022
Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants