Skip to content

Commit 977b89e

Browse files
uy-rrodriguezatodorov
authored andcommitted
Update forms.py to fix #284
An exception `AttributeError` is raised when `child.targets[0]` does not have an attribute `name`, as stated in the issue #284 . `meta.get_children()` returns all children of the `Meta` class, which in the sample code contains a dictionary assignment. The expected children are of type `Assign` whose first item in the attribute `targets` is of type `AssignName`, like `AssignName.widgets(name='widgets')`. An instruction like `widget["field"] = forms.SomeWidget` will result in a `Assign` child containing a `Subscript(ctx=<Context.Store: 2>, value=<Name.widgets ...>, slice=<Index ...>)`. This is of course unexpected. I guess other unexpected instructions in the `Meta` class can trigger similar errors, so probably the most economic solution is to enclose the code in `try... except` and ignore the `AttributeError`. Trying to identify the type of object before accessing the `name` attribute would probably incur in a slower execution.
1 parent 7721691 commit 977b89e

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

pylint_django/checkers/forms.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ def visit_classdef(self, node):
4444
if not isinstance(child, Assign):
4545
continue
4646

47-
if child.targets[0].name == 'exclude':
48-
self.add_message('W%s04' % BASE_ID, node=child)
49-
break
47+
# Capture and ignore AttributeError raised when targets[0] does not
48+
# have an attribute "name"
49+
try:
50+
if child.targets[0].name == 'exclude':
51+
self.add_message('W%s04' % BASE_ID, node=child)
52+
break
53+
except AttributeError:
54+
pass

0 commit comments

Comments
 (0)