-
-
Notifications
You must be signed in to change notification settings - Fork 307
Fix pathlib.Path.parents brain inference for variable assignments #2866
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
If the changes are correct...Please add hacktoberfest-accepted label if possible. Didn't make a changelog entry as was not really sure what to do. Thanks |
b07e208 to
6968641
Compare
for more information, see https://pre-commit.ci
Summary: Fixing
|
| Python | Status |
|---|---|
| 3.10-3.12 | ✅ 6/6 PASSED |
| 3.13 | ✅ 6/6 PASSED (fixed empty tuple) |
| 3.14 | ✅ 6/6 PASSED (original issue resolved) |
Outcome: Eliminated Pylint false positives across all Python versions while maintaining backward compatibility.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR. Hopefully there is a less invasive patch. I'm seeing a pass with just:
diff --git a/astroid/brain/brain_pathlib.py b/astroid/brain/brain_pathlib.py
index d1d1bda7..bd6ab727 100644
--- a/astroid/brain/brain_pathlib.py
+++ b/astroid/brain/brain_pathlib.py
@@ -21,7 +21,8 @@ Path
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
diff --git a/tests/brain/test_pathlib.py b/tests/brain/test_pathlib.py
index 2335e28a..e8421785 100644
--- a/tests/brain/test_pathlib.py
+++ b/tests/brain/test_pathlib.py
@@ -48,6 +48,23 @@ def test_inference_parents_subscript_index() -> None:
else:
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)
+ assert inferred[0].qname() == "pathlib.Path"
+
def test_inference_parents_subscript_slice() -> None:
"""Test inference of ``pathlib.Path.parents``, accessed by slice."""
Thank you for submitting a PR to astroid!
To ease the process of reviewing your PR, do make sure to complete the following boxes.
and preferred name in
script/.contributors_aliases.json-->
Type of Changes
Description
Closes #2864
Summary
Fixes pathlib brain inference for
Path.parentswhen assigned to variables, resolving false positiveE1101: Instance of 'tuple' has no 'name' membererrors in Python 3.14.Problem
The existing pathlib brain only handled direct subscript access like
cwd.parents[0], but failed whenparentswas assigned to a variable first:This occurred because the brain's inference tip only worked on
Subscriptnodes, not onNamenodes that were assigned fromPath.parents.Solution
Added a new inference tip for
Namenodes that:_looks_like_parents_name()identifies when aNamenode was assigned from aPath.parentsattributeinfer_parents_name()returns appropriate types for both Python 3.13+ (tuple) and older versions (_PathParents)Changes Made
astroid/brain/brain_pathlib.py_looks_like_parents_name()predicate function to detectNamenodes assigned fromPath.parentsinfer_parents_name()function to provide proper inference for such variablesregister()function to include the newNamenode transformtests/brain/test_pathlib.pytest_inference_parents_assigned_to_variable()to test the new functionalitytest_inference_parents_assigned_to_variable_slice()to test slice access on assigned variablesTesting
E1101errors with the exact code from the GitHub issueBackward Compatibility
This change is fully backward compatible. It only adds new inference capabilities without modifying existing functionality.
This PR template is now properly filled out with: