Skip to content

Update forms.py to fix #284 #285

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

Merged
merged 3 commits into from
Sep 2, 2020
Merged

Update forms.py to fix #284 #285

merged 3 commits into from
Sep 2, 2020

Conversation

uy-rrodriguez
Copy link
Contributor

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.

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.
@coveralls
Copy link

coveralls commented Aug 6, 2020

Pull Request Test Coverage Report for Build 1258

  • 2 of 2 (100.0%) changed or added relevant lines in 1 file are covered.
  • 4 unchanged lines in 3 files lost coverage.
  • Overall coverage decreased (-1.8%) to 86.867%

Files with Coverage Reduction New Missed Lines %
pylint_django/augmentations/init.py 1 88.61%
pylint_django/checkers/forms.py 1 85.71%
pylint_django/checkers/django_installed.py 2 71.43%
Totals Coverage Status
Change from base Build 1183: -1.8%
Covered Lines: 721
Relevant Lines: 830

💛 - Coveralls

This checks for the expected `AssignName` type in `child.targets[0]` to avoid the `AttributeError`
Copy link

@asottile asottile left a comment

Choose a reason for hiding this comment

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

Copy link
Contributor Author

@uy-rrodriguez uy-rrodriguez left a comment

Choose a reason for hiding this comment

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

This seems cleaner than adding the isinstance before checking for the name, but it may need to be changed if at some point more verification is added to the children and you need to handle other types of items in targets.

Include `Form` class to test that assigning a specific widget doesn't throw an error, as mentioned in #284.
@atodorov atodorov merged commit 6132245 into pylint-dev:master Sep 2, 2020
@uy-rrodriguez uy-rrodriguez deleted the patch-1 branch September 2, 2020 15:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants