Skip to content

Commit

Permalink
Hardcode Oscar and Django contrib models
Browse files Browse the repository at this point in the history
  • Loading branch information
rik authored and Thomas DA COSTA committed Apr 26, 2022
1 parent 0beda64 commit 9da5fce
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions pylint_django/transforms/foreignkey.py
@@ -1,8 +1,7 @@
from itertools import chain

from astroid import MANAGER, InferenceError, UseInferenceDefault, inference_tip, nodes
from astroid import MANAGER, InferenceError, UseInferenceDefault, inference_tip, nodes, exceptions
from astroid.nodes import Attribute, ClassDef

from pylint_django.utils import node_is_subclass


Expand Down Expand Up @@ -116,8 +115,19 @@ def infer_key_classes(node, context=None):
) from exep

# ensure that module is loaded in astroid_cache, for cases when models is a package
if module_name not in MANAGER.astroid_cache:
MANAGER.ast_from_module_name(module_name)
for prefix in ['', 'oscar.apps.', 'django.contrib.']:
try:
if prefix + module_name not in MANAGER.astroid_cache:
MANAGER.ast_from_module_name(prefix + module_name)
except exceptions.AstroidImportError:
continue
else:
# We've imported without a crash!
module_name = prefix + module_name
break
else:
# We've exhausted the prefixes
raise ImportError(f'Could not import {module_name}')

# create list from dict_values, because it may be modified in a loop
for module in list(MANAGER.astroid_cache.values()):
Expand Down

0 comments on commit 9da5fce

Please sign in to comment.