3

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?

John Vandenberg
  • 474
  • 6
  • 16
turbzcoding
  • 173
  • 1
  • 6

1 Answers1

6

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
Underyx
  • 1,599
  • 1
  • 17
  • 23
ine
  • 14,014
  • 8
  • 55
  • 80