Skip to content

False positive error "incompatible type" on bound TypeVar #13596

@epenet

Description

@epenet

Originally posted on gitter july 29th, where it was suggested I open a bug report

Bug Report
For this code:

from typing import Generic, TypeVar, Union

ListT = TypeVar("ListT", str, float)


class TestListA(Generic[ListT]):
    def __init__(self, device: ListT) -> None:
        self._device: ListT = device


class TestListB(TestListA[ListT]):
    def __init__(self, device: ListT) -> None:
        super().__init__(device)

mypy outputs error:

test.py:13: error: Argument 1 to "__init__" of "TestListA" has incompatible type "str"; expected "ListT"  [arg-type]
test.py:13: error: Argument 1 to "__init__" of "TestListA" has incompatible type "float"; expected "ListT"  [arg-type]

which is false positive.

Please note that when using a TypeVar with bound=Union, then mypy doesn't complain.

ListT = TypeVar("ListT", bound=Union[str, float])

Your Environment

  • Mypy version used: mypy 0.971 (compiled: yes)
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: Python 3.9.12
  • Operating system and version:

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions