Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Distribute return type over union#2047

Unanswered
randolf-scholz asked this question inQ&A
Discussion options

Inspired bymicrosoft/pyright#10673, I looked at this simple example: Code sample inpyright playground

fromcollections.abcimportSequencedeflistify[T](x:Sequence[T])->list[T]:returnlist(x)deffunc(x:Sequence[str]|Sequence[int]):y=listify(x)reveal_type(y)# list[str | int]print(y)

It feels like the revealed type is not actually what we want here. The desirable inference is imolist[str] | list[int].

Is there simple way to annotatelistify to make the return type distribute over unions, or is this a general type-checker limitation?

What seems to happen here is that we assignSequence[str] | Sequence[int] <: Sequence[str | int] = Sequence[T], so we pickT = int | str. However, in principle wouldn't it be more precise if the type checker, when seeing aUnionType as an argument, passes every member separately and yields theUnionType of the individual return types?

I guess with multiple arguments this runs into combinatoric explosion...

You must be logged in to vote

Replies: 0 comments

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
1 participant
@randolf-scholz

[8]ページ先頭

©2009-2025 Movatter.jp