Skip to content

Problems using class type variable in class methods #3645

Closed
@LiraNuna

Description

@LiraNuna

Follow up from a discussion on gitter:

from typing import *


T = TypeVar('T')
class Factory(Generic[T]):
    @classmethod
    def instance(cls) -> 'Factory[T]':
        return cls()

    def produce(self) -> T:
        raise NotImplementedError()

    @classmethod
    def get(cls) -> T:
        return cls.instance().produce()


class HelloWorldFactory(Factory[str]):
    def produce(self) -> str:
        return 'Hello World'


# factory.py:25: error: Incompatible return value type (got "T", expected "str")
def error_with_mypy() -> str:
    return HelloWorldFactory.get()

@ilevkivskyi mentioned this should not error and that this code is valid. I don't have the necessary context to understand how to classify this issue.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions