Skip to content

ClassVar shouldn't be allowed to contain type variables #518

@karlicoss

Description

@karlicoss

Summary

See
https://typing.python.org/en/latest/spec/class-compat.html#classvar

Note that a ClassVar parameter cannot include any type variables, regardless of the level of nesting: ClassVar[T] and ClassVar[list[set[T]]] are both invalid if T is a type variable.

I find their example unnecessarily complicated, here's a simpler reproducer

from typing import ClassVar, reveal_type, cast

class Box[T]:
    value: ClassVar[T] = cast(T, None) 

int_box = Box[int]()
reveal_type(int_box.value)

str_box = Box[str]()
reveal_type(str_box.value)

Currently this passes, inferring conflicting types:

info[revealed-type]: Revealed type
 --> test_classvar.py:7:13
  |
6 | int_box = Box[int]()
7 | reveal_type(int_box.value)
  |             ^^^^^^^^^^^^^ `int`
8 |
9 | str_box = Box[str]()
  |

info[revealed-type]: Revealed type
  --> test_classvar.py:10:13
   |
 9 | str_box = Box[str]()
10 | reveal_type(str_box.value)
   |             ^^^^^^^^^^^^^ `str`
   |

Found 2 diagnostics

Expected result

Should be rejected, e.g. mypy rejects this with error: ClassVar cannot contain type variables [misc]

Relevant issues

Version

ty 0.0.1-alpha.7

Metadata

Metadata

Assignees

Labels

bugSomething isn't workinggenericsBugs or features relating to ty's generics implementationtyping semanticstyping-module features, spec compliance, etc

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions