Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix type of conditional statement to Union and not Join. (#3487) #5041

Merged
merged 2 commits into from
May 14, 2018
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
4 changes: 2 additions & 2 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1328,9 +1328,9 @@ def check_protocol_variance(self, defn: ClassDef) -> None:
tvars = info.defn.type_vars
for i, tvar in enumerate(tvars):
up_args = [object_type if i == j else AnyType(TypeOfAny.special_form)
for j, _ in enumerate(tvars)]
for j, _ in enumerate(tvars)] # type: List[Type]
down_args = [UninhabitedType() if i == j else AnyType(TypeOfAny.special_form)
for j, _ in enumerate(tvars)]
for j, _ in enumerate(tvars)] # type: List[Type]
up, down = Instance(info, up_args), Instance(info, down_args)
# TODO: add advanced variance checks for recursive protocols
if is_subtype(down, up, ignore_declared_variance=True):
Expand Down
2 changes: 1 addition & 1 deletion mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -2336,7 +2336,7 @@ def visit_conditional_expr(self, e: ConditionalExpr) -> Type:
# branch's type.
else_type = self.analyze_cond_branch(else_map, e.else_expr, context=if_type)

res = join.join_types(if_type, else_type)
res = UnionType.make_simplified_union([if_type, else_type])

return res

Expand Down
4 changes: 2 additions & 2 deletions test-data/unit/check-functions.test
Original file line number Diff line number Diff line change
Expand Up @@ -2064,8 +2064,8 @@ def f() -> None:
def g(x: int) -> None:
pass
h = f if bool() else g
reveal_type(h) # E: Revealed type is 'builtins.function'
h(7) # E: Cannot call function of unknown type
reveal_type(h) # E: Revealed type is 'Union[def (), def (x: builtins.int)]'
h(7) # E: Too many arguments for "f"
[builtins fixtures/bool.pyi]

-- Positional-only arguments
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-optional.test
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ def lookup_field(name, obj):
attr = None

[case testTernaryWithNone]
reveal_type(None if bool() else 0) # E: Revealed type is 'Union[builtins.int, builtins.None]'
reveal_type(None if bool() else 0) # E: Revealed type is 'Union[builtins.None, builtins.int]'
Copy link
Member

Choose a reason for hiding this comment

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

Could you please add few dedicated tests? (Probably not in this test but in check-unions)

[builtins fixtures/bool.pyi]

[case testListWithNone]
Expand Down