| 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 bases, builder, nodes |
| 8 |
from astroid.exceptions import InferenceError |
| 9 |
|
| 10 |
|
| 11 |
def test_infer_typevar() -> None: |
| 12 |
""" |
| 13 |
Regression test for: https://github.com/pylint-dev/pylint/issues/8802 |
| 14 |
|
| 15 |
Test that an inferred `typing.TypeVar()` call produces a `nodes.ClassDef` |
| 16 |
node. |
| 17 |
""" |
| 18 |
call_node = builder.extract_node(""" |
| 19 |
from typing import TypeVar |
| 20 |
TypeVar('My.Type') |
| 21 |
""") |
| 22 |
with pytest.raises(InferenceError): |
| 23 |
call_node.inferred() |
| 24 |
|
| 25 |
|
| 26 |
class TestTypingAlias: |
| 27 |
def test_infer_typing_alias(self) -> None: |
| 28 |
""" |
| 29 |
Test that _alias() calls can be inferred. |
| 30 |
""" |
| 31 |
node = builder.extract_node(""" |
| 32 |
from typing import _alias |
| 33 |
x = _alias(int, float) |
| 34 |
""") |
| 35 |
assert isinstance(node, nodes.Assign) |
| 36 |
assert isinstance(node.value, nodes.Call) |
| 37 |
inferred = next(node.value.infer()) |
| 38 |
assert isinstance(inferred, nodes.ClassDef) |
| 39 |
assert len(inferred.bases) == 1 |
| 40 |
assert inferred.bases[0].name == "int" |
| 41 |
|
| 42 |
@pytest.mark.parametrize( |
| 43 |
"alias_args", |
| 44 |
[ |
| 45 |
"", # two missing arguments |
| 46 |
"int", # one missing argument |
| 47 |
"int, float, tuple", # one additional argument |
| 48 |
], |
| 49 |
) |
| 50 |
def test_infer_typing_alias_incorrect_number_of_arguments( |
| 51 |
self, alias_args: str |
| 52 |
) -> None: |
| 53 |
""" |
| 54 |
Regression test for: https://github.com/pylint-dev/astroid/issues/2513 |
| 55 |
|
| 56 |
Test that _alias() calls with the incorrect number of arguments can be inferred. |
| 57 |
""" |
| 58 |
node = builder.extract_node(f""" |
| 59 |
from typing import _alias |
| 60 |
x = _alias({alias_args}) |
| 61 |
""") |
| 62 |
assert isinstance(node, nodes.Assign) |
| 63 |
assert isinstance(node.value, nodes.Call) |
| 64 |
inferred = next(node.value.infer()) |
| 65 |
assert isinstance(inferred, bases.Instance) |
| 66 |
assert inferred.name == "_SpecialGenericAlias" |
| 67 |
|
| 68 |
|
| 69 |
class TestSpecialAlias: |
| 70 |
@pytest.mark.parametrize( |
| 71 |
"code", |
| 72 |
[ |
| 73 |
"_CallableType()", |
| 74 |
"_TupleType()", |
| 75 |
], |
| 76 |
) |
| 77 |
def test_special_alias_no_crash_on_empty_args(self, code: str) -> None: |
| 78 |
""" |
| 79 |
Regression test for: https://github.com/pylint-dev/astroid/issues/2772 |
| 80 |
|
| 81 |
Test that _CallableType() and _TupleType() calls with no arguments |
| 82 |
do not cause an IndexError. |
| 83 |
""" |
| 84 |
# Should not raise IndexError |
| 85 |
module = builder.parse(code) |
| 86 |
assert isinstance(module, nodes.Module) |