gpt4 book ai didi

python - GitLab CI Python 黑色格式化程序说 : would reformat, 而运行黑色不会重新格式化

转载 作者:行者123 更新时间:2023-12-05 02:30:57 25 4
gpt4 key购买 nike

当我在 this commit 上运行 GitLab CI 时

用这个gitlab-ci.yml:

stages:
- format
- test

black_formatting:
image: python:3.6
stage: format

before_script:
# Perform an update to make sure the system is up to date.
- sudo apt-get update --fix-missing
# Download miniconda.
- wget -q https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda.sh; bash miniconda.sh -b -f -p $HOME/miniconda;
# Ensure the (mini) conda environment can be activated.
- export PATH="$HOME/miniconda/bin:$PATH"
# (Re)create the environment.yml file for the repository.
- conda env create -q -f environment.yml -n checkstyle-for-bash --force
# Activate the environment of the repository.
- source activate checkstyle-for-bash

script:
# Verify the Python code is black formatting compliant.
- black . --check --exclude '\.venv/|\.local/|\.cache/|\.git/'
# Verify the Python code is flake8 formatting compliant.
- flake8 .
allow_failure: false


test:pytest:36:
stage: test
image: python:3.6
script:
# Ensure the (mini) conda environment can be activated.
- export PATH="$HOME/miniconda/bin:$PATH"
# Activate the environment of the repository.
- source activate checkstyle-for-bash
# Run the python tests.
- python -m pytest

它输出:

Running with gitlab-runner 14.8.0 (565b6c0b)
on trucolrunner DS42qHSq
Preparing the "shell" executor
00:00
Using Shell executor...
Preparing environment
00:00
Running on pcname...
Getting source from Git repository
00:02
Fetching changes with git depth set to 20...
Reinitialized existing Git repository in /home/gitlab-runner/builds/DS42qHSq/0/root/checkstyle-for-bash/.git/
Checking out 001577c3 as main...
Removing miniconda.sh
Skipping Git submodules setup
Executing "step_script" stage of the job script
02:55
$ sudo apt-get update --fix-missing
Hit:1 http://nl.archive.ubuntu.com/ubuntu impish InRelease
Get:2 http://security.ubuntu.com/ubuntu impish-security InRelease [110 kB]
Get:3 http://nl.archive.ubuntu.com/ubuntu impish-updates InRelease [115 kB]
Hit:4 https://repo.nordvpn.com/deb/nordvpn/debian stable InRelease
Get:5 http://nl.archive.ubuntu.com/ubuntu impish-backports InRelease [101 kB]
Hit:6 https://brave-browser-apt-release.s3.brave.com stable InRelease
Get:7 http://security.ubuntu.com/ubuntu impish-security/main amd64 DEP-11 Metadata [20,3 kB]
Get:8 http://security.ubuntu.com/ubuntu impish-security/universe amd64 DEP-11 Metadata [3.624 B]
Get:9 http://nl.archive.ubuntu.com/ubuntu impish-updates/main amd64 DEP-11 Metadata [25,8 kB]
Get:10 http://nl.archive.ubuntu.com/ubuntu impish-updates/universe amd64 DEP-11 Metadata [35,4 kB]
Get:11 http://nl.archive.ubuntu.com/ubuntu impish-updates/multiverse amd64 DEP-11 Metadata [940 B]
Get:12 http://nl.archive.ubuntu.com/ubuntu impish-backports/universe amd64 DEP-11 Metadata [16,4 kB]
Fetched 428 kB in 2s (235 kB/s)
Reading package lists...
$ wget -q https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda.sh; bash miniconda.sh -b -f -p $HOME/miniconda;
PREFIX=/home/gitlab-runner/miniconda
Unpacking payload ...
Collecting package metadata (current_repodata.json): ...working... done
Solving environment: ...working... done
# All requested packages already installed.
installation finished.
$ export PATH="$HOME/miniconda/bin:$PATH"
$ conda env create -q -f environment.yml -n checkstyle-for-bash --force
Collecting package metadata (repodata.json): ...working... done
Solving environment: ...working... done
Preparing transaction: ...working... done
Verifying transaction: ...working... done
Executing transaction: ...working... done
Installing pip dependencies: ...working... done
$ source activate checkstyle-for-bash
$ black . --check --exclude '\.venv/|\.local/|\.cache/|\.git/'
would reformat src/arg_parser.py
would reformat src/helper_text_parsing.py
Oh no! 💥 💔 💥
2 files would be reformatted, 10 files would be left unchanged.
ERROR: Job failed: exit status 1

