0

When I run flake8 . command I want to raise flake8 error if "print_queries" string is present anywhere in project files checked into VCS (and not excluded).

Here's my flake8 configuration:

[flake8]
exclude = .venv, migrations, scaffoldapp
max-line-length = 130

per-file-ignores =
    # imported but not used
    */__init__.py: F401,F403
    */*urls.py: E122,E501
    */*settings.py: E501
    */*messages.py: E501
    */apps.py: F401
    */*employee_constant.py: W605

How can I deny checking print_queries string into VCS? I'm also using pre-commit.

STerliakov
  • 4,983
  • 3
  • 15
  • 37
  • Not sure that `flake8` can handle it. Are you using pre-commit? If yes, then [`pygrep` hook](https://pre-commit.com/#pygrep) should be the easiest option (see also [this question](https://stackoverflow.com/questions/64339118/pygrep-pre-commit-hook-to-check-string-is-present)). – STerliakov Dec 06 '22 at 11:33
  • ... and why do you ignore W605 somewhere? It is a real problem that will become a SyntaxError soon and emits DeprecationWarning now... – STerliakov Dec 06 '22 at 11:34
  • @SUTerliakov Actually i am working on a large project and I am using 1 decorator and which is "print_queries" to only check timing of the queries run so its only for development purpose only so if i am using this decorator in any API to check the number of queries i run and forgot to remove then flake8 should raise the error. – Abbas Shaikh Dec 06 '22 at 11:46
  • @SUTerliakov And yes i am using pre-commit too – Abbas Shaikh Dec 06 '22 at 11:46
  • also i have to add "@print_queries" decorator in pre-commit @SUTerliakov – Abbas Shaikh Dec 06 '22 at 11:57

1 Answers1

1

You can add the following pre-commit hook:

- repo: local
  hooks:
    - id: love_statement
      name: Check that `print_queries` is not used
      types: [python]
      entry: '@print_queries'
      language: pygrep

It will fail iff the string @print_queries is present anywhere in staged files.

STerliakov
  • 4,983
  • 3
  • 15
  • 37
  • I have tried this but its skipping and committing the code Check that @print_queries is in source code..........(no files to check) Skipped – Abbas Shaikh Dec 06 '22 at 12:15
  • "No files to check" means that you are not committing any python files now. Try `pre-commit run --all-files` to see it in action or `git add` some `.py` files with this string before committing. – STerliakov Dec 06 '22 at 12:21
  • while this does work, it doesn't actually answer the question as written – anthony sottile Dec 06 '22 at 20:21
  • @anthonysottile AFAIC you are the author of pre-commit (and pyupgrade, right?), so would love to hear any other solutions! I just think that there's no `flake8` solution for that and thus propose a viable alternative, since OP confirmed using pre-commit. – STerliakov Dec 06 '22 at 20:49
  • the actual answer to the question as written is to author a `flake8` plugin to find the pattern and forbid it – anthony sottile Dec 06 '22 at 21:27
  • @SUTerliakov Can we check multiple strings in same hook? Like entry: '@print_queries' or 'breakpoint()' – Abbas Shaikh Dec 19 '22 at 11:43