pylint-dev / pylint-django

Pylint plugin for improving code analysis for when using Django
Other
593 stars 117 forks source link

pylint-django to check models raise error if it have foreignkey like this #388

Open liangqingzhao opened 1 year ago

liangqingzhao commented 1 year ago

error:

Traceback (most recent call last):
  File "/opt/hostedtoolcache/Python/3.8.14/x64/lib/python3.8/site-packages/astroid/inference_tip.py", line 38, in _inference_tip_cached
    result = _cache[func, node]
KeyError: (<function infer_key_classes at 0x7fd7a30493a0>, <Call l.18 at 0x7fd7a20c89a0>)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/opt/hostedtoolcache/Python/3.8.14/x64/lib/python3.8/site-packages/pylint/checkers/utils.py", line 1344, in safe_infer
    value = next(infer_gen)
  File "/opt/hostedtoolcache/Python/3.8.14/x64/lib/python3.8/site-packages/astroid/nodes/node_ng.py", line [16](https://github.com/liangqingzhao/my_local_test/actions/runs/3628194094/jobs/6118923554#step:7:17)9, in infer
    yield from self._infer(context=context, **kwargs)
  File "/opt/hostedtoolcache/Python/3.8.14/x64/lib/python3.8/site-packages/astroid/decorators.py", line 140, in raise_if_nothing_inferred
    yield next(generator)
  File "/opt/hostedtoolcache/Python/3.8.14/x64/lib/python3.8/site-packages/astroid/decorators.py", line 109, in wrapped
    for res in _func(node, context, **kwargs):
  File "/opt/hostedtoolcache/Python/3.8.14/x64/lib/python3.8/site-packages/astroid/bases.py", line 165, in _infer_stmts
    for inf in stmt.infer(context=context):  # type: ignore[union-attr]
  File "/opt/hostedtoolcache/Python/3.8.14/x64/lib/python3.8/site-packages/astroid/nodes/node_ng.py", line 159, in infer
    results = list(self._explicit_inference(self, context, **kwargs))
  File "/opt/hostedtoolcache/Python/3.8.14/x64/lib/python3.8/site-packages/astroid/inference_tip.py", line 45, in _inference_tip_cached
    result = _cache[func, node] = list(func(*args, **kwargs))
  File "/opt/hostedtoolcache/Python/3.8.14/x64/lib/python3.8/site-packages/pylint_django/transforms/foreignkey.py", line 1[20](https://github.com/liangqingzhao/my_local_test/actions/runs/3628194094/jobs/6118923554#step:7:21), in infer_key_classes
    MANAGER.ast_from_module_name(module_name)
  File "/opt/hostedtoolcache/Python/3.8.14/x64/lib/python3.8/site-packages/astroid/manager.py", line [21](https://github.com/liangqingzhao/my_local_test/actions/runs/3628194094/jobs/6118923554#step:7:22)6, in ast_from_module_name
    raise e
  File "/opt/hostedtoolcache/Python/3.8.14/x64/lib/python3.8/site-packages/astroid/manager.py", line 161, in ast_from_module_name
    found_spec = self.file_from_module_name(modname, context_file)
  File "/opt/hostedtoolcache/Python/3.8.14/x64/lib/python3.8/site-packages/astroid/manager.py", line [26](https://github.com/liangqingzhao/my_local_test/actions/runs/3628194094/jobs/6118923554#step:7:27)7, in file_from_module_name
    raise value.with_traceback(None)  # pylint: disable=no-member
astroid.exceptions.AstroidImportError: Failed to import module main.models with error:
No module named main.models.

this is my model

class EngravingPromotionOrder(models.Model):
    engraving_promotion_number = models.ForeignKey('main.EngravingPromotion', blank=True, null=True, related_name='engraving_promotion', on_delete=models.CASCADE)
    order_number = models.OneToOneField('order.Order', blank=True, null=True, on_delete=models.CASCADE)

if i change it to this way,the error will dissappear

from xxx.apps.main.models import EngravingPromotion
class EngravingPromotionOrder(models.Model):
    engraving_promotion_number = models.ForeignKey(EngravingPromotion, blank=True, null=True, related_name='engraving_promotion', on_delete=models.CASCADE)
    order_number = models.OneToOneField('order.Order', blank=True, null=True, on_delete=models.CASCADE)

do we must to use the second way,if not it will raise error i find a similar issue #2995 also use this way to slove the error