但是,如果我在 GitHub 存储库上运行 black src/**,black 返回:

~/git/checkstyle-for-bash$ git pull
Already up to date.
(base) some@name:~/git/checkstyle-for-bash$ black src/**
All done! ✨ 🍰 ✨
8 files left unchanged.

以防万一我没有克隆正确的存储库,我还手动将 src/arg_parser.py 文件的内容从 GitLab 复制粘贴到 ~/git/checkstyle-for -bash/src/arg_parser.py 并再次运行 black。然而,输出是一样的,它没有改变任何东西。

为了完整起见,这是 src/arg_parser.py 文件的内容:

# This is the main code of this project nr, and it manages running the code and
# outputting the results to LaTex.
import argparse


def parse_cli_args():
# Instantiate the parser
parser = argparse.ArgumentParser(description="Optional app description")

# Include argument parsing for default code.
# Allow user to load a graph from file.
parser.add_argument(
"--ggl",
dest="google_style_guide",
action="store_true",
help=(
"boolean flag, determines whether the Google Style Guide for "
"Bash rules are followed."
),
)

# Allow user to specify an infile.
parser.add_argument("infile", nargs="?", type=argparse.FileType("r"))

# Specify default argument values for the parser.
parser.set_defaults(google_style_guide=True,)

# Load the arguments that are given.
args = parser.parse_args()
return args

问题

是什么导致 GitLab CI 说文件将被黑色重新格式化(即使文件在运行黑色时没有重新格式化(在同一设备上(在不同的 conda 环境中)))?

设置

我在测试 conda black 命令的同一台设备上运行我自己的 GitLab CI。 GitLab CI 复制存储库的 GitHub 提交,一次一个,在其上运行其 CI,然后将结果报告回 GitHub。

我目前无法在 clearnet 上公开我的 GitLab 服务器,因为我位于我目前无法控制的网关后面。

疑问

我相当确定这是我这边的一个“愚蠢”错误,但是我还没有弄清楚它是什么。特别是因为我手动复制粘贴了 src/arg_parser.py 文件的 GitLab 文件内容两次,并运行了两次 black 以验证 black 确实不会更改“那个”文件。另外,为了确保它不是尾随的换行符或其他任何东西,我使用了鼠标的复制按钮,而不是手动选择: enter image description here

此外,该文件符合 flake8 标准。我目前的猜测是,不知何故,CI 没有在该存储库的最新提交上运行。但是,要验证我是否单击了失败提交的 GitLab,它确实重定向到 GitLab 中的“05e85fd54f93ccfc427023b21f9cdb0c0cd6db2e”提交(复制): enter image description here正是从这次提交中,我复制粘贴了 src/arg_parser.py 文件两次。

另一种猜测是 gitlab-ci.yml 加载了一个 miniconda 环境,而我的本地版本 black 使用了一个完整的 conda 环境。也许他们有不同的换行符,这会导致格式差异。 (尽管我怀疑情况是否如此)。

问题

我再次运行 CI,同时包含 black 。 gitlab-ci.yml脚本中的--diff命令,区别在于:

''' Return

和:

'''Return

如随附输出中所示:

$ black . --diff --exclude '\.venv/|\.local/|\.cache/|\.git/'
--- src/arg_parser.py 2022-04-03 10:13:10.751289 +0000
+++ src/arg_parser.py 2022-04-03 11:11:26.297995 +0000
@@ -24,10 +24,12 @@

# Allow user to specify an infile.
parser.add_argument("infile", nargs="?", type=argparse.FileType("r"))

# Specify default argument values for the parser.
- parser.set_defaults(google_style_guide=True,)
+ parser.set_defaults(
+ google_style_guide=True,
+ )

# Load the arguments that are given.
args = parser.parse_args()
return args
would reformat src/arg_parser.py
--- src/helper_text_parsing.py 2022-04-02 19:35:45.142619 +0000
+++ src/helper_text_parsing.py 2022-04-03 11:11:26.342908 +0000
@@ -5,11 +5,11 @@
def add_two(x):
return x + 2


def get_function_line_nrs(filecontent, rules):
- """ Returns two lists containing the starting and ending line numbers of
+ """Returns two lists containing the starting and ending line numbers of
the functions respectively.

:param filecontent: The content of the bash file that is being analysed.
:param rules: The Bash formatting rules that are chosen by the user.
"""
would reformat src/helper_text_parsing.py
All done! ✨ 🍰 ✨
2 files would be reformatted, 10 files would be left unchanged.

我不太清楚为什么会发生这种情况,因为我认为 python black 对于给定的有效 python 文件总是会收敛到完全相同的格式。我猜是因为在同一台设备上的miniconda环境和anaconda环境之间使用了两个不同的黑色版本。

为了检验这个假设,我在 gitlab-ci.yml 中包含了一个 black --version 命令。

最佳答案

GitLab CI中的miniconda环境使用的是python black版本:

black, 22.3.0 (compiled: yes)

而本地环境使用的是python black版本:

black, version 19.10b0

更新本地 black 版本,根据最新的 python black 版本推送格式化代码,并在该 GitHub 提交上运行 GitLab CI 导致 GitLab CI 成功跑。

关于python - GitLab CI Python 黑色格式化程序说 : would reformat, 而运行黑色不会重新格式化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71724842/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com