Skip to content
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

Set undefined env variables using CLI arguments #363

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@
* [naquiroz](https://github.com/naquiroz)
* [john-sandall](https://github.com/john-sandall)
* [dineshtrivedi](https://github.com/dineshtrivedi)
* [TheAfroOfDoom](https://github.com/TheAfroOfDoom)
31 changes: 31 additions & 0 deletions pylint_django/checkers/foreign_key_strings.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from __future__ import absolute_import

import os

import astroid
from configurations import importer
from pylint.checkers import BaseChecker
from pylint.checkers.utils import check_messages
from pylint.interfaces import IAstroidChecker
Expand Down Expand Up @@ -41,6 +44,15 @@ class ForeignKeyStringsChecker(BaseChecker):
"help": "A module containing Django settings to be used while linting.",
},
),
(
"django-configuration",
{
"default": None,
"type": "string",
"metavar": "<django configuration>",
"help": "The configuration for Django to use while linting.",
},
),
)

msgs = {
Expand Down Expand Up @@ -89,6 +101,25 @@ def open(self):
try:
import django # pylint: disable=import-outside-toplevel

if os.environ.get("DJANGO_SETTINGS_MODULE") is None or os.environ.get("DJANGO_CONFIGURATION") is None:
try:
os.environ.setdefault(
"DJANGO_SETTINGS_MODULE",
os.environ.get("DJANGO_SETTINGS_MODULE") or self.config.django_settings_module,
)
os.environ.setdefault(
"DJANGO_CONFIGURATION",
os.environ.get("DJANGO_CONFIGURATION") or self.config.django_configuration,
)
except TypeError as ex:
missing_module = ""
if self.config.django_settings_module is None:
missing_module = "DJANGO_SETTINGS_MODULE"
else:
missing_module = "DJANGO_CONFIGURATION"
raise RuntimeError(f"{missing_module} required to initialize Django project settings") from ex
importer.install()

django.setup()
from django.apps import ( # noqa pylint: disable=import-outside-toplevel,unused-import
apps,
Expand Down