Skip to content

[conformance] Add examples that require bidi inference of list literals after specialization inference#2300

Open
dcreager wants to merge 1 commit into
python:mainfrom
dcreager:bidi-list-literals
Open

[conformance] Add examples that require bidi inference of list literals after specialization inference#2300
dcreager wants to merge 1 commit into
python:mainfrom
dcreager:bidi-list-literals

Conversation

@dcreager
Copy link
Copy Markdown

@dcreager dcreager commented Jun 4, 2026

The conformance suite currently contains the following example:

class Class8(Generic[T]):
def __new__(cls, x: list[T], y: list[T]) -> Self:
return super().__new__(cls)
r8 = accepts_callable(Class8)
reveal_type(r8) # `def [T] (x: list[T], y: list[T]) -> Class8[T]`
assert_type(r8([""], [""]), Class8[str])
r8([1], [""]) # E

The important point is that r8 is a generic callable. (There isn't a syntax that lets us easily spell a generic callable type using Callable, so we had to jump through the small hoop of accept_callable to obtain it.) It has the following signature:

[T](x: list[T], y: list[T]) -> Class8[T]

Previously, the last line of this example would require that if you pass in list literals of different types, it should be a type inference error. However, the union of the two lists' respective element types is a valid solution — in this case, T = int | str.

I've updated these examples so that they are expected to succeed.

Open question: I also had considered marking them as E?, since it does require a more complex specialization inference process that we might not want to mandate.

I've also added additional tests for examples where there is genuinely no valid specialization: when one of the arguments is not a list at all, and where the typevar is bounded and the argument is a list that cannot possibly satisfy the upper bound.

@python-cla-bot
Copy link
Copy Markdown

python-cla-bot Bot commented Jun 4, 2026

All commit authors signed the Contributor License Agreement.

CLA signed

@AlexWaygood AlexWaygood added the topic: conformance tests Issues with the conformance test suite label Jun 4, 2026
@JelleZijlstra
Copy link
Copy Markdown
Member

I agree with changing this, but the proposal to require type checkers to infer the union also doesn't feel right. This test isn't about testing precise inference behaviors, it's about testing class constructors. Type inference behavior in cases like this test isn't specified precisely, so we shouldn't force a specific behavior in the conformance suite.

The same behavior is observable in a simpler function:

def f[T](x: list[T], y: list[T]):
    pass

f([1], ["x"])

is currently rejected by all type checkers in multiplay.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic: conformance tests Issues with the conformance test suite

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants