| 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 |
from __future__ import annotations |
| 6 |
|
| 7 |
import pytest |
| 8 |
|
| 9 |
from astroid import builder, nodes |
| 10 |
|
| 11 |
try: |
| 12 |
import typing_extensions |
| 13 |
|
| 14 |
HAS_TYPING_EXTENSIONS = True |
| 15 |
HAS_TYPING_EXTENSIONS_TYPEVAR = hasattr(typing_extensions, "TypeVar") |
| 16 |
except ImportError: |
| 17 |
HAS_TYPING_EXTENSIONS = False |
| 18 |
HAS_TYPING_EXTENSIONS_TYPEVAR = False |
| 19 |
|
| 20 |
|
| 21 |
@pytest.mark.skipif( |
| 22 |
not HAS_TYPING_EXTENSIONS, |
| 23 |
reason="These tests require the typing_extensions library", |
| 24 |
) |
| 25 |
class TestTypingExtensions: |
| 26 |
@staticmethod |
| 27 |
@pytest.mark.skipif( |
| 28 |
not HAS_TYPING_EXTENSIONS_TYPEVAR, |
| 29 |
reason="Need typing_extensions>=4.4.0 to test TypeVar", |
| 30 |
) |
| 31 |
def test_typing_extensions_types() -> None: |
| 32 |
ast_nodes = builder.extract_node(""" |
| 33 |
from typing_extensions import TypeVar |
| 34 |
TypeVar('MyTypeVar', int, float, complex) #@ |
| 35 |
TypeVar('AnyStr', str, bytes) #@ |
| 36 |
""") |
| 37 |
for node in ast_nodes: |
| 38 |
inferred = next(node.infer()) |
| 39 |
assert isinstance(inferred, nodes.ClassDef) |