Skip to content

Feature: reveal_type should work from comments #8864

Closed

Description

@ramalho

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)

Activity

  1. JukkaL commented on May 22, 2020

    @JukkaL
    Collaborator

    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.

  2. ramalho commented on May 22, 2020

    @ramalho
    Author

    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?

  3. msullivan commented on May 22, 2020

    @msullivan
    Collaborator
  4. ramalho commented on May 22, 2020

    @ramalho
    Author

    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.

  5. AlexWaygood commented on Apr 1, 2022

    @AlexWaygood
    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.)

  6. ramalho commented on Apr 2, 2022

    @ramalho
    Author

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

  7. JelleZijlstra commented on Apr 2, 2022

    @JelleZijlstra
    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:.

  8. ramalho commented on Apr 3, 2022

    @ramalho
    Author

    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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions