首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Pylint: AttributeError:‘下标’对象没有属性'name‘

Pylint: AttributeError:‘下标’对象没有属性'name‘
EN

Stack Overflow用户
提问于 2020-08-06 22:21:45
回答 1查看 388关注 0票数 0

有一段时间我在使用pylint_django运行Pylint时遇到了一个问题。我包括错误跟踪和我的环境的细节。

我认为我已经找到了一个解决方案,并提交了一个推送请求,但我在这里发表这个问题,以防其他人面临同样的问题。

这个问题发表在GitHub:https://github.com/PyCQA/pylint-django/issues/284上。

回溯:

代码语言:javascript
复制
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'

在彻底清除了文件forms.py,给出了错误后,我想我已经成功地隔离了有问题的行。仍然提供错误的文件现在只包含以下内容:

代码语言:javascript
复制
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

当然,我已经过度简化了表单类。这个错误似乎是由widgets["field"] = forms.CheckboxSelectMultiple引起的,它在实际代码中是在循环创建小部件dict之后向特定字段添加不同的小部件。

如果我使用的是widgets = {"field": forms.CheckboxSelectMultiple},那么一切都很好。

关于我的配置的一些细节:

19.2.3

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

pip冻结的结果:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2020-08-06 22:21:45

虽然pylint_django没有提供修复,但是您可以通过避免像widget["field"] = ...这样的赋值来避免错误。

我想类似的代码(如fields["field"] = "another_field_to_handle" )会产生类似的问题。

解释:

在深入研究了pylint_django的代码之后,我发现了以下内容:

meta.get_children()返回Meta类的所有子类,该类在示例代码中包含一个字典赋值。

期望的子级为Assign类型,其属性目标中的第一项为AssignName类型,例如AssignName.widgets(name='widgets')

widget["field"] = forms.SomeWidget这样的指令将导致一个包含Subscript(ctx=<Context.Store: 2>, value=<Name.widgets ...>, slice=<Index ...>)Assign子程序。这当然是出乎意料的。

我已经提交了一个push请求,其中包含了一个建议的修复程序:https://github.com/PyCQA/pylint-django/pull/285

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63292565

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档