Debian Codegraph @

tests/brain/test_pathlib.py

raw
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
6 import astroid
7 from astroid import bases
8 from astroid.const import PY313
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 from pathlib import Path
16
17 current_path = Path().resolve()
18 path_parents = current_path.parents
19 path_parents
20 """)
21 inferred = name_node.inferred()
22 assert len(inferred) == 1
23 assert isinstance(inferred[0], bases.Instance)
24 if PY313:
25 assert inferred[0].qname() == "builtins.tuple"
26 else:
27 assert inferred[0].qname() == "pathlib._PathParents"
28
29
30 def test_inference_parents_subscript_index() -> None:
31 """Test inference of ``pathlib.Path.parents``, accessed by index."""
32 path = astroid.extract_node("""
33 from pathlib import Path
34
35 current_path = Path().resolve()
36 current_path.parents[2] #@
37 """)
38
39 inferred = path.inferred()
40 assert len(inferred) == 1
41 assert isinstance(inferred[0], bases.Instance)
42 if PY313:
43 assert inferred[0].qname() == "pathlib._local.Path"
44 else:
45 assert inferred[0].qname() == "pathlib.Path"
46
47
48 def test_inference_parents_subscript_slice() -> None:
49 """Test inference of ``pathlib.Path.parents``, accessed by slice."""
50 name_node = astroid.extract_node("""
51 from pathlib import Path
52
53 current_path = Path().resolve()
54 parent_path = current_path.parents[:2]
55 parent_path
56 """)
57 inferred = name_node.inferred()
58 assert len(inferred) == 1
59 assert isinstance(inferred[0], bases.Instance)
60 assert inferred[0].qname() == "builtins.tuple"
61
62
63 def test_inference_parents_subscript_not_path() -> None:
64 """Test inference of other ``.parents`` subscripts is unaffected."""
65 name_node = astroid.extract_node("""
66 class A:
67 parents = 42
68
69 c = A()
70 error = c.parents[:2]
71 error
72 """)
73 inferred = name_node.inferred()
74 assert len(inferred) == 1
75 assert inferred[0] is Uninferable