There isn’t a built‐in “inline disable” comment for the pygrep no‑eval hook. In other words, you can’t simply append a comment like # noqa (or similar) to prevent that hook from firing on one line. (This hook, like other pygrep hooks, simply scans for a regex match and ignores inline “skip” comments.)
That said, there are a couple of common workarounds:
Refactor the call so it “dodges” the regex.
Since the hook is (by design) meant to catch generic uses of “.eval(”, one common trick is to hide the .eval string from the simple regex. For example, instead of writing:
python
model.eval()
you can “break up” the string:
python
getattr(model, "eval")()
This still calls the intended method but avoids a literal “.eval(” in your source so that the hook won’t flag it.
Exclude the file (or files) in your pre-commit configuration.
If your use of .eval() is known to be safe (for example, when working with PyTorch models) you might choose to disable the hook on that file altogether. In your .pre-commit-config.yaml you can add an exclude: key to the hook’s configuration to skip files (by regex match) where the Torch model lives.
For example:
yaml
- repo: https://github.com/pre-commit/pygrep-hooks
rev: v1.8.1 # or whichever version you use
hooks:
- id: no-eval
exclude: 'models/torch_model\.py'
This way the hook won’t run on that file.
Disable the hook altogether (if appropriate).
If the no‑eval hook causes too many false positives for your project, another option is to remove it from your pre‑commit configuration entirely. However, be sure you really understand the security implications before doing so.
In summary, pygrep hooks (including no‑eval) do not currently offer a mechanism to disable them on a per‑line basis (for example via an inline comment). The common solutions are either to “hide” the offending pattern using a refactoring (as in option 1) or to adjust the pre‑commit configuration (as in option 2).