Skip to content

Commit d2d8036

Browse files
committed
Allow bare typing.ClassVar qualifiers
1 parent fafcdeb commit d2d8036

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

docs/spec/annotations.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ The following grammar describes the allowed elements of type and annotation expr
109109
annotation_expression: <Required> '[' `annotation_expression` ']'
110110
: | <NotRequired> '[' `annotation_expression` ']'
111111
: | <ReadOnly> '[' `annotation_expression`']'
112-
: | <ClassVar> '[' `annotation_expression`']'
112+
: | <ClassVar> ('[' `annotation_expression`']')?
113113
: | <Final> ('[' `annotation_expression`']')?
114114
: | <InitVar> '[' `annotation_expression` ']'
115115
: | <Annotated> '[' `annotation_expression` ','

docs/spec/class-compat.rst

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,35 @@ Class type assignability
1010

1111
(Originally specified in :pep:`526`.)
1212

13-
A :term:`type qualifier` ``ClassVar[T]`` exists in the :py:mod:`typing`
14-
module. It accepts only a single argument that should be a valid type,
15-
and is used to annotate class variables that should not be set on class
16-
instances. This restriction is enforced by static checkers,
17-
but not at runtime.
13+
The :py:data:`typing.ClassVar` :term:`type qualifier` is used to annotate
14+
class variables that should not be set on class instances. This restriction
15+
is enforced by static checkers, but not at runtime.
16+
17+
Syntax
18+
^^^^^^
19+
20+
:py:data:`~typing.ClassVar` may be used in one of several forms:
21+
22+
* With an explicit type, using the syntax ``ClassVar[<type>]``. Example::
23+
24+
class C:
25+
x: ClassVar[float] = 1
26+
27+
* With no type annotation. Example::
28+
29+
class C:
30+
y: ClassVar = 2
31+
z: ClassVar
32+
33+
If an assigned value is available (e.g. with ``y``), the type should be
34+
inferred as some type to which this value is :term:`assignable` (in this
35+
case, either ``int``, ``Literal[2]``, or ``Any``).
36+
37+
If the ``ClassVar`` qualifier is used without any assigned value, the type
38+
should be inferred to an unknown static type (such as :ref:`Any`).
39+
40+
Semantics and examples
41+
^^^^^^^^^^^^^^^^^^^^^^
1842

1943
Type annotations can be used to annotate class and instance variables
2044
in class bodies and methods. In particular, the value-less notation ``a: int``

0 commit comments

Comments
 (0)