Skip to content

Commit 22c2b12

Browse files
authored
chore: bring pyupgrade (#908)
- bring tool `pyupgrade`, tests and such - apply latest code style --------- Signed-off-by: Jan Kowalleck <[email protected]>
1 parent 1e7b373 commit 22c2b12

File tree

9 files changed

+55
-15
lines changed

9 files changed

+55
-15
lines changed

.github/workflows/python.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,30 @@ env:
4444
permissions: {}
4545

4646
jobs:
47+
pyupgrade:
48+
name: find Upgradable CodingFeatures
49+
runs-on: ubuntu-latest
50+
timeout-minutes: 10
51+
steps:
52+
- name: Checkout
53+
# see https://github.com/actions/checkout
54+
uses: actions/checkout@v4
55+
- name: Setup Python Environment
56+
# see https://github.com/actions/setup-python
57+
uses: actions/setup-python@v5
58+
with:
59+
python-version: ${{ env.PYTHON_VERSION_DEFAULT }}
60+
architecture: 'x64'
61+
- name: Install poetry
62+
# see https://github.com/marketplace/actions/setup-poetry
63+
uses: Gr1N/setup-poetry@v9
64+
with:
65+
poetry-version: ${{ env.POETRY_VERSION }}
66+
- name: Install dependencies
67+
run: poetry install --no-root
68+
- name: Run tox
69+
run: poetry run tox run -e pyupgrade -s false
70+
4771
coding-standards:
4872
name: Linting & Coding Standards
4973
runs-on: ubuntu-latest

CONTRIBUTING.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@ poetry install
1717

1818
## Code style
1919

20+
THis project loves latest python features.
2021
This project uses [PEP8] Style Guide for Python Code.
21-
This project loves sorted imports.
22+
This project loves sorted imports.
23+
2224
Get it all applied via:
2325

2426
```shell
25-
poetry run isort .
26-
poetry run autopep8 -ir cyclonedx_py/ tests/
27+
poetry run -- tox r -e pyupgrade -- --exit-zero-even-if-changed
28+
poetry run -- tox r -e isort
29+
poetry run -- tox r -e autopep8
2730
```
2831

2932
This project prefers `f'strings'` over `'string'.format()`.

cyclonedx_py/_internal/cli.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,10 @@ def _shorten_purls(self, bom: 'Bom') -> bool:
186186
if component.purl is not None:
187187
purl = component.purl
188188
component.purl = type(purl)(
189-
type=purl.type, # type:ignore[arg-type]
190-
namespace=purl.namespace, # type:ignore[arg-type]
191-
name=purl.name, # type:ignore[arg-type]
192-
version=purl.version # type:ignore[arg-type]
189+
type=purl.type,
190+
namespace=purl.namespace,
191+
name=purl.name,
192+
version=purl.version
193193
# omit qualifiers
194194
# omit subdirectory
195195
)

cyclonedx_py/_internal/pipenv.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def __call__(self, *, # type:ignore[override]
124124

125125
lock_file = join(project_directory, 'Pipfile.lock')
126126
try:
127-
lock = open(lock_file, 'rt', encoding='utf8', errors='replace')
127+
lock = open(lock_file, encoding='utf8', errors='replace')
128128
except OSError as err:
129129
raise ValueError(f'Could not open lock file: {lock_file}') from err
130130
with lock:

cyclonedx_py/_internal/poetry.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,11 @@ def __call__(self, *, # type:ignore[override]
171171
pyproject_file = join(project_directory, 'pyproject.toml')
172172
lock_file = join(project_directory, 'poetry.lock')
173173
try:
174-
pyproject = open(pyproject_file, 'rt', encoding='utf8', errors='replace')
174+
pyproject = open(pyproject_file, encoding='utf8', errors='replace')
175175
except OSError as err:
176176
raise ValueError(f'Could not open pyproject file: {pyproject_file}') from err
177177
try:
178-
lock = open(lock_file, 'rt', encoding='utf8', errors='replace')
178+
lock = open(lock_file, encoding='utf8', errors='replace')
179179
except OSError as err:
180180
pyproject.close()
181181
raise ValueError(f'Could not open lock file: {lock_file}') from err

cyclonedx_py/_internal/utils/pyproject.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def pyproject2component(data: dict[str, Any], *,
4343

4444
def pyproject_load(pyproject_file: str) -> dict[str, Any]:
4545
try:
46-
pyproject_fh = open(pyproject_file, 'rt', encoding='utf8', errors='replace')
46+
pyproject_fh = open(pyproject_file, encoding='utf8', errors='replace')
4747
except OSError as err:
4848
raise ValueError(f'Could not open pyproject file: {pyproject_file}') from err
4949
with pyproject_fh:

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ mypy = "1.16.0"
9494
bandit = "1.8.3"
9595
tomli = { version = "^2.0.1", python = "<3.11" }
9696
tox = "4.26.0"
97+
pyupgrade = "3.20.0"
9798

9899
# min version required to be able to install some dependencies
99100
# see https://github.com/MichaelKim0407/flake8-use-fstring/issues/33

tests/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from os import getenv, path
2121
from pathlib import Path
2222
from re import sub as re_sub
23-
from typing import Any, Dict, Union
23+
from typing import Any, Union
2424
from unittest import TestCase
2525
from xml.sax.saxutils import escape as xml_escape, quoteattr as xml_quoteattr # nosec:B406
2626

@@ -62,12 +62,12 @@ def getSnapshotFile(snapshot_name: str) -> str: # noqa: N802
6262

6363
@classmethod
6464
def writeSnapshot(cls, snapshot_name: str, data: str) -> None: # noqa: N802
65-
with open(cls.getSnapshotFile(snapshot_name), 'wt', encoding='utf8', newline='\n') as sf:
65+
with open(cls.getSnapshotFile(snapshot_name), 'w', encoding='utf8', newline='\n') as sf:
6666
sf.write(data)
6767

6868
@classmethod
6969
def readSnapshot(cls, snapshot_name: str) -> str: # noqa: N802
70-
with open(cls.getSnapshotFile(snapshot_name), 'rt', encoding='utf8', newline='\n') as sf:
70+
with open(cls.getSnapshotFile(snapshot_name), encoding='utf8', newline='\n') as sf:
7171
return sf.read()
7272

7373
def assertEqualSnapshot(self: Union[TestCase, 'SnapshotMixin'], # noqa: N802
@@ -227,7 +227,7 @@ def make_comparable(bom: str, of: OutputFormat) -> str:
227227
# endregion reproducible test results
228228

229229

230-
def load_pyproject() -> Dict[str, Any]:
230+
def load_pyproject() -> dict[str, Any]:
231231
if sys.version_info >= (3, 11):
232232
from tomllib import load as toml_load
233233
else:

tox.ini

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,15 @@ commands =
4646
skip_install = True
4747
commands =
4848
poetry run bandit -c bandit.yml -v -r cyclonedx_py tests
49+
50+
[testenv:pyupgrade]
51+
allowlist_externals = poetry, sh
52+
commands = sh -c "\
53+
find cyclonedx_py tests -not -path '*/.venv/*' -type f -name '*.py' -print0 \
54+
| xargs -0 poetry run pyupgrade --py39-plus {posargs} "
55+
56+
[testenv:isort]
57+
commands = poetry run isort .
58+
59+
[testenv:autopep8]
60+
commands = poetry run autopep8 --in-place -r cyclonedx typings tests tools examples

0 commit comments

Comments
 (0)