[issue45283] Top / function argument level ClassVar should not be allowed during `get_type_hints()`

Nikita Sobolev report at bugs.python.org
Fri Sep 24 09:44:07 EDT 2021


New submission from Nikita Sobolev <mail at sobolevn.me>:

This code currently does not raise any issues:

```python
# contents of `ex.py`
from typing import ClassVar

a: ClassVar[int] = 1
def b(c: ClassVar[int]): ...
```

And then: 
1. Module: `python -c 'import ex; from typing import get_type_hints; print(get_type_hints(ex))'`
2. Function argument: `python -c 'import ex; from typing import get_type_hints; print(get_type_hints(ex.b))'`

It should not be allowed. Currently, the same with `from __future__ import annotations` does correctly raise `TypeError: typing.ClassVar[int] is not valid as type argument`

Related: https://github.com/python/cpython/pull/28279

I will send a PR with the fix shortly.

----------
components: Library (Lib)
messages: 402563
nosy: sobolevn
priority: normal
severity: normal
status: open
title: Top / function argument level ClassVar should not be allowed during `get_type_hints()`
type: behavior
versions: Python 3.10, Python 3.11, Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue45283>
_______________________________________


More information about the Python-bugs-list mailing list