Skip to content

Contextual typing using TypeVar union bounds fails #3269

@rchen152

Description

@rchen152

Describe the Bug

Pyrefly is able to use a TypeVar's bound as a contextual hint, like so:

class A: ...
class B: ...
class B2(B): ...

def f[T: list[B]](x: T) -> T:
    return x

f([B2()])  # this succeeds - pyrefly infers the type of `[B2()]` as `list[B]`

(sandbox)

However, this fails when the bound is a union:

class A: ...
class B: ...
class B2(B): ...

def f[T: list[A] | list[B]](x: T) -> T:
    return x

f([B2()])  # Error: `list[B2]` is not assignable to upper bound `list[A] | list[B]` of type variable `T`

(sandbox)

More context: #793

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

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions