Skip to content

Should literal promotion recurse into generic types? #1284

@sharkdp

Description

@sharkdp

Literal promotion currently recurses down into types to also promote nested types. When doing type inference for generic container literals, this makes sense for unions:

x = 1 if flag else "a"
xs = [x]  # list[Unknown | int | str]

But it seems wrong to descend into generic types (and possibly other Type variants)? For example:

class Invariant[T]:
    x: T

    def __init__(self, value: T):
        self.x = value

def _(a: Invariant[Literal[1]]):
    xs = [a]  # list[Unknown | Invariant[int]]

Shouldn't we infer list[Unknown | Invariant[Literal[1]]] here?

Metadata

Metadata

Assignees

Labels

bidirectional inferenceInference of types that takes into account the context of a declared type or expected typebugSomething isn't working

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions