Skip to content

enforce type of ClassVar in subclasses #167

@beauxq

Description

@beauxq

Summary

This is for red knot, tested in https://playknot.ruff.rs/

from typing import ClassVar, assert_type, reveal_type

class B:
    x: ClassVar[int | str] = 0

class C(B):
    x = "random"

class D(C):
    x = 1

class E(D):
    x = b"x"


def foo(b: type[B]) -> None:
    x = b.x
    reveal_type(x)
    if isinstance(x, str):
        print(x + "y")
    else:
        reveal_type(x)
        print(x + 3)


foo(E)

This program crashes with a TypeError because an invalid type is assigned to x in E.
But that invalid assignment is not reported.

Version

https://playknot.ruff.rs/

Metadata

Metadata

Assignees

Labels

typing semanticstyping-module features, spec compliance, etc

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions