Skip to content

Contextual typing works only with the first element in a union #793

@rchen152

Description

@rchen152

Describe the Bug

I was surprised to not see an issue for this yet (unless I just missed it?). Opening one for V1 tracking. Contextual typing doesn't play well with union types, as seen here:

testcase!(
bug = "Both assignments should be allowed. When decomposing the contextual hint, we eagerly resolve vars to the 'first' branch of the union. Note: due to the union's sorted representation, the first branch is not necessarily the first in source order.",
test_contextual_typing_against_unions,
r#"
class A: ...
class B: ...
class B2(B): ...
class C: ...
x: list[A] | list[B] = [B2()] # E: `list[B2]` is not assignable to `list[A] | list[B]`
y: list[B] | list[C] = [B2()]
"#,
);

Sandbox Link

No response

(Only applicable for extension issues) IDE Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    contextual-typingissues related to contextual typing

    Type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions