@@ -2289,26 +2289,26 @@ def _is_variable_violation(
2289
2289
annotation_return = True
2290
2290
if frame .returns .name in defframe .locals :
2291
2291
definition = defframe .locals [node .name ][0 ]
2292
- if (
2293
- definition .lineno is None
2294
- or definition .lineno < frame .lineno
2295
- ):
2296
- # Detect class assignments with a name defined earlier in the
2297
- # class. In this case, no warning should be raised.
2298
- maybe_before_assign = False
2299
- else :
2300
- maybe_before_assign = True
2292
+ # no warning raised if a name was defined earlier in the class
2293
+ maybe_before_assign = (
2294
+ definition .lineno is not None and definition .lineno >= frame .lineno
2295
+ )
2301
2296
else :
2302
2297
maybe_before_assign = True
2303
2298
# Using a name defined in the module if this is a nested function.
2304
2299
elif (
2300
+ # defframe is the class containing the function.
2301
+ # It shouldn't be nested: expect its parent to be a module.
2305
2302
(defframe_parent := next (defframe .node_ancestors ()))
2306
2303
and isinstance (defframe_parent , nodes .Module )
2304
+ # frame is the function inside the class.
2307
2305
and (frame_ancestors := tuple (frame .node_ancestors ()))
2306
+ # Does that function have any functions as ancestors?
2308
2307
and any (
2309
2308
isinstance (ancestor , nodes .FunctionDef )
2310
2309
for ancestor in frame_ancestors
2311
2310
)
2311
+ # And is its last ancestor the same module as the class's?
2312
2312
and frame_ancestors [- 1 ] is defframe_parent
2313
2313
):
2314
2314
annotation_return = True
0 commit comments