| 1 |
# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html |
| 2 |
# For details: https://github.com/pylint-dev/astroid/blob/main/LICENSE |
| 3 |
# Copyright (c) https://github.com/pylint-dev/astroid/blob/main/CONTRIBUTORS.txt |
| 4 |
|
| 5 |
import pytest |
| 6 |
|
| 7 |
from astroid import extract_node, nodes |
| 8 |
from astroid.brain.helpers import is_class_var |
| 9 |
|
| 10 |
|
| 11 |
@pytest.mark.parametrize( |
| 12 |
"code", |
| 13 |
[ |
| 14 |
pytest.param( |
| 15 |
""" |
| 16 |
from typing import ClassVar |
| 17 |
|
| 18 |
foo: ClassVar[int] |
| 19 |
""", |
| 20 |
id="from-import", |
| 21 |
), |
| 22 |
pytest.param( |
| 23 |
""" |
| 24 |
from typing import ClassVar |
| 25 |
|
| 26 |
foo: ClassVar |
| 27 |
""", |
| 28 |
id="bare-classvar", |
| 29 |
), |
| 30 |
pytest.param( |
| 31 |
""" |
| 32 |
import typing |
| 33 |
|
| 34 |
foo: typing.ClassVar[int] |
| 35 |
""", |
| 36 |
id="module-import", |
| 37 |
), |
| 38 |
], |
| 39 |
) |
| 40 |
def test_is_class_var_returns_true(code): |
| 41 |
node = extract_node(code) |
| 42 |
assert isinstance(node, nodes.AnnAssign) |
| 43 |
assert is_class_var(node.annotation) |
| 44 |
|
| 45 |
|
| 46 |
@pytest.mark.parametrize( |
| 47 |
"code", |
| 48 |
[ |
| 49 |
pytest.param( |
| 50 |
""" |
| 51 |
from typing import Final |
| 52 |
|
| 53 |
foo: Final[int] |
| 54 |
""", |
| 55 |
id="wrong-name", |
| 56 |
), |
| 57 |
pytest.param( |
| 58 |
""" |
| 59 |
from typing import ClassVar |
| 60 |
|
| 61 |
foo: list[ClassVar[int]] |
| 62 |
""", |
| 63 |
id="classvar-not-outermost", |
| 64 |
), |
| 65 |
pytest.param( |
| 66 |
""" |
| 67 |
from typing import ClassVar |
| 68 |
ClassVar = int |
| 69 |
|
| 70 |
foo: ClassVar |
| 71 |
""", |
| 72 |
id="shadowed-name", |
| 73 |
), |
| 74 |
pytest.param( |
| 75 |
""" |
| 76 |
foo: ClassVar[int] |
| 77 |
""", |
| 78 |
id="missing-import", |
| 79 |
), |
| 80 |
], |
| 81 |
) |
| 82 |
def test_is_class_var_returns_false(code): |
| 83 |
node = extract_node(code) |
| 84 |
assert isinstance(node, nodes.AnnAssign) |
| 85 |
assert not is_class_var(node.annotation) |