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

reveal_type should print out nice formatted types #6526

Open
kaste opened this issue Mar 8, 2019 · 0 comments
Open

reveal_type should print out nice formatted types #6526

kaste opened this issue Mar 8, 2019 · 0 comments

Comments

@kaste
Copy link

kaste commented Mar 8, 2019

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.

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

3 participants