Skip to content

Pylint crashes if Django is not configured #277

Closed
@ljodal

Description

@ljodal

Hi there, starting with version 2.2.0 I'm seeing a crash if django is not properly configured. I'm assuming this is originating from #243. This happens even if the string foreign key is resolvable without using the Django runtime.

While I kinda understand that you'd expect to have a Django configured while running pylint, it's not always the case and in some cases it might be hard (like in an editor).

Traceback (most recent call last):
  File "<snip>/.venv/lib/python3.7/site-packages/astroid/__init__.py", line 93, in _inference_tip_cached
    return iter(_cache[func, node])
KeyError: (<function infer_key_classes at 0x1113d5b00>, <Call l.162 at 0x11b5f3650>)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<snip>/.venv/bin/pylint", line 8, in <module>
    sys.exit(run_pylint())
  File "<snip>/.venv/lib/python3.7/site-packages/pylint/__init__.py", line 22, in run_pylint
    PylintRun(sys.argv[1:])
  File "<snip>/.venv/lib/python3.7/site-packages/pylint/lint/run.py", line 344, in __init__
    linter.check(args)
  File "<snip>/.venv/lib/python3.7/site-packages/pylint/lint/pylinter.py", line 871, in check
    self.get_ast, self._iterate_file_descrs(files_or_modules)
  File "<snip>/.venv/lib/python3.7/site-packages/pylint/lint/pylinter.py", line 904, in _check_files
    self._check_file(get_ast, check_astroid_module, name, filepath, modname)
  File "<snip>/.venv/lib/python3.7/site-packages/pylint/lint/pylinter.py", line 930, in _check_file
    check_astroid_module(ast_node)
  File "<snip>/.venv/lib/python3.7/site-packages/pylint/lint/pylinter.py", line 1063, in check_astroid_module
    ast_node, walker, rawcheckers, tokencheckers
  File "<snip>/.venv/lib/python3.7/site-packages/pylint/lint/pylinter.py", line 1107, in _check_astroid_module
    walker.walk(ast_node)
  File "<snip>/.venv/lib/python3.7/site-packages/pylint/utils/ast_walker.py", line 75, in walk
    self.walk(child)
  File "<snip>/.venv/lib/python3.7/site-packages/pylint/utils/ast_walker.py", line 75, in walk
    self.walk(child)
  File "<snip>/.venv/lib/python3.7/site-packages/pylint/utils/ast_walker.py", line 75, in walk
    self.walk(child)
  [Previous line repeated 7 more times]
  File "<snip>/.venv/lib/python3.7/site-packages/pylint/utils/ast_walker.py", line 72, in walk
    callback(astroid)
  File "<snip>/.venv/lib/python3.7/site-packages/pylint/checkers/base.py", line 781, in visit_call
    self._check_inferred_class_is_abstract(inferred, node)
  File "<snip>/.venv/lib/python3.7/site-packages/pylint/checkers/base.py", line 798, in _check_inferred_class_is_abstract
    abstract_methods = _has_abstract_methods(inferred)
  File "<snip>/.venv/lib/python3.7/site-packages/pylint/checkers/base.py", line 385, in _has_abstract_methods
    return len(utils.unimplemented_abstract_methods(node)) > 0
  File "<snip>/.venv/lib/python3.7/site-packages/pylint/checkers/utils.py", line 819, in unimplemented_abstract_methods
    inferred = safe_infer(obj)
  File "<snip>/.venv/lib/python3.7/site-packages/pylint/checkers/utils.py", line 1119, in safe_infer
    value = next(infer_gen)
  File "<snip>/.venv/lib/python3.7/site-packages/astroid/decorators.py", line 132, in raise_if_nothing_inferred
    yield next(generator)
  File "<snip>/.venv/lib/python3.7/site-packages/astroid/decorators.py", line 96, in wrapped
    res = next(generator)
  File "<snip>/.venv/lib/python3.7/site-packages/astroid/bases.py", line 136, in _infer_stmts
    for inferred in stmt.infer(context=context):
  File "<snip>/.venv/lib/python3.7/site-packages/astroid/node_classes.py", line 357, in infer
    return self._explicit_inference(self, context, **kwargs)
  File "<snip>/.venv/lib/python3.7/site-packages/astroid/__init__.py", line 95, in _inference_tip_cached
    result = func(*args, **kwargs)
  File "<snip>/.venv/lib/python3.7/site-packages/pylint_django/transforms/foreignkey.py", line 103, in infer_key_classes
    module_name = _module_name_from_django_model_resolution(model_name, module_name)
  File "<snip>/.venv/lib/python3.7/site-packages/pylint_django/transforms/foreignkey.py", line 46, in _module_name_from_django_model_resolution
    django.setup()
  File "<snip>/.venv/lib/python3.7/site-packages/django/__init__.py", line 19, in setup
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
  File "<snip>/.venv/lib/python3.7/site-packages/django/conf/__init__.py", line 76, in __getattr__
    self._setup(name)
  File "<snip>/.venv/lib/python3.7/site-packages/django/conf/__init__.py", line 61, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting LOGGING_CONFIG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

pip freeze:

