Skip to content

Detect unsupported use of self types in generic classes #8020

Closed
@JukkaL

Description

@JukkaL

The last line generates a strange error message:

from typing import TypeVar, Generic, Type

M = TypeVar("M")
T = TypeVar("T")

class Descriptor(Generic[M]):
    pass

class BaseWrapper(Generic[M]):
    @classmethod
    def create_wrapper(cls, metric_descriptor):
        # type: (Type[T], Descriptor[M]) -> T
        raise NotImplementedError
    
class WrapperInstance(BaseWrapper[M]):
    @classmethod
    def create_wrapper(cls, descriptor):
        # type: (Descriptor[M]) -> WrapperInstance[M]
        return WrapperInstance()
    
def build_wrapper(descriptor):
    # type: (Descriptor[M]) -> BaseWrapper[M]
    wrapper = WrapperInstance  # type: Type[BaseWrapper[M]]
    # Argument 1 to "create_wrapper" of "BaseWrapper" has incompatible type "Descriptor[M]"; 
    # expected "Descriptor[BaseWrapper[M]]"
    return wrapper.create_wrapper(descriptor)

The root cause seems to be the signature of BaseWrapper.create_wrapper; it shouldn't use two type variables M and T. It would be better to generate an error about the signature instead of the call site. However, there may be something deeper going on.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions