|
| 1 | +# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html |
| 2 | +# For details: https://github.com/PyCQA/astroid/blob/main/LICENSE |
| 3 | +# Copyright (c) https://github.com/PyCQA/astroid/blob/main/CONTRIBUTORS.txt |
| 4 | + |
| 5 | + |
| 6 | +import astroid |
| 7 | +from astroid import bases |
| 8 | +from astroid.const import PY310_PLUS |
| 9 | +from astroid.util import Uninferable |
| 10 | + |
| 11 | + |
| 12 | +def test_inference_parents() -> None: |
| 13 | + """Test inference of ``pathlib.Path.parents``.""" |
| 14 | + name_node = astroid.extract_node( |
| 15 | + """ |
| 16 | + from pathlib import Path |
| 17 | +
|
| 18 | + current_path = Path().resolve() |
| 19 | + path_parents = current_path.parents |
| 20 | + path_parents |
| 21 | + """ |
| 22 | + ) |
| 23 | + inferred = name_node.inferred() |
| 24 | + assert len(inferred) == 1 |
| 25 | + assert isinstance(inferred[0], bases.Instance) |
| 26 | + assert inferred[0].qname() == "pathlib._PathParents" |
| 27 | + |
| 28 | + |
| 29 | +def test_inference_parents_subscript_index() -> None: |
| 30 | + """Test inference of ``pathlib.Path.parents``, accessed by index.""" |
| 31 | + parents, path = astroid.extract_node( |
| 32 | + """ |
| 33 | + from pathlib import Path |
| 34 | +
|
| 35 | + current_path = Path().resolve() |
| 36 | + path_parents = current_path.parents |
| 37 | + path_parents #@ |
| 38 | + path_parents[2] #@ |
| 39 | + """ |
| 40 | + ) |
| 41 | + inferred = parents.inferred() |
| 42 | + assert len(inferred) == 1 |
| 43 | + assert isinstance(inferred[0], bases.Instance) |
| 44 | + assert inferred[0].qname() == "pathlib._PathParents" |
| 45 | + |
| 46 | + inferred = path.inferred() |
| 47 | + assert len(inferred) == 1 |
| 48 | + assert isinstance(inferred[0], bases.Instance) |
| 49 | + assert inferred[0].qname() == "pathlib.Path" |
| 50 | + |
| 51 | + |
| 52 | +def test_inference_parents_subscript_slice() -> None: |
| 53 | + """Test inference of ``pathlib.Path.parents``, accessed by slice.""" |
| 54 | + name_node = astroid.extract_node( |
| 55 | + """ |
| 56 | + from pathlib import Path |
| 57 | +
|
| 58 | + current_path = Path().resolve() |
| 59 | + parent_path = current_path.parents[:2] |
| 60 | + parent_path |
| 61 | + """ |
| 62 | + ) |
| 63 | + inferred = name_node.inferred() |
| 64 | + assert len(inferred) == 1 |
| 65 | + if PY310_PLUS: |
| 66 | + assert isinstance(inferred[0], bases.Instance) |
| 67 | + assert inferred[0].qname() == "builtins.tuple" |
| 68 | + else: |
| 69 | + assert inferred[0] is Uninferable |
| 70 | + |
| 71 | + |
| 72 | +def test_inference_parents_subscript_not_path() -> None: |
| 73 | + """Test inference of other ``.parents`` subscripts is unaffected.""" |
| 74 | + name_node = astroid.extract_node( |
| 75 | + """ |
| 76 | + class A: |
| 77 | + parents = 42 |
| 78 | +
|
| 79 | + c = A() |
| 80 | + error = c.parents[:2] |
| 81 | + error |
| 82 | + """ |
| 83 | + ) |
| 84 | + inferred = name_node.inferred() |
| 85 | + assert len(inferred) == 1 |
| 86 | + assert inferred[0] is Uninferable |
0 commit comments