Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion astroid/brain/brain_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@

def _looks_like_parents_subscript(node: nodes.Subscript) -> bool:
if not (
isinstance(node.value, nodes.Attribute) and node.value.attrname == "parents"
(isinstance(node.value, nodes.Attribute) and node.value.attrname == "parents")
or isinstance(node.value, nodes.Name)
):
return False

Expand Down
23 changes: 22 additions & 1 deletion tests/brain/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def test_inference_parents() -> None:
if PY313:
assert inferred[0].qname() == "builtins.tuple"
else:
assert inferred[0].qname() == "pathlib._PathParents"
assert inferred[0].qname() == "._PathParents"


def test_inference_parents_subscript_index() -> None:
Expand All @@ -49,6 +49,27 @@ def test_inference_parents_subscript_index() -> None:
assert inferred[0].qname() == "pathlib.Path"


def test_inference_parents_assigned_to_variable() -> None:
"""Test inference of ``pathlib.Path.parents`` when assigned to a variable."""
name_node = astroid.extract_node(
"""
from pathlib import Path

cwd = Path.cwd()
parents = cwd.parents
parents[0] #@
"""
)

inferred = name_node.inferred()
assert len(inferred) == 1
assert isinstance(inferred[0], bases.Instance)
if PY313:
assert inferred[0].qname() == "pathlib._local.Path"
else:
assert inferred[0].qname() == "pathlib.Path"


def test_inference_parents_subscript_slice() -> None:
"""Test inference of ``pathlib.Path.parents``, accessed by slice."""
name_node = astroid.extract_node(
Expand Down
Loading