Skip to content

Feature: reveal_type should work from comments #8864

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

Closed
ramalho opened this issue May 20, 2020 · 8 comments
Closed

Feature: reveal_type should work from comments #8864

ramalho opened this issue May 20, 2020 · 8 comments

Comments

@ramalho
Copy link

ramalho commented May 20, 2020

It would be great if reveal_type could be called from comments.

Mypy already looks at various comments to do its job, and reveal_type is not a runtime function, it only exists during a type check.

While experimenting with type hints in test code, I could write this:

a = some_func() # reveal_type(a)

Instead of:

a = some_func()
if typing.TYPE_CHECKING:
    reveal_type(a)
@JukkaL
Copy link
Collaborator

JukkaL commented May 22, 2020

I'm not sure if this would be a net improvement. Now it's possible to comment out uses of reveal_type to temporarily silence them. What do others think?

Also, this may be tricky to support since the parser we use drops most comments, I think.

@ramalho
Copy link
Author

ramalho commented May 22, 2020

Thanks for you response and your work on Mypy, @JukkaL.

The syntax for the comment can be different, to accommodate the current use of commenting out uses of reveal_type, and make it easier for the parser. Maybe this:

a = some_func() # type: reveal_type(a)

Before focusing on the syntax of the comment, what do people think of the idea of having a way to put calls to reveal_type in test suites in a way that does not interfere with running the tests, and also doesn't require the cumbersome if TYPE_CHECKING guards?

@msullivan
Copy link
Collaborator

msullivan commented May 22, 2020 via email

@ramalho
Copy link
Author

ramalho commented May 22, 2020

reveal_type() is a great debugging and learning feature.

My use case is as a teacher and a writer: I have code that is tested with pytest, and is sprinkled with reveal_type to demonstrate Mypy's type inference. Perhaps having if TYPE_CHECKING guards is not too bad in normal Mypy usage, but in my examples it does add noise.

I am biased, but I do believe that features that help with learning should be carefully considered, even if they don't seem useful to those with a lot of experience with the tool.

@AlexWaygood
Copy link
Member

@ramalho, does the recent addition of typing.reveal_type to the stdlib work for your use case? (It will be new in 3.11, but has also been added to the typing_extensions backports package.)

@ramalho
Copy link
Author

ramalho commented Apr 2, 2022

No, it does not solve my use case. Please consider my initial feature request and my responses to the other comments. Thanks!

@JelleZijlstra
Copy link
Member

The use case seems too marginal to justify the complexity, especially now that we have the runtime reveal_type() available so you shouldn't need if TYPE_CHECKING:.

@ramalho
Copy link
Author

ramalho commented Apr 3, 2022

I agree @JelleZijlstra. I did not know that reveal_type() would no longer require the if. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants