如何配置预先承认的YAML以与诗歌合作?

发布于 2025-02-13 14:29:43 字数 5119 浏览 0 评论 0原文

我在python 3.10.4在这里,并在我的诗歌1.1.13项目中使用.pre-commit-config.yaml文件,该项目使用pyproject.toml

这是我的.pre-commit-config.yaml看起来像

# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
# https://stackoverflow.com/questions/64935598/git-pre-commit-get-all-python-files
default_language_version:
  python: python3.10

default_stages: [commit, push]

repos:
  - repo: local
    hooks:
      # https://github.com/pre-commit/pre-commit-hooks#check-ast
      - id: check-ast
        description: Simply checks whether the files parse as valid python.
        entry: check-ast
        name: Check python syntax
        language: system
        types: [python]

      # https://github.com/pre-commit/pre-commit-hooks#check-added-large-files
      - id: check-added-large-files
        description: prevents giant files from being committed.
        entry: check-added-large-files
        name: Check added large files
        language: system

      # https://github.com/pre-commit/pre-commit-hooks#check-json
      - id: check-json
        description: Checks json files for parseable syntax.
        entry: check-json
        name: Check JSON
        language: system
        types: [json]

      # https://github.com/pre-commit/pre-commit-hooks#check-toml
      - id: check-toml
        description: Checks toml files for parseable syntax.
        entry: check-toml
        name: Check TOML
        language: system
        types: [toml]

      # https://github.com/pre-commit/pre-commit-hooks#check-yaml
      - id: check-yaml
        description: Checks yaml files for parseable syntax.
        entry: check-yaml
        name: Check YAML
        language: system
        types: [yaml]

      # https://github.com/pre-commit/pre-commit-hooks#end-of-file-fixer
      - id: end-of-file-fixer
        description: Ensures that a file is either empty, or ends with one newline.
        entry: end-of-file-fixer
        name: End of file fixer
        language: system

      # https://github.com/pre-commit/pre-commit-hooks#trailing-whitespace
      - id: trailing-whitespace
        description: Trims trailing whitespace.
        entry: trailing-whitespace-fixer
        name: Trailing whitespace
        language: system

  - repo: local
    hooks:
      - args: ["--verbose"]
        id: flake8
        description: Command-line utility for enforcing style consistency across Python projects.
        entry: flake8
        name: flake8
        language: python
        require_serial: true
        types: [python]
      - args: ["--verbose"]
        id: black
        description: The uncompromising Python code formatter
        entry: black
        name: black
        language: python
        require_serial: true
        types: [python]
      - args: ["--verbose"]
        id: isort
        name: isort
        entry: isort
        require_serial: true
        language: python
        types: [python]
      - args: ["--verbose"]
        exclude: tests/.*$
        id: bandit
        name: bandit
        entry: bandit
        language: python
        types: [python]
      - args: ["--verbose"]
        id: mypy
        name: mypy
        entry: mypy
        language: python
        require_serial: true
        types: [python]

我在我运行一个时 看起来像是什么git commit -m“测试”它失败了以下错误,

Check python syntax......................................................Failed
- hook id: check-ast
- exit code: 1

Executable `check-ast` not found

Check added large files..................................................Failed
- hook id: check-added-large-files
- exit code: 1

Executable `check-added-large-files` not found

Check JSON...........................................(no files to check)Skipped
Check TOML...............................................................Failed
- hook id: check-toml
- exit code: 1

Executable `check-toml` not found

Check YAML...........................................(no files to check)Skipped
End of file fixer........................................................Failed
- hook id: end-of-file-fixer
- exit code: 1

Executable `end-of-file-fixer` not found

Trailing whitespace......................................................Failed
- hook id: trailing-whitespace
- exit code: 1

Executable `trailing-whitespace-fixer` not found

flake8...................................................................Failed
- hook id: flake8
- exit code: 1

Executable `flake8` not found

black....................................................................Failed
- hook id: black
- exit code: 1

Executable `black` not found

isort....................................................................Failed
- hook id: isort
- exit code: 1

Executable `isort` not found

bandit...................................................................Failed
- hook id: bandit
- exit code: 1

Executable `bandit` not found

mypy.....................................................................Failed
- hook id: mypy
- exit code: 1

Executable `mypy` not found

我如何使预订使用诗歌创建的虚拟环境并在本地使用其所有依赖关系?

I am on Python 3.10.4 here and using a .pre-commit-config.yaml file in my Poetry 1.1.13 project that uses pyproject.toml

Here is what my .pre-commit-config.yaml looks like

# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
# https://stackoverflow.com/questions/64935598/git-pre-commit-get-all-python-files
default_language_version:
  python: python3.10

default_stages: [commit, push]

repos:
  - repo: local
    hooks:
      # https://github.com/pre-commit/pre-commit-hooks#check-ast
      - id: check-ast
        description: Simply checks whether the files parse as valid python.
        entry: check-ast
        name: Check python syntax
        language: system
        types: [python]

      # https://github.com/pre-commit/pre-commit-hooks#check-added-large-files
      - id: check-added-large-files
        description: prevents giant files from being committed.
        entry: check-added-large-files
        name: Check added large files
        language: system

      # https://github.com/pre-commit/pre-commit-hooks#check-json
      - id: check-json
        description: Checks json files for parseable syntax.
        entry: check-json
        name: Check JSON
        language: system
        types: [json]

      # https://github.com/pre-commit/pre-commit-hooks#check-toml
      - id: check-toml
        description: Checks toml files for parseable syntax.
        entry: check-toml
        name: Check TOML
        language: system
        types: [toml]

      # https://github.com/pre-commit/pre-commit-hooks#check-yaml
      - id: check-yaml
        description: Checks yaml files for parseable syntax.
        entry: check-yaml
        name: Check YAML
        language: system
        types: [yaml]

      # https://github.com/pre-commit/pre-commit-hooks#end-of-file-fixer
      - id: end-of-file-fixer
        description: Ensures that a file is either empty, or ends with one newline.
        entry: end-of-file-fixer
        name: End of file fixer
        language: system

      # https://github.com/pre-commit/pre-commit-hooks#trailing-whitespace
      - id: trailing-whitespace
        description: Trims trailing whitespace.
        entry: trailing-whitespace-fixer
        name: Trailing whitespace
        language: system

  - repo: local
    hooks:
      - args: ["--verbose"]
        id: flake8
        description: Command-line utility for enforcing style consistency across Python projects.
        entry: flake8
        name: flake8
        language: python
        require_serial: true
        types: [python]
      - args: ["--verbose"]
        id: black
        description: The uncompromising Python code formatter
        entry: black
        name: black
        language: python
        require_serial: true
        types: [python]
      - args: ["--verbose"]
        id: isort
        name: isort
        entry: isort
        require_serial: true
        language: python
        types: [python]
      - args: ["--verbose"]
        exclude: tests/.*$
        id: bandit
        name: bandit
        entry: bandit
        language: python
        types: [python]
      - args: ["--verbose"]
        id: mypy
        name: mypy
        entry: mypy
        language: python
        require_serial: true
        types: [python]

When I run a git commit -m "test" it fails with the following error

Check python syntax......................................................Failed
- hook id: check-ast
- exit code: 1

Executable `check-ast` not found

Check added large files..................................................Failed
- hook id: check-added-large-files
- exit code: 1

Executable `check-added-large-files` not found

Check JSON...........................................(no files to check)Skipped
Check TOML...............................................................Failed
- hook id: check-toml
- exit code: 1

Executable `check-toml` not found

Check YAML...........................................(no files to check)Skipped
End of file fixer........................................................Failed
- hook id: end-of-file-fixer
- exit code: 1

Executable `end-of-file-fixer` not found

Trailing whitespace......................................................Failed
- hook id: trailing-whitespace
- exit code: 1

Executable `trailing-whitespace-fixer` not found

flake8...................................................................Failed
- hook id: flake8
- exit code: 1

Executable `flake8` not found

black....................................................................Failed
- hook id: black
- exit code: 1

Executable `black` not found

isort....................................................................Failed
- hook id: isort
- exit code: 1

Executable `isort` not found

bandit...................................................................Failed
- hook id: bandit
- exit code: 1

Executable `bandit` not found

mypy.....................................................................Failed
- hook id: mypy
- exit code: 1

Executable `mypy` not found

How do I make pre-commit use the virtual environment created by poetry and use all its dependencies locally?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

断舍离 2025-02-20 14:29:43

不建议和不支持您的寻找方式。预先支配的重点是它安装和管理您的工具 - 这样您就不必让贡献者担心在所需版本上安装每个工具。

也就是说,您 can 在使用语言时逃脱了受支持的路径:system - 但随后您的贡献者可以正确设置事物,并在适当的版本以及适当的Virtualenv激活并在Virtualenv中安装了适当的版本(这是错误的,通常是不良的体验)。

您遇到问题的原因是因为您犯了错误的:)


免责声明:我创建了预先竞争

using pre-commit in the way you're looking for is not recommend and not supported. the point of pre-commit is it installs and manages your tools -- that way you don't have to have your contributors worry about installing each and every tool at the required version.

that said, you can escape the supported path as you're doing with language: system -- but then it is on your contributors to have things set up properly and at the proper versions and with the proper virtualenv activated and with the proper versions installed in the virtualenv (which is error prone and generally a bad experience).

the reason you're encountering problems is because you're holding it wrong :)


disclaimer: I created pre-commit

生死何惧 2025-02-20 14:29:43

首先,配置诗歌在项目内使用虚拟环境。之后,创建一个虚拟环境 Python版本(请参阅pyproject.toml)。安装依赖项,然后激活虚拟环境。最后,预先使用准备使用。

[tool.poetry]
name = "poetry-precommit"
version = "0.1.0"
description = ""
authors = ["Author Name <[email protected]>"]
readme = "README.md"
packages = [{include = "poetry_precommit"}]

[tool.poetry.dependencies]
python = "^3.9"
pre-commit = "3.2.2"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

在此示例中,使用Python&gt; = 3.9.0或用于虚拟环境的4.0.0版本。 使用 config 命令。确保通过诗歌config -list命令正确设置它。

poetry config virtualenvs.in-project true

使用诗歌安装安装依赖项。确保其在虚拟环境下安装依赖关系。激活环境后,准备使用预先使用。

source <venv>/bin/activate
pre-commit run --all-files

为了在每次提交之前自动使用预签名。

pre-commit install

First, configure poetry to use virtual environments inside the project. After that, create a virtual environment with the appropriate Python version (see in pyproject.toml). Install dependencies then activate the virtual environment. Finally, pre-commit is ready to use.

[tool.poetry]
name = "poetry-precommit"
version = "0.1.0"
description = ""
authors = ["Author Name <[email protected]>"]
readme = "README.md"
packages = [{include = "poetry_precommit"}]

[tool.poetry.dependencies]
python = "^3.9"
pre-commit = "3.2.2"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

In this example use Python >=3.9.0 or <4.0.0 version for the virtual environment. Configure poetry using the config command. Ensure that it is set correctly by poetry config --list command.

poetry config virtualenvs.in-project true

Install dependencies using poetry install. Make sure that it installed dependencies under the virtual environment. After activating the environment, pre-commit is ready to used.

source <venv>/bin/activate
pre-commit run --all-files

In order to use pre-commit automatically before every commit.

pre-commit install
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文