dropbox / pyannotate

Auto-generate PEP-484 annotations
Apache License 2.0
1.42k stars 59 forks source link

Remove redundant Dict/List/Set unions. #40

Closed rowillia closed 6 years ago

rowillia commented 6 years ago

We currently generate some redundant types such as: Union[Dict[str, str], Dict[str, Any]].

gvanrossum commented 6 years ago

Hm. There's a line of argument that Union[str, Any] is not equivalent to Any, because when e.g. we call x.append(y) we know that this isn't type-safe, since one branch of the union definitely doesn't have that method. @JukkaL do you recall the issue where this was decided? In any case it's easily determined that mypy currently thinks this is so:

from typing import Union, Any
A = Union[str, Any]
def f(a: A):
  reveal_type(a)

prints

__tmp__.py:4: error: Revealed type is 'Union[builtins.str, Any]'
rowillia commented 6 years ago

@gvanrossum That's totally fair. I updated this PR to be a bit more specific. Where this is biting is is when we have functions that accept JSON-like data where you have a Dict that has a huge Union of value types. Ideally pyannotate would generate TypedDict annotations, but until then we should fall back to Any. What do you think?

gvanrossum commented 6 years ago

I'm so sorry, reading more of the code I realize that your original version was fine. The type simplification engine was written by Jukka, and I misremembered how it works -- it frequently replaces verbose unions with Any, so I think that what you did originally is fine. Can you roll back the last two commits?

rowillia commented 6 years ago

Done, thanks for clearing that up @gvanrossum !