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

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

Open
liangqingzhao opened this issue Dec 6, 2022 · 0 comments

Comments

@liangqingzhao
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant