Skip to content

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

Open
@iharabukhouski

Description

@iharabukhouski

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-join-v-unionUsing join vs. using unions

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions