How to use a config from pyproject.toml inside pre-commit-config.yaml

239 Views Asked by At

I have a python repository which has a pyproject.toml and a pre-commit-config.yaml

The pyproject.toml looks something like this

[tool.poetry]
name = "projectname"
description = "My project's description"
authors = ["My Name <[email protected]>"]
version = "0.1.0"

Now I want to use the name field from pyproject.toml instead of in the following custom hook in my pre-commit config

  - repo: local
    hooks:
      - id: check-for-utility-imports
        name: check-for-utility-imports
        entry: ^from <PROJECT-NAME-COMES-HERE>.utility.* import
        files: ^<PROJECT-NAME-COMES-HERE>/contracts/.*.py$
        language: pygrep
        types: [ python ]

Is there any way to do this?

1

There are 1 best solutions below

1
anthony sottile On

the pre-commit configuration itself is entirely static, there is no substitution. I suspect you're trying to over-DRY and you'd be better to just use the name inline.

that said, you can write a custom script which does what you want (reads the toml file, searches for the particular string in your files)

note you have a microbug in your regex as well, you should be escaping the . otherwise it false-positives on projectname_utilities


disclaimer: I wrote pre-commit