Adyen==3.0.0
aioredis==1.3.1
amqp==2.6.0
apipkg==1.5
appdirs==1.4.4
appnope==0.1.0
asgiref==3.2.10
astroid==2.4.2
async-timeout==3.0.1
attrs==19.3.0
autobahn==20.6.2
autograd==1.3
Automat==20.2.0
backcall==0.2.0
bcrypt==3.1.7
billiard==3.6.3.0
black==19.10b0
bokeh==2.1.1
cached-property==1.5.1
cachetools==4.1.1
celery==4.4.6
certifi==2020.6.20
cffi==1.14.0
channels==2.4.0
channels-redis==2.4.2
chardet==3.0.4
click==7.1.2
constantly==15.1.0
coverage==5.2
croniter==0.3.34
cryptography==2.9.2
cssselect==1.1.0
cssutils==1.0.2
curlify==2.2.1
dacite==1.5.1
daphne==2.5.0
datadog==0.38.0
ddtrace==0.40.0
decorator==4.4.2
defusedxml==0.6.0
dill==0.3.2
Django==3.0.8
django-appconf==1.0.4
django-bootstrap3==14.1.0
django-compressor==2.4
django-countries==6.1.2
django-debug-toolbar==2.2
django-environ==0.4.5
django-form-utils==1.0.3
django-groove==0.4.6
django-helpscout==0.6.3
django-ratelimit==3.0.1
django-redis==4.12.1
django-sendgrid-v5==0.8.1
django-stubs==1.5.0
django-webpack-loader==0.7.0
djangorestframework==3.11.0
dkimpy==1.0.4
dnspython==1.16.0
elasticsearch==6.8.1
execnet==1.7.1
Fabric3==1.14.post1
facebook-business==7.0.3
flake8==3.8.3
flake8-bugbear==20.1.4
freezegun==0.3.15
future==0.18.2
fuzzywuzzy==0.18.0
geographiclib==1.50
geopy==2.0.0
github3.py==1.3.0
google-api-core==1.21.0
google-auth==1.19.2
google-auth-oauthlib==0.4.1
google-cloud-core==1.3.0
google-cloud-pubsub==1.7.0
google-cloud-storage==1.30.0
google-crc32c==0.1.0
google-resumable-media==0.7.0
googleads==24.1.0
googleapis-common-protos==1.52.0
googletrans==2.4.0
grpc-google-iam-v1==0.12.3
grpcio==1.30.0
gunicorn==20.0.4
hiredis==1.1.0
hyperlink==19.0.0
idna==2.10
implicit==0.4.2
importlib-metadata==1.7.0
incremental==17.5.0
intervaltree==3.0.2
ipython==7.16.1
ipython-genutils==0.2.0
isodate==0.6.0
isort==4.3.21
jedi==0.17.1
Jinja2==2.11.2
jwcrypto==0.7
kombu==4.6.11
lazy-object-proxy==1.4.3
libcst==0.3.8
libsass==0.20.0
Lifetimes==0.11.3
logstash-python-formatter==0.1.2
lxml==4.5.2
MarkupSafe==1.1.1
maxminddb==1.5.4
mccabe==0.6.1
mock==4.0.2
more-itertools==8.4.0
msgpack==0.6.2
mypy==0.770
mypy-extensions==0.4.3
natsort==7.0.1
netaddr==0.8.0
numpy==1.18.3
oauthlib==3.1.0
ortools==7.7.7810
packaging==20.4
pandas==0.25.3
paramiko==2.7.1
parso==0.7.0
pathspec==0.8.0
pexpect==4.8.0
phonenumberslite==8.12.6
pickleshare==0.7.5
Pillow==7.2.0
pluggy==0.13.1
premailer==3.7.0
prompt-toolkit==3.0.5
protobuf==3.12.2
psycopg2==2.8.5
ptyprocess==0.6.0
py==1.9.0
pyasn1==0.4.8
pyasn1-modules==0.2.8
pycodestyle==2.6.0
pycountry==20.7.3
pycparser==2.20
pyflakes==2.2.0
Pygments==2.6.1
PyHamcrest==2.0.2
PyJWT==1.7.1
pylint==2.5.3
pylint-django==2.2.0
pylint-plugin-utils==0.6
PyNaCl==1.4.0
pyOpenSSL==19.1.0
pyparsing==2.4.7
pyproj==2.6.1.post1
pysaml2==6.1.0
pytest==5.4.3
pytest-cov==2.10.0
pytest-django==3.9.0
pytest-forked==1.2.0
pytest-mock==3.2.0
pytest-socket==0.3.5
pytest-xdist==1.34.0
python-barcode==0.11.0
python-dateutil==2.8.1
python-http-client==3.2.7
python-Levenshtein==0.12.0
python-memcached==1.59
pytz==2020.1
PyYAML==5.3.1
rcssmin==1.0.6
redis==3.5.3
regex==2020.7.14
requests==2.24.0
requests-mock==1.8.0
requests-oauthlib==1.3.0
requests-toolbelt==0.9.1
responses==0.10.15
rjsmin==1.1.0
rsa==4.6
scipy==1.5.1
sendgrid==6.4.3
sentry-sdk==0.16.1
service-identity==18.1.0
six==1.15.0
sortedcontainers==2.2.2
sqlparse==0.3.1
starkbank-ecdsa==1.0.0
stripe==2.49.0
suds-py3==1.4.1.0
tenacity==6.2.0
text-unidecode==1.3
toml==0.10.1
tornado==6.0.4
tqdm==4.47.0
traitlets==4.3.3
twilio==6.44.1
Twisted==20.3.0
txaio==20.4.1
typed-ast==1.4.1
typing-extensions==3.7.4.2
typing-inspect==0.6.0
ua-parser==0.10.0
uritemplate==3.0.1
urllib3==1.25.9
user-agents==2.1
vine==1.3.0
wcwidth==0.2.5
whitenoise==5.1.0
wrapt==1.11.2
xlrd==1.2.0
XlsxWriter==1.2.9
xmltodict==0.12.0
zeep==3.4.0
zipp==3.1.0
zope.interface==5.1.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions