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
str.join result incorrectly inferred as Unknown for list[Any] #4845
This seems to have appeared in 1.3.0.dev4. The previous 1.3.0.dev3 reports str as the type. Interestingly, ty (sandbox) has the same issue but mypy (sandbox) does not.
This is a bit tricky. The immediate cause of the regression is a change in overload evaluation (0d8e9f6 and #4782). The str.join overloads look like this:
Because list[Any] matches either Iterable[LiteralString] or Iterable[str], we say that the result of overload evaluation is ambiguous and fall back to Any. Previously, we selected str on the basis that LiteralString is assignable to str, but I changed this behavior because it violates the typing spec.
I do think selecting str here makes some intuitive sense, but the reason the spec doesn't allow it is that it would violate something called the "gradual guarantee":
def f(x: LiteralString): ...
def g1(x: list[LiteralString]):
f("".join(x)) # this succeeds
def g2(x: list[Any]):
f("".join(x)) # making the input type more gradual should not cause an error to appear here
I dug into mypy's behavior a bit more, and it looks like the reason for it is that mypy doesn't yet support LiteralString: python/mypy#12554. It treats it as an alias to str.
Interesting. It’s another situation where the current behavior is technically (spec) correct but does not provide the best user experience or bug finding ability. Given the stronger guarantee that LiteralString provides, I think the more semantically defensible result is str for this ambiguous overload situation. Maybe for all such cases where (str, LiteralString) ambiguity is met, take str. But generalizing this doesn’t seem simple 😅
The right solution in terms of set theory actually seems like either an intersection (which I think might actually work?) or an AnyOf operator that is gradual over some specific set of types but not all types.
I think the spec is choosing Any because it has no choice, given limited type combinators available today, but to either accept false positives in some cases or just spit out Any and produce arbitrarily many false negatives (which is what @dillydill123 ran into).
I believe @grievejia highlighted the fact that overloads + gradual typing naturally call for some sort of bounded gradual type to get optimal behavior, and it might have been this exact case.
But I think an intersection might actually work just about as well, given that this is only relevant in a return type and the idea is that the ambiguously-typed value (gradually) can sort of behave as any one of the possible return types.
This specific bug could potentially be closed if we're willing to accept intersection as the theoretically right answer even if we don't want to expose it, because in this case LiteralStr & str reduces to just LiteralStr. It would be sort of weird to spell the ambiguously-typed return as LiteralStr with no indication of how that came about, but I think in terms of typing set theory it has the right properties.
I believe the consensus - to what extent there is one right now - is that AnyOf is the theoretically best solution. An intersection isn't always right; A & B says that the answer is both an A and a B, but you can have ambiguity in whether A or B is returned even when they don't overlap and being both simultaneously is impossible.
On a side note, the spec no longer mandates returning Any. It was changed recently to allow for experimentation with other return types (link):
In this case [of gradual ambiguity], infer a return type that is assignable to each of the remaining return types and permits operations supported by any of them without errors, then stop. A type checker may infer Any or a more precise gradual type that satisfies these requirements.
Activity
rchen152 commentedon Sep 10, 2026
This is a bit tricky. The immediate cause of the regression is a change in overload evaluation (0d8e9f6 and #4782). The
str.joinoverloads look like this:Because
list[Any]matches eitherIterable[LiteralString]orIterable[str], we say that the result of overload evaluation is ambiguous and fall back toAny. Previously, we selectedstron the basis thatLiteralStringis assignable tostr, but I changed this behavior because it violates the typing spec.I do think selecting
strhere makes some intuitive sense, but the reason the spec doesn't allow it is that it would violate something called the "gradual guarantee":rchen152 commentedon Sep 10, 2026
I dug into mypy's behavior a bit more, and it looks like the reason for it is that mypy doesn't yet support
LiteralString: python/mypy#12554. It treats it as an alias tostr.dillydill123 commentedon Sep 10, 2026
Interesting. It’s another situation where the current behavior is technically (spec) correct but does not provide the best user experience or bug finding ability. Given the stronger guarantee that LiteralString provides, I think the more semantically defensible result is str for this ambiguous overload situation. Maybe for all such cases where (str, LiteralString) ambiguity is met, take str. But generalizing this doesn’t seem simple 😅
stroxler commentedon Sep 15, 2026
The right solution in terms of set theory actually seems like either an intersection (which I think might actually work?) or an
AnyOfoperator that is gradual over some specific set of types but not all types.I think the spec is choosing
Anybecause it has no choice, given limited type combinators available today, but to either accept false positives in some cases or just spit outAnyand produce arbitrarily many false negatives (which is what @dillydill123 ran into).I believe @grievejia highlighted the fact that overloads + gradual typing naturally call for some sort of bounded gradual type to get optimal behavior, and it might have been this exact case.
But I think an intersection might actually work just about as well, given that this is only relevant in a return type and the idea is that the ambiguously-typed value (gradually) can sort of behave as any one of the possible return types.
This specific bug could potentially be closed if we're willing to accept intersection as the theoretically right answer even if we don't want to expose it, because in this case
LiteralStr & strreduces to justLiteralStr. It would be sort of weird to spell the ambiguously-typed return asLiteralStrwith no indication of how that came about, but I think in terms of typing set theory it has the right properties.rchen152 commentedon Sep 15, 2026
I believe the consensus - to what extent there is one right now - is that
AnyOfis the theoretically best solution. An intersection isn't always right;A & Bsays that the answer is both anAand aB, but you can have ambiguity in whetherAorBis returned even when they don't overlap and being both simultaneously is impossible.On a side note, the spec no longer mandates returning
Any. It was changed recently to allow for experimentation with other return types (link):rchen152 commentedon Sep 15, 2026
I'm going to merge this into #4910, since both reports are caused by the same overload change.
Unknown#4910