Skip to content

AttributeError: 'Subscript' object has no attribute 'name' #284

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

Closed
uy-rrodriguez opened this issue Aug 6, 2020 · 1 comment
Closed

AttributeError: 'Subscript' object has no attribute 'name' #284

uy-rrodriguez opened this issue Aug 6, 2020 · 1 comment

Comments

@uy-rrodriguez
Copy link
Contributor

Dear all,

It's been a while I've experienced an issue when running pylint with pylint_django. I include the error traceback and the line I think causes the problem.

Traceback:

Traceback (most recent call last):
  File "D:\DevTools\Python38-32\lib\runpy.py", line 193, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "D:\DevTools\Python38-32\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "D:\DevRepo\dex\venv\Scripts\pylint.exe\__main__.py", line 9, in <module>
  File "d:\devrepo\dex\venv\lib\site-packages\pylint\__init__.py", line 22, in run_pylint
    PylintRun(sys.argv[1:])
  File "d:\devrepo\dex\venv\lib\site-packages\pylint\lint\run.py", line 344, in __init__
    linter.check(args)
  File "d:\devrepo\dex\venv\lib\site-packages\pylint\lint\pylinter.py", line 870, in check
    self._check_files(
  File "d:\devrepo\dex\venv\lib\site-packages\pylint\lint\pylinter.py", line 904, in _check_files
    self._check_file(get_ast, check_astroid_module, name, filepath, modname)
  File "d:\devrepo\dex\venv\lib\site-packages\pylint\lint\pylinter.py", line 930, in _check_file
    check_astroid_module(ast_node)
  File "d:\devrepo\dex\venv\lib\site-packages\pylint\lint\pylinter.py", line 1062, in check_astroid_module 
    retval = self._check_astroid_module(
  File "d:\devrepo\dex\venv\lib\site-packages\pylint\lint\pylinter.py", line 1107, in _check_astroid_module
    walker.walk(ast_node)
  File "d:\devrepo\dex\venv\lib\site-packages\pylint\utils\ast_walker.py", line 75, in walk
    self.walk(child)
  File "d:\devrepo\dex\venv\lib\site-packages\pylint\utils\ast_walker.py", line 72, in walk
    callback(astroid)
  File "d:\devrepo\dex\venv\lib\site-packages\pylint_django\checkers\forms.py", line 47, in visit_classdef
    if child.targets[0].name == 'exclude':
AttributeError: 'Subscript' object has no attribute 'name'

After completely cleaned the file forms.py giving the error I think I've managed to isolate the problematic line. The file, still giving the error, now contains only this:

from django import forms

from dex import models


class DataSelectionForm(forms.ModelForm):
    """
    Form
    """
    class Meta:
        model = models.DataSelection
        fields = [
            "field",
        ]
        widgets = {}
        widgets["field"] = forms.CheckboxSelectMultiple

Of course, I've overly simplified that form class. It seems the error is caused by widgets["field"] = forms.CheckboxSelectMultiple, which in the real code is adding a different widget to a specific field after a loop creates the widgets dict.

If I use instead widgets = {"field": forms.CheckboxSelectMultiple} then everything is fine.

A few details about my configuration:

  • OS: Windows 10 Pro x64
  • Pip: 19.2.3
  • Python: 3.8.2

Result of pip freeze:

astroid==2.4.2
certifi==2020.6.20
chardet==3.0.4
colorama==0.4.3
Django==2.2.14
django-activity-stream==0.9.0
django-annoying==0.10.6
django-filter==2.3.0
django-formtools==2.2
django-jsonstore==0.4.1
django-material==1.6.7
django-simple-history==2.11.0
django-viewflow==1.6.1
docutils==0.16
gunicorn==20.0.4
idna==2.10
isort==4.3.21
lazy-object-proxy==1.4.3
mccabe==0.6.1
oauthlib==3.1.0
Pillow==7.2.0
psycopg2==2.8.5
pyasn1==0.4.8
pyasn1-modules==0.2.8
pylint==2.5.3
pylint-django==2.2.0
pylint-plugin-utils==0.6
python-ldap==3.3.1
python-magic==0.4.18
python-magic-bin==0.4.14
pytz==2020.1
PyYAML==5.3.1
requests==2.24.0
requests-oauthlib==1.3.0
rope==0.17.0
six==1.15.0
sqlparse==0.3.1
toml==0.10.1
urllib3==1.25.10
wrapt==1.12.1

Thank you for your time!

@uy-rrodriguez
Copy link
Contributor Author

After digging into pylint_django's code I've found the following:

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'll submit a PR fixing that, if that's fine for you. 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.

atodorov pushed a commit that referenced this issue Sep 2, 2020
This checks for the expected `AssignName` type in `child.targets[0]` to avoid the `AttributeError`
atodorov pushed a commit that referenced this issue Sep 2, 2020
Include `Form` class to test that assigning a specific widget doesn't throw an error, as mentioned in #284.
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

No branches or pull requests

1 participant