Skip to content

False positive "return-value" from class method when using class generics #20301

Closed as duplicate

Description

@Apakottur

Bug Report
Consider the following code:

# test.py
class MyClass[T: int | str]:
    def __init__(self, factory: type[T]) -> None:
        self.factory = factory

    def func(self) -> T:
        return self.factory()

Running mypy test.py gives:

test.py:7:16: error: Incompatible return value type (got "int | str", expected "T")  [return-value]
Found 1 error in 1 file (checked 1 source file)

The type of self.factory is type[T] so self.factory() should be just T.

Am I missing something or is this a bug?

Your Environment

  • Mypy version used: 1.18.2
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.13

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions