Skip to content

str.join result incorrectly inferred as Unknown for list[Any] #4845

Closed as duplicate

Description

@dillydill123

Describe the Bug

Pyrefly infers the result of str.join as Unknown when the argument is Iterable[Any].

from typing import Any, reveal_type
def b(x: list[Any]) -> None:
    reveal_type("".join(x))

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.

Sandbox Link

https://pyrefly.org/sandbox/?project=v2.lZRBDsIgEEWvQuyiddHO3lN4AF1gJRU1QIAq3t4PrYnUhNjtMDP5-W_4ayMzC5vI-dSE-Zfh_bgE_tXeQPamu2pQDFsA-vWvYvupwGYpSA4YP4zIJxjaHVSFaBLs4r1xO6LPuLYDCUVn3TvKJojFU-EPLlPq5euYTrwd1hbAGSuc8KkE_n-hrCeSdQlkHTmiQwRzl730LVevxRWM6qb0E6q4lVF9G03Meopg3w

(Only applicable for extension issues) IDE Information

No response

Activity

  1. added
    needs-discussionAn issue where it's not clear whether there is a bug or we are behaving as expected.
    on Sep 10, 2026
  2. rchen152 commented on Sep 10, 2026

    @rchen152
    Contributor

    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:

    class str:
        @overload
        def join(self: LiteralString, iterable: Iterable[LiteralString], /) -> LiteralString: ...
        @overload
        def join(self, iterable: Iterable[str], /) -> str: ...
    

    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
    
  3. rchen152 commented on Sep 10, 2026

    @rchen152
    Contributor

    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.

  4. dillydill123 commented on Sep 10, 2026

    @dillydill123
    ContributorAuthor

    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 😅

  5. stroxler commented on Sep 15, 2026

    @stroxler
    Contributor

    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.

  6. rchen152 commented on Sep 15, 2026

    @rchen152
    Contributor

    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.

  7. rchen152 commented on Sep 15, 2026

    @rchen152
    Contributor

    I'm going to merge this into #4910, since both reports are caused by the same overload change.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs-discussionAn issue where it's not clear whether there is a bug or we are behaving as expected.overloads

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions