# Bare \`ClassVar\` annotation

**URL:** <https://discuss.python.org/t/bare-classvar-annotation/81705>\
**Category:** Typing\
**Created:** [February 22, 2025, 11:30am UTC](https://discuss.python.org/t/bare-classvar-annotation/81705 "2025-02-22T11:30:14Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Viicos](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/viicos/32/14616_2.png) [@Viicos](https://discuss.python.org/u/Viicos)\
**Post date:** [February 22, 2025, 11:30am UTC](https://discuss.python.org/t/bare-classvar-annotation/81705/1 "2025-02-22T11:30:14Z")

</div>

Should bare `ClassVar` annotations be allowed?:

```python
from typing import ClassVar

class A:
    x: ClassVar = 1

reveal_type(A.x) # int

```

Some references:

- `Final` is explicitly allowed as a bare annotation in [the spec](https://typing.readthedocs.io/en/latest/spec/qualifiers.html#syntax) (the grammar was [recently updated](https://github.com/python/typing/pull/1915)).
- Supported by the CPython implementation since [bpo-46553: allow bare typing.ClassVar annotations by GBeauregard · Pull Request #30983 · python/cpython · GitHub](https://github.com/python/cpython/pull/30983).
- Both mypy and pyright support it, although mypy does not infer the type from the assignment, while it does for `Final` (as per the spec). Playgrounds: [pyright](https://pyright-play.net/?code=GYJw9gtgBALgngBwJYDsDmUkQWEMoDCANgIYDOZAaiSAFD0DGpFUAggFy1TdQAe7hZlRpQAvFACMXHnAHFywujyjTutEAFMAbhpJEA%2BvAQaAFKwB0vAJTrtug0dMW4N%2BiTFsTrkpY8AmWh84f1ogA), [mypy](https://mypy-play.net/?mypy=latest&python=3.12&gist=53ee8efbb5ba256cb1303e446484452f).
- [Ruff issue](https://github.com/astral-sh/ruff/issues/15767), giving more details.

The least path of resistance seems to align the `ClassVar` behavior with `Final`. Thoughts?

---

<div class="post-metadata">

**Author:** ![mikeshardmind](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/mikeshardmind/32/14381_2.png) [@mikeshardmind](https://discuss.python.org/u/mikeshardmind)\
**Post date:** [February 22, 2025, 3:09pm UTC](https://discuss.python.org/t/bare-classvar-annotation/81705/2 "2025-02-22T15:09:14Z")

</div>

Aligning it being allowed is probably a good thing here, but ruff as a linter\[1\] might still want to flag this (as it does for many other annotations) as this could result in inference-defined (allowed to diverge across type checkers) behavior in an exported type.

* * *

1. I’m aware the issue is actually for red-knot, the in-progress type checker, but answering holistically about what is allowed in the type system versus flaggable for a reason.

---

<div class="post-metadata">

**Author:** ![Jelle](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jelle/32/1049_2.png) [@Jelle](https://discuss.python.org/u/Jelle)\
**Post date:** [February 22, 2025, 3:54pm UTC](https://discuss.python.org/t/bare-classvar-annotation/81705/3 "2025-02-22T15:54:25Z")

</div>

Should the inferred type be `int` or `Literal[1]`? I think the reason it makes sense to allow bare `Final` is that it’s sensible to always infer the narrowest type, but with `ClassVar` that’s not the case.

---

<div class="post-metadata">

**Author:** ![mikeshardmind](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/mikeshardmind/32/14381_2.png) [@mikeshardmind](https://discuss.python.org/u/mikeshardmind)\
**Post date:** [February 22, 2025, 4:32pm UTC](https://discuss.python.org/t/bare-classvar-annotation/81705/4 "2025-02-22T16:32:29Z")

</div>

If (and this is a big if IMO) we’re at the point of specifying inference behavior, in the absence of Final, a literal value should be inferred as the type of the literal, not as the more constrained literal type (so in this case `int`)

If anyone thinks the alternative, what would be the purpose of it not being Final if it’s a constant (can only be that exact literal value)?

---

<div class="post-metadata">

**Author:** ![carljm](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/carljm/32/959_2.png) [@carljm](https://discuss.python.org/u/carljm)\
**Post date:** [February 22, 2025, 6:06pm UTC](https://discuss.python.org/t/bare-classvar-annotation/81705/5 "2025-02-22T18:06:52Z")

</div>

I do not think we should attempt to specify inference as part of this issue. If we decide that bare `ClassVar` should be allowed, then it should be specified simply as “type is inferred as some type to which the RHS, if any, is assignable.” (I think mypy’s current behavior of choosing `Any` should thus also be allowable under this specification.)

We also should specify the behavior of bare `ClassVar` with no RHS; both mypy and pyright currently agree that this is not an error and the inferred type is Any/Unknown.

---

<div class="post-metadata">

**Author:** ![Viicos](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/viicos/32/14616_2.png) [@Viicos](https://discuss.python.org/u/Viicos)\
**Post date:** [February 24, 2025, 4:39pm UTC](https://discuss.python.org/t/bare-classvar-annotation/81705/6 "2025-02-24T16:39:23Z")

</div>

I went with what @carljm mentioned:

- Allow bare `ClassVar` to be used: if an assigned value is available, the type should be inferred as some type to which this value is assignable. If no assigned value is available, the type  
should be inferred to an unknown static type (such as `Any`).

PR:

> <https://github.com/python/typing/pull/1931>
>
> As per https://discuss.python.org/t/81705.

Does this require an approval from the typing council?

---

<div class="post-metadata">

**Author:** ![Jelle](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jelle/32/1049_2.png) [@Jelle](https://discuss.python.org/u/Jelle)\
**Post date:** [February 24, 2025, 4:57pm UTC](https://discuss.python.org/t/bare-classvar-annotation/81705/7 "2025-02-24T16:57:35Z")

</div>

I suppose type checkers should treat a bare ClassVar similar to an unannotated global. Since we already have that concept, it makes sense to me to allow bare ClassVar with the expectation that type checkers will use similar inference.

> [@Viicos](#):
>
> Does this require an approval from the typing council?

Yes.

---

<div class="post-metadata">

**Author:** ![Avasam](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/avasam/32/14989_2.png) [@Avasam](https://discuss.python.org/u/Avasam)\
**Post date:** [February 24, 2025, 5:51pm UTC](https://discuss.python.org/t/bare-classvar-annotation/81705/8 "2025-02-24T17:51:42Z")

</div>

Glad I found this post. I wanted to ask about this exactly.  
In real-world scenario, my use-case is to explicitly mark ClassVars as such on vars where the inferred type was already correct, but not marked as a ClassVariable.

I’ve been doing this a lot in distutils/setuptools. For example:

```py
class Distribution:
    common_usage = """\
Common commands: (see '--help-commands' for more)

  setup.py build will build the package underneath 'build/'
  setup.py install will install the package
"""
    display_option_names = [translate_longopt(x[0]) for x in display_options]

```

I’d like to be able to do:

```py

class Distribution:
    common_usage: ClassVar = """\
Common commands: (see '--help-commands' for more)

  setup.py build will build the package underneath 'build/'
  setup.py install will install the package
"""
    display_option_names: ClassVar = [translate_longopt(x[0]) for x in display_options]

```

Instead of:

```py

class Distribution:
    common_usage: ClassVar[str] = """\
Common commands: (see '--help-commands' for more)

  setup.py build will build the package underneath 'build/'
  setup.py install will install the package
"""
    display_option_names: ClassVar[list[str]] = [translate_longopt(x[0]) for x in display_options]

```

If the infered type was the literal (like `Final` does), then for me that’d defeat the purpose of wanting implicit annotation.

Also if your classvar is only allowed to be a specific Literal, shouldn’t you use `Final`? Either way a subclass changing the value would be in violation.

---

<div class="post-metadata">

**Author:** ![jorenham](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jorenham/32/27806_2.png) [@jorenham](https://discuss.python.org/u/jorenham)\
**Post date:** [March 1, 2025, 5:15pm UTC](https://discuss.python.org/t/bare-classvar-annotation/81705/9 "2025-03-01T17:15:55Z")

</div>

In the basedpyright there’s the `reportUnannotatedClassAttribute` rule, which only allows bare `ClassVar` if the class is `@final`.

So

```py
class A:
    value: ClassVar = 1

```

reports

```python
Type annotation for attribute `value` is required because this class is not decorated with `@final` (reportUnannotatedClassAttribute)

```

and

```python
@final
class B:
    value: ClassVar = 1

```

is accepted.

- [basedpyright playground](https://basedpyright.com/?typeCheckingMode=all&reportUnusedParameter=true&code=GYJw9gtgBALgngBwJYDsDmUkQWEMoDCANgIYDOZAaiSADRTColECwAUOwMakVQCCALnZQRUAG7MArgFMBhHlRpQAvFACMIgMRQAoiHAh27AAKMUzLgqgAhIW1Hips%2BeUUgV6rVADyAa3ZAA)
- [`reportUnannotatedClassAttribute` docs](https://docs.basedpyright.com/latest/benefits-over-pyright/new-diagnostic-rules/#reportunannotatedclassattribute)

---

<div class="post-metadata">

**Author:** ![Viicos](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/viicos/32/14616_2.png) [@Viicos](https://discuss.python.org/u/Viicos)\
**Post date:** [March 17, 2025, 7:56pm UTC](https://discuss.python.org/t/bare-classvar-annotation/81705/10 "2025-03-17T19:56:35Z")

</div>

I created the TC request: [Typing specification update: Allow bare `typing.ClassVar` qualifiers · Issue #43 · python/typing-council · GitHub](https://github.com/python/typing-council/issues/43).

---

<div class="post-metadata">

**Author:** ![erictraut](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/erictraut/32/15190_2.png) [@erictraut](https://discuss.python.org/u/erictraut)\
**Post date:** [April 9, 2025, 4:56pm UTC](https://discuss.python.org/t/bare-classvar-annotation/81705/11 "2025-04-09T16:56:03Z")

</div>

This proposal has been accepted by the TC and will be incorporated into the typing spec.

Thanks @Victorien for driving the proposal and to everyone else who contributed.
