python / python/mypy

`reveal_type` should print out nice formatted types

Open
#6,526 0 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

priority-1-normal topic-reveal-type topic-tests topic-usability
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Extracted from #6460

Say you have

lx: Optional[Tuple[str, int]] = None
lx[0]
reveal_type(lx)

mypy outputs

Value of type "Optional[Tuple[str, int]]" is not indexable
Revealed type is 'Union[Tuple[builtins.str, builtins.int], None]'

As you can see (and probably know) it shows two different representations for the inferred type. For user ergonomics the revealed type should be 'Optional[Tuple[int, str]]' as well, no?

The maybe too naive but works for me change is

@@ -1051,7 +1051,7 @@ class MessageBuilder:
         self.fail('Invalid signature "{}" for "{}"'.format(func_type, method_name), context)
 
     def reveal_type(self, typ: Type, context: Context) -> None:
-        self.fail('Revealed type is \'{}\''.format(typ), context)
+        self.fail('Revealed type is {}'.format(self.format(typ)), context)
 
     def reveal_locals(self, type_map: Dict[str, Optional[Type]], context: Context) -> None:
         # To ensure that the output is predictable on Python < 3.6,

But then you get 1.000.000 failures which is expected because the tests rely on the verbose output.

E.g.

Expected:
  main:10: error: Revealed type is 'builtins.int' (diff)
  main:11: error: Revealed type is 'Tuple[builtins.int, builtins.int]' (diff)
  main:13: error: Revealed type is 'Literal[1]' (diff)
  main:14: error: Argument 1 to "force2" has incompatible type "Tuple[int, in...
  main:14: error: Revealed type is 'Tuple[builtins.int, builtins.int]' (diff)
Actual:
  main:10: error: Revealed type is "int"        (diff)
  main:11: error: Revealed type is "Tuple[int, int]" (diff)
  main:13: error: Revealed type is "Literal[1]" (diff)
  main:14: error: Argument 1 to "force2" has incompatible type "Tuple[int, in...
  main:14: error: Revealed type is "Tuple[int, int]" (diff)

This makes it a non-trivial change.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at MessageBuilder.reveal_type, where the issue shows the current output construction, and compare it with the existing formatted type output. Update the affected reveal_type expectations across the test suite so the revealed types use the user-facing formatting, then run the mypy tests to confirm the revised diagnostics.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.