Semgrep: Looking for wrong import

438 Views Asked by At

How would I go on about making Semgrep check if my codebase is importing functions from the wrong place? E.g. in my django project:

from gettext import gettext but we should really do from django.utils.translation import gettext

Any ideas on how to achieve this with semgrep?

1

There are 1 best solutions below

0
On

Does this do what you want? Here's a live example link: https://semgrep.dev/s/0n0w

rules:
- id: import-from-wrong-module
  patterns:
    - pattern: |
        from $X import gettext
    - pattern-not: |
        from my.special.module import gettext
  message: Please import gettext from my.special.module
  severity: ERROR