Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add union simplification test cases
  • Loading branch information
JukkaL committed Mar 20, 2017
commit d745a50e7283eaadbc6e91e1e6939efee8769c3d
1 change: 0 additions & 1 deletion mypy/subtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,6 @@ def check_argument(leftarg: Type, rightarg: Type, variance: int) -> bool:

return all(check_argument(ta, ra, tvar.variance) for ta, ra, tvar in
zip(left.args, self.right.args, self.right.type.defn.type_vars))
# TODO: TypeType
return False

def visit_type_var(self, left: TypeVarType) -> bool:
Expand Down
54 changes: 54 additions & 0 deletions test-data/unit/check-unions.test
Original file line number Diff line number Diff line change
Expand Up @@ -307,3 +307,57 @@ reveal_type(u(2.3, 1)) # E: Revealed type is 'builtins.float*'
reveal_type(u(False, 2.2)) # E: Revealed type is 'builtins.float*'
reveal_type(u(2.2, False)) # E: Revealed type is 'builtins.float*'
[builtins fixtures/primitives.pyi]

[case testSimplifyingUnionWithTypeTypes1]
from typing import TypeVar, Union, Type, Any

T = TypeVar('T')
S = TypeVar('S')
def u(x: T, y: S) -> Union[S, T]: pass

t_o = None # type: Type[object]
t_s = None # type: Type[str]
t_a = None # type: Type[Any]

# Two identical items
reveal_type(u(t_o, t_o)) # E: Revealed type is 'Type[builtins.object]'
reveal_type(u(t_s, t_s)) # E: Revealed type is 'Type[builtins.str]'
reveal_type(u(t_a, t_a)) # E: Revealed type is 'Type[Any]'
reveal_type(u(type, type)) # E: Revealed type is 'def (x: builtins.Any) -> builtins.type'

# One type, other non-type
reveal_type(u(t_s, 1)) # E: Revealed type is 'Union[builtins.int*, Type[builtins.str]]'
reveal_type(u(1, t_s)) # E: Revealed type is 'Union[Type[builtins.str], builtins.int*]'
reveal_type(u(type, 1)) # E: Revealed type is 'Union[builtins.int*, def (x: builtins.Any) -> builtins.type]'
reveal_type(u(1, type)) # E: Revealed type is 'Union[def (x: builtins.Any) -> builtins.type, builtins.int*]'
reveal_type(u(t_a, 1)) # E: Revealed type is 'Union[builtins.int*, Type[Any]]'
reveal_type(u(1, t_a)) # E: Revealed type is 'Union[Type[Any], builtins.int*]'
reveal_type(u(t_o, 1)) # E: Revealed type is 'Union[builtins.int*, Type[builtins.object]]'
reveal_type(u(1, t_o)) # E: Revealed type is 'Union[Type[builtins.object], builtins.int*]'

[case testSimplifyingUnionWithTypeTypes2]
from typing import TypeVar, Union, Type, Any

T = TypeVar('T')
S = TypeVar('S')
def u(x: T, y: S) -> Union[S, T]: pass

t_o = None # type: Type[object]
t_s = None # type: Type[str]
t_a = None # type: Type[Any]

# Union with object
reveal_type(u(t_o, object())) # E: Revealed type is 'builtins.object*'
reveal_type(u(object(), t_o)) # E: Revealed type is 'builtins.object*'
reveal_type(u(t_s, object())) # E: Revealed type is 'builtins.object*'
reveal_type(u(object(), t_s)) # E: Revealed type is 'builtins.object*'
reveal_type(u(t_a, object())) # E: Revealed type is 'builtins.object*'
reveal_type(u(object(), t_a)) # E: Revealed type is 'builtins.object*'

# Union between type objects
reveal_type(u(t_o, t_a)) # E: Revealed type is 'Union[Type[Any], Type[builtins.object]]'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Union[Type[Any], Type[object]] and Union[type, Type[object]] should give the same result, since type is equivalent to Type[Any] (at least it is what PEP 484 says). Maybe this could be fixed by adding a special case in spirit of #3012 at the end of visit_type_type.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not feasible since Type[Any] and type have different internal representations. We can't have two different types being proper subtypes of each other unless they are the same type -- otherwise the subtyping relation doesn't form a lattice.

reveal_type(u(t_a, t_o)) # E: Revealed type is 'Union[Type[builtins.object], Type[Any]]'
reveal_type(u(t_s, t_o)) # E: Revealed type is 'Type[builtins.object]'
reveal_type(u(t_o, t_s)) # E: Revealed type is 'Type[builtins.object]'
reveal_type(u(t_o, type)) # E: Revealed type is 'Type[builtins.object]'
reveal_type(u(type, t_o)) # E: Revealed type is 'Type[builtins.object]'
2 changes: 1 addition & 1 deletion typeshed
Submodule typeshed updated 163 files