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)
Comment on lines -24 to +25
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we can merge this without a benchmark. Traditionally we check a specific name like "parents" instead of inferring every single name node to check for some edge case like this.

@Pierre-Sassoulas what do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could check for the specific name "parents" but not anything else.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think we do need a benchmark in this case.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That being said assigning path.parents is not weird code that is never seen so I think we need to handle most of it and find some clever optimisation/compromise that eludes me right now.

):
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().lstrip(".").endswith("_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