turbzcoding
turbzcoding

Reputation: 203

Semgrep: Looking for wrong import

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?

Upvotes: 3

Views: 562

Answers (1)

ine
ine

Reputation: 14074

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

Upvotes: 7

Related Questions