Skip to content

ClassVar cannot contain Self type in generic classes #20331

@randolf-scholz

Description

@randolf-scholz

This rule seems correct when Self appears in a covariant position, like

class MyGeneric[T]:
    # ill-defined, what's the type of MySequence.example?
    example: ClassVar[Self]  # error: ClassVar cannot contain Self type in generic classes

However, if Self appears in a contravariant position, I do not see why this should be an error.

class MyGeneric[T]:
    pre_method_hooks: ClassVar[list[ Callable[[Self], None] ]] = []

Full example: https://mypy-play.net/?mypy=master&python=3.12&gist=951d5b3a168378cd33d9fc4aebd4572e

from typing import Callable, ClassVar, Self, Any
from abc import abstractmethod

class Transform[X, Y]:
    pre_transform_hooks: ClassVar[list[Callable[[Self], None]]] = []  # ❌️

    def __init_subclass__(cls) -> None:
        super().__init_subclass__()
        original_transform = cls.transform

        def wrapped_transform(self: Self, x: X, /, *args: Any, **kwargs: Any) -> Y:
            for hook in cls.pre_transform_hooks:
                hook(self)
            return original_transform(self, x, *args, **kwargs)
        cls.transform = wrapped_transform  # type: ignore[method-assign]

    @abstractmethod
    def transform(self, x: X, /) -> Y: ...

class Demo(Transform[int, int]):
    def transform(self, x: int, /) -> int: return 0

    def hello_world(self) -> None:
        print(f"Hello, world! from {self}")

    pre_transform_hooks: ClassVar[list[Callable]] = [hello_world]

Demo().transform(0)  # prints "Hello, world! from <__main__.Demo object at ...>"
main.py:5: error: ClassVar cannot contain Self type in generic classes  [misc]
Found 1 error in 1 file (checked 1 source file)

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