Skip to content

Commit

Permalink
Add union simplification test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
JukkaL committed Mar 20, 2017
1 parent d406157 commit d745a50
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
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]]'
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

0 comments on commit d745a50

Please sign in to comment.