You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
false positive "Tuple index out of range" when indexing tuple[Any, *tuple[Any, ...]] #19858
As you can see from the note this is intentional. For historical reasons plain tuple[X, ...] is handled in unsafe manner. But types that can be only expressed with unpack syntax are handled strictly.
Hm no that's not what I meant. You see, the issue here is that tuple[Any, ...] is the "gradual tuple", which is not only "any-isch" in value, but also in size. That is, tuple[Any, ...] is assignable to every other tuple type and vice-versa. That's why mypy correctly (as can be seen in the example) accepts indexing it for any index.
A restricted form of the gradual tuple is tuple[int, *tuple[Any, ...]], which has the additional requirement that 1) the type at index 0 is int and 2) that the size must be at least 1 (not 0). Just like before, it should be allowed to arbitrarily index it, since it's gradual in size. So it's length is a a hypothetical gradual positive int of at least 1, i.e. Literal[1] | (PosInt & Any).
But mypy does not behave that way here. Instead, it reports that indexing it beyond 1 can be type-unsafe. Technically this is true. But by gradual construction, we have indicated that it should be treated as if it has any size.
The issue you linked is certainly related, but I don't think its the same thing here. This is about the special-cased tuple type itself, not about user-defined variadically-gradual types.
Activity
ilevkivskyi commentedon Sep 16, 2025
As you can see from the note this is intentional. For historical reasons plain
tuple[X, ...]is handled in unsafe manner. But types that can be only expressed with unpack syntax are handled strictly.ilevkivskyi commentedon Sep 16, 2025
Oh, I guess you want
Anyto be special-cased, if yes, then we already have an issue for this IIRC.ilevkivskyi commentedon Sep 16, 2025
#18665
jorenham commentedon Sep 16, 2025
Hm no that's not what I meant. You see, the issue here is that
tuple[Any, ...]is the "gradual tuple", which is not only "any-isch" in value, but also in size. That is,tuple[Any, ...]is assignable to every other tuple type and vice-versa. That's why mypy correctly (as can be seen in the example) accepts indexing it for any index.A restricted form of the gradual tuple is
tuple[int, *tuple[Any, ...]], which has the additional requirement that 1) the type at index 0 isintand 2) that the size must be at least 1 (not 0). Just like before, it should be allowed to arbitrarily index it, since it's gradual in size. So it's length is a a hypothetical gradual positiveintof at least1, i.e.Literal[1] | (PosInt & Any).But mypy does not behave that way here. Instead, it reports that indexing it beyond
1can be type-unsafe. Technically this is true. But by gradual construction, we have indicated that it should be treated as if it has any size.The issue you linked is certainly related, but I don't think its the same thing here. This is about the special-cased tuple type itself, not about user-defined variadically-gradual types.
Lenient handling of *tuple[Any, ...] (part 2) (#22006)