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

Fix #18423 - reveal_type not run in "unreachable" branches #18424

Closed
wants to merge 1 commit into from

Conversation

SANTHOSH-SACHIN
Copy link

Fix #18423 - reveal_type not run in "unreachable" branches

Currently, reveal_type does not output type information in unreachable code branches, such as those following assert_never. This is because mypy considers such code unreachable and skips type-checking it. However, for debugging purposes, it is useful to see the type information even in unreachable branches.

Solution

This PR modifies the reveal_type function to explicitly handle unreachable code by checking for UninhabitedType, which mypy uses to represent unreachable branches. When UninhabitedType is encountered, reveal_type outputs "Never".

Changes

  1. reveal_type Function:
    • Added a check for UninhabitedType to handle unreachable code.
    • If the type is UninhabitedType, the function outputs "Never".
    • Otherwise, it behaves as before, using TypeStrVisitor to print the type.

Why This Fix Works

  • UninhabitedType Represents Unreachable Code:
    • mypy uses UninhabitedType to represent unreachable code, such as after assert_never. By checking for UninhabitedType, we can detect and handle this case.
  • Maintains Compatibility:
    • This fix does not introduce any new types or changes to the mypy type system. It leverages the existing UninhabitedType to represent the "never" case.

Copy link
Contributor

github-actions bot commented Jan 6, 2025

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

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

Successfully merging this pull request may close these issues.

reveal_type not run in "unreachable" branches
2 participants