Skip to content

mypy loses information about types. 'str | tuple[str, ...]' becomes 'Sequence[str]' #19211

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
iharabukhouski opened this issue Jun 2, 2025 · 3 comments
Labels
bug mypy got something wrong topic-join-v-union Using join vs. using unions

Comments

@iharabukhouski
Copy link

Bug Report

mypy loses information about types. 'str | tuple[str, ...]' becomes 'Sequence[str]'

To Reproduce

version 1: this fails

from typing import TypeVar

T = TypeVar('T')

def unwrap(x: T | None) -> T:

  assert x is not None

  return x

class Qux:

    def foo(self) -> str | tuple[str, ...]:

        baz = unwrap(self.bar())

        return baz

    def bar(self) -> str | tuple[str, ...] | None:

        return 'quux'

version 2: this works

class Qux:

    def foo(self) -> str | tuple[str, ...]:

        baz = self.bar()

        assert baz is not None

        return baz

    def bar(self) -> str | tuple[str, ...] | None:

        return 'quux'

Expected Behavior

both versions of code should run successfully

Actual Behavior

version 2 fails with the following error: 'Incompatible return value type (got "Sequence[str]", expected "str | tuple[str, ...]") [return-value]'

Your Environment

mypy version: 1.15.0
python version: 3.12

reproducible even the mypy playground

@iharabukhouski iharabukhouski added the bug mypy got something wrong label Jun 2, 2025
@sterliakov
Copy link
Collaborator

Working playground link: https://mypy-play.net/?mypy=master&python=3.12&gist=525c6298e4a1367cfba7fc00d042feea

This is another example of #12025, just with different common ancestor: str and tuple[str] are both Sequence[str], usually this manifests as object type in unexpected places, here it's a bit more friendly.

@sterliakov sterliakov added the topic-join-v-union Using join vs. using unions label Jun 3, 2025
@iharabukhouski
Copy link
Author

@sterliakov #12025 seems to be merged. does it mean that the issue is already fixed?

@sterliakov
Copy link
Collaborator

Damn, sorry, I typoed or copied wrong issue number... That should be #12056, a long-standing issue that isn't resolved yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-join-v-union Using join vs. using unions
Projects
None yet
Development

No branches or pull requests

2 participants