Skip to content

Commit 1935995

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 6968641 commit 1935995

File tree

2 files changed

+37
-19
lines changed

2 files changed

+37
-19
lines changed

astroid/brain/brain_pathlib.py

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ def infer_parents_subscript(
4747
# For slices, return a tuple
4848
parents_tuple = nodes.Tuple()
4949
path_cls = next(_extract_single_node(PATH_TEMPLATE).infer())
50-
parents_tuple.elts = [path_cls.instantiate_class() for _ in range(2)] # Mock some parents
50+
parents_tuple.elts = [
51+
path_cls.instantiate_class() for _ in range(2)
52+
] # Mock some parents
5153
return iter([parents_tuple])
5254

5355
raise UseInferenceDefault
@@ -58,32 +60,37 @@ def _looks_like_parents_name(node: nodes.Name) -> bool:
5860
# Only apply to direct Name nodes, not to Name nodes in subscripts
5961
if isinstance(node.parent, nodes.Subscript):
6062
return False
61-
63+
6264
# Only apply to Name nodes that are direct expressions, not in subscripts
6365
if not isinstance(node.parent, nodes.Expr):
6466
return False
65-
67+
6668
# Look for the assignment in the current scope
6769
try:
6870
frame, stmts = node.lookup(node.name)
6971
if not stmts:
7072
return False
71-
73+
7274
# Check each assignment statement
7375
for stmt in stmts:
7476
if isinstance(stmt, nodes.AssignName):
7577
# Get the parent Assign node
7678
assign_node = stmt.parent
7779
if isinstance(assign_node, nodes.Assign):
7880
# Check if the value is an Attribute access to .parents
79-
if (isinstance(assign_node.value, nodes.Attribute)
80-
and assign_node.value.attrname == "parents"):
81+
if (
82+
isinstance(assign_node.value, nodes.Attribute)
83+
and assign_node.value.attrname == "parents"
84+
):
8185
try:
8286
# Check if the attribute is from a Path object
8387
value = next(assign_node.value.expr.infer())
84-
if (isinstance(value, bases.Instance)
88+
if (
89+
isinstance(value, bases.Instance)
8590
and isinstance(value._proxied, nodes.ClassDef)
86-
and value.qname() in ("pathlib.Path", "pathlib._local.Path")):
91+
and value.qname()
92+
in ("pathlib.Path", "pathlib._local.Path")
93+
):
8794
return True
8895
except (InferenceError, StopIteration):
8996
pass
@@ -99,16 +106,20 @@ def infer_parents_name(
99106
if PY313:
100107
# For Python 3.13+, parents is a tuple
101108
from astroid import nodes
109+
102110
# Create a tuple that behaves like Path.parents
103111
parents_tuple = nodes.Tuple()
104112
# Add some mock Path elements to make indexing work
105113
path_cls = next(_extract_single_node(PATH_TEMPLATE).infer())
106-
parents_tuple.elts = [path_cls.instantiate_class() for _ in range(3)] # Mock some parents
114+
parents_tuple.elts = [
115+
path_cls.instantiate_class() for _ in range(3)
116+
] # Mock some parents
107117
return iter([parents_tuple])
108118
else:
109119
# For older versions, it's a _PathParents object
110120
# We need to create a mock _PathParents instance that behaves correctly
111-
parents_cls = _extract_single_node("""
121+
parents_cls = _extract_single_node(
122+
"""
112123
class _PathParents:
113124
def __getitem__(self, key):
114125
from pathlib import Path
@@ -117,27 +128,30 @@ def __getitem__(self, key):
117128
return (Path(), Path())
118129
# For indexing, return a Path object
119130
return Path()
120-
""")
131+
"""
132+
)
121133
return iter([parents_cls.instantiate_class()])
122134

123135

124136
def _looks_like_parents_attribute(node: nodes.Attribute) -> bool:
125137
"""Check if an Attribute node is accessing Path.parents."""
126138
if node.attrname != "parents":
127139
return False
128-
140+
129141
# Check if the expression is a Path object
130142
try:
131143
expr_inferred = list(node.expr.infer())
132144
if expr_inferred and not isinstance(expr_inferred[0], util.UninferableBase):
133145
expr_value = expr_inferred[0]
134-
if (isinstance(expr_value, bases.Instance)
146+
if (
147+
isinstance(expr_value, bases.Instance)
135148
and isinstance(expr_value._proxied, nodes.ClassDef)
136-
and expr_value.qname() in ("pathlib.Path", "pathlib._local.Path")):
149+
and expr_value.qname() in ("pathlib.Path", "pathlib._local.Path")
150+
):
137151
return True
138152
except (InferenceError, StopIteration):
139153
pass
140-
154+
141155
return False
142156

143157

@@ -149,11 +163,14 @@ def infer_parents_attribute(
149163
# For Python 3.13+, parents is a tuple
150164
parents_tuple = nodes.Tuple()
151165
path_cls = next(_extract_single_node(PATH_TEMPLATE).infer())
152-
parents_tuple.elts = [path_cls.instantiate_class() for _ in range(3)] # Mock some parents
166+
parents_tuple.elts = [
167+
path_cls.instantiate_class() for _ in range(3)
168+
] # Mock some parents
153169
return iter([parents_tuple])
154170
else:
155171
# For older versions, it's a _PathParents object
156-
parents_cls = _extract_single_node("""
172+
parents_cls = _extract_single_node(
173+
"""
157174
class _PathParents:
158175
def __getitem__(self, key):
159176
from pathlib import Path
@@ -162,7 +179,8 @@ def __getitem__(self, key):
162179
return (Path(), Path())
163180
# For indexing, return a Path object
164181
return Path()
165-
""")
182+
"""
183+
)
166184
return iter([parents_cls.instantiate_class()])
167185

168186

tests/brain/test_pathlib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,4 @@ def test_inference_parents_assigned_to_variable_slice() -> None:
121121
inferred = name_node.inferred()
122122
assert len(inferred) == 1
123123
assert isinstance(inferred[0], bases.Instance)
124-
assert inferred[0].qname() == "builtins.tuple"
124+
assert inferred[0].qname() == "builtins.tuple"

0 commit comments

Comments
 (0)