Skip to content

Commit d745a50

Browse files
committed
Add union simplification test cases
1 parent d406157 commit d745a50

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-2
lines changed

mypy/subtypes.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,6 @@ def check_argument(leftarg: Type, rightarg: Type, variance: int) -> bool:
583583

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

589588
def visit_type_var(self, left: TypeVarType) -> bool:

test-data/unit/check-unions.test

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,3 +307,57 @@ reveal_type(u(2.3, 1)) # E: Revealed type is 'builtins.float*'
307307
reveal_type(u(False, 2.2)) # E: Revealed type is 'builtins.float*'
308308
reveal_type(u(2.2, False)) # E: Revealed type is 'builtins.float*'
309309
[builtins fixtures/primitives.pyi]
310+
311+
[case testSimplifyingUnionWithTypeTypes1]
312+
from typing import TypeVar, Union, Type, Any
313+
314+
T = TypeVar('T')
315+
S = TypeVar('S')
316+
def u(x: T, y: S) -> Union[S, T]: pass
317+
318+
t_o = None # type: Type[object]
319+
t_s = None # type: Type[str]
320+
t_a = None # type: Type[Any]
321+
322+
# Two identical items
323+
reveal_type(u(t_o, t_o)) # E: Revealed type is 'Type[builtins.object]'
324+
reveal_type(u(t_s, t_s)) # E: Revealed type is 'Type[builtins.str]'
325+
reveal_type(u(t_a, t_a)) # E: Revealed type is 'Type[Any]'
326+
reveal_type(u(type, type)) # E: Revealed type is 'def (x: builtins.Any) -> builtins.type'
327+
328+
# One type, other non-type
329+
reveal_type(u(t_s, 1)) # E: Revealed type is 'Union[builtins.int*, Type[builtins.str]]'
330+
reveal_type(u(1, t_s)) # E: Revealed type is 'Union[Type[builtins.str], builtins.int*]'
331+
reveal_type(u(type, 1)) # E: Revealed type is 'Union[builtins.int*, def (x: builtins.Any) -> builtins.type]'
332+
reveal_type(u(1, type)) # E: Revealed type is 'Union[def (x: builtins.Any) -> builtins.type, builtins.int*]'
333+
reveal_type(u(t_a, 1)) # E: Revealed type is 'Union[builtins.int*, Type[Any]]'
334+
reveal_type(u(1, t_a)) # E: Revealed type is 'Union[Type[Any], builtins.int*]'
335+
reveal_type(u(t_o, 1)) # E: Revealed type is 'Union[builtins.int*, Type[builtins.object]]'
336+
reveal_type(u(1, t_o)) # E: Revealed type is 'Union[Type[builtins.object], builtins.int*]'
337+
338+
[case testSimplifyingUnionWithTypeTypes2]
339+
from typing import TypeVar, Union, Type, Any
340+
341+
T = TypeVar('T')
342+
S = TypeVar('S')
343+
def u(x: T, y: S) -> Union[S, T]: pass
344+
345+
t_o = None # type: Type[object]
346+
t_s = None # type: Type[str]
347+
t_a = None # type: Type[Any]
348+
349+
# Union with object
350+
reveal_type(u(t_o, object())) # E: Revealed type is 'builtins.object*'
351+
reveal_type(u(object(), t_o)) # E: Revealed type is 'builtins.object*'
352+
reveal_type(u(t_s, object())) # E: Revealed type is 'builtins.object*'
353+
reveal_type(u(object(), t_s)) # E: Revealed type is 'builtins.object*'
354+
reveal_type(u(t_a, object())) # E: Revealed type is 'builtins.object*'
355+
reveal_type(u(object(), t_a)) # E: Revealed type is 'builtins.object*'
356+
357+
# Union between type objects
358+
reveal_type(u(t_o, t_a)) # E: Revealed type is 'Union[Type[Any], Type[builtins.object]]'
359+
reveal_type(u(t_a, t_o)) # E: Revealed type is 'Union[Type[builtins.object], Type[Any]]'
360+
reveal_type(u(t_s, t_o)) # E: Revealed type is 'Type[builtins.object]'
361+
reveal_type(u(t_o, t_s)) # E: Revealed type is 'Type[builtins.object]'
362+
reveal_type(u(t_o, type)) # E: Revealed type is 'Type[builtins.object]'
363+
reveal_type(u(type, t_o)) # E: Revealed type is 'Type[builtins.object]'

typeshed

Submodule typeshed updated 163 files

0 commit comments

Comments
 (0)