Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent Handling of Self in ClassVar Annotations #18776

Open
avranu opened this issue Mar 9, 2025 · 0 comments
Open

Inconsistent Handling of Self in ClassVar Annotations #18776

avranu opened this issue Mar 9, 2025 · 0 comments
Labels
bug mypy got something wrong topic-self-types Types for self

Comments

@avranu
Copy link

avranu commented Mar 9, 2025

Bug Report
When using Self within a ClassVar, mypy raises an arg-type error. This behavior seems inconsistent with the intended use of Self and ClassVar.​

To Reproduce

MyPy playground: https://mypy-play.net/?mypy=latest&python=3.12&gist=dfe06f06e69d157317a667311aa75952

from typing import TypeVar, Generic, ClassVar, Self

T = TypeVar("T", bound="BaseModel")

class BaseModel:
    _meta: "ClassVar[Meta[Self]]"

    class Meta(Generic[T]):
        """Metadata class for BaseModel."""
        def __init__(self, model: type[T]) -> None:
            self.model = model

    @classmethod
    def __init_subclass__(cls) -> None:
        # The following line triggers mypy error "arg-type"
        # bug.py: note: In member "__init_subclass__" of class "BaseModel":
	# bug.py:21:30: error: Argument 1 to "Meta" has incompatible type "type[BaseModel]"; expected "type[Self]"  [arg-type]
        cls._meta = cls.Meta(cls)

# outputs  -->  Revealed type is "bug.BaseModel.Meta[bug.BaseModel]"
reveal_type(BaseModel._meta)

Expected Behavior

The code should type-check without errors, as Self within the ClassVar should correspond to the subclass type, making cls.Meta(cls) a valid instantiation.​

Actual Behavior

mypy raises an arg-type error

bug.py: note: In member "__init_subclass__" of class "BaseModel":
bug.py:21:30: error: Argument 1 to "Meta" has incompatible type "type[BaseModel]"; expected "type[Self]"  [arg-type]

Your Environment:

mypy version used: 1.15.0
Python version used: 3.12.8

Additional Context:

Removing ClassVar, or changing Self to BaseModel both eliminate the mypy error.

This issue may be related to or a duplicate of Issue #17395, where Self types in ClassVar annotations lose specificity through TypeVar. In that case, Self appears to decay to the TypeVar bound instead of retaining the specific subclass type.​ I'm not certain if these are the same issue.

@avranu avranu added the bug mypy got something wrong label Mar 9, 2025
@sterliakov sterliakov added the topic-self-types Types for self label Mar 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-self-types Types for self
Projects
None yet
Development

No branches or pull requests

2 participants