Skip to content

Commit f2616e6

Browse files
authored
style: pyupgrade 3.9 (#822)
- followup of #809 - add pyupgrade tooling and tests - apply pyupgraded code style --------- Signed-off-by: Jan Kowalleck <jan.kowalleck@gmail.com>
1 parent 8ac09b6 commit f2616e6

File tree

14 files changed

+58
-22
lines changed

14 files changed

+58
-22
lines changed

.github/workflows/python.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,30 @@ env:
2626
TESTS_REPORTS_ARTIFACT: tests-reports
2727

2828
jobs:
29+
pyupgrade:
30+
name: Find Upgradable CodingStandards
31+
runs-on: ubuntu-latest
32+
timeout-minutes: 10
33+
steps:
34+
- name: Checkout
35+
# see https://github.com/actions/checkout
36+
uses: actions/checkout@v4
37+
- name: Setup Python Environment
38+
# see https://github.com/actions/setup-python
39+
uses: actions/setup-python@v5
40+
with:
41+
python-version: ${{ env.PYTHON_VERSION_DEFAULT }}
42+
architecture: 'x64'
43+
- name: Install poetry
44+
# see https://github.com/marketplace/actions/setup-poetry
45+
uses: Gr1N/setup-poetry@v9
46+
with:
47+
poetry-version: ${{ env.POETRY_VERSION }}
48+
- name: Install dependencies
49+
run: poetry install --no-root
50+
- name: Run tox
51+
run: poetry run tox run -e pyupgrade -s false
52+
2953
coding-standards:
3054
name: Linting & CodingStandards
3155
runs-on: ubuntu-latest

CONTRIBUTING.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,16 @@ poetry install --all-extras
2121

2222
## Code style
2323

24+
THis project loves latest python features.
2425
This project uses [PEP8] Style Guide for Python Code.
25-
This project loves sorted imports.
26+
This project loves sorted imports.
27+
2628
Get it all applied via:
2729

2830
```shell
29-
poetry run isort .
30-
poetry run autopep8 -ir cyclonedx/ tests/ typings/ examples/
31+
poetry run -- tox r -e pyupgrade -- --exit-zero-even-if-changed
32+
poetry run -- tox r -e isort
33+
poetry run -- tox r -e autopep8
3134
```
3235

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

cyclonedx/_internal/compare.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class ComparableDict(ComparableTuple):
6464
"""
6565

6666
def __new__(cls, d: dict[Any, Any]) -> 'ComparableDict':
67-
return super(ComparableDict, cls).__new__(cls, sorted(d.items()))
67+
return super().__new__(cls, sorted(d.items()))
6868

6969

7070
class ComparablePackageURL(ComparableTuple):
@@ -73,7 +73,7 @@ class ComparablePackageURL(ComparableTuple):
7373
"""
7474

7575
def __new__(cls, p: 'PackageURL') -> 'ComparablePackageURL':
76-
return super(ComparablePackageURL, cls).__new__(cls, (
76+
return super().__new__(cls, (
7777
p.type,
7878
p.namespace,
7979
p.version,

cyclonedx/spdx.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
__IDS: set[str] = set(json_load(schema).get('enum', []))
4040
assert len(__IDS) > 0, 'known SPDX-IDs should be non-empty set'
4141

42-
__IDS_LOWER_MAP: dict[str, str] = dict((id_.lower(), id_) for id_ in __IDS)
42+
__IDS_LOWER_MAP: dict[str, str] = {id_.lower(): id_ for id_ in __IDS}
4343

4444
__SPDX_EXPRESSION_LICENSING: 'Licensing' = get_spdx_licensing()
4545

cyclonedx/validation/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,7 @@ def make_schemabased_validator(output_format: OutputFormat, schema_version: 'Sch
110110
Raises error when no instance could be made.
111111
"""
112112
if TYPE_CHECKING: # pragma: no cover
113-
from typing import Type
114-
Validator: Type[BaseSchemabasedValidator] # noqa:N806
113+
Validator: type[BaseSchemabasedValidator] # noqa:N806
115114
if OutputFormat.JSON is output_format:
116115
from .json import JsonValidator as Validator
117116
elif OutputFormat.XML is output_format:

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ tomli = { version = "2.2.1", python = "<3.11" }
9898
tox = "4.26.0"
9999
xmldiff = "2.7.0"
100100
bandit = "1.8.3"
101+
pyupgrade = "3.20.0"
101102

102103
[tool.semantic_release]
103104
# see https://python-semantic-release.readthedocs.io/en/latest/configuration.html

tests/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def writeSnapshot(cls, snapshot_name: str, data: str) -> None: # noqa: N802
5858

5959
@classmethod
6060
def readSnapshot(cls, snapshot_name: str) -> str: # noqa: N802
61-
with open(cls.getSnapshotFile(snapshot_name), 'r') as s:
61+
with open(cls.getSnapshotFile(snapshot_name)) as s:
6262
return s.read()
6363

6464
def assertEqualSnapshot(self: Union[TestCase, 'SnapshotMixin'], # noqa: N802

tests/test_deserialize_xml.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def test_prepared(self, get_bom: Callable[[], Bom], *_: Any, **__: Any) -> None:
4242
# only latest schema will have all data populated in serialized form
4343
snapshot_name = mksname(get_bom, SchemaVersion.V1_6, OutputFormat.XML)
4444
expected = get_bom()
45-
with open(self.getSnapshotFile(snapshot_name), 'r') as s:
45+
with open(self.getSnapshotFile(snapshot_name)) as s:
4646
bom = Bom.from_xml(s)
4747
self.assertBomDeepEqual(expected, bom,
4848
fuzzy_deps=get_bom in all_get_bom_funct_with_incomplete_deps)

tests/test_enums.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,7 @@ def dp_cases_from_json_schema(sf: str, jsonpointer: Iterable[str]) -> Generator[
103103
data = data[pp]
104104
except KeyError:
105105
return
106-
for value in data['enum']:
107-
yield value
106+
yield from data['enum']
108107

109108

110109
def dp_cases_from_json_schemas(*jsonpointer: str) -> Generator[str, None, None]:

tests/test_validation_json.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
# SPDX-License-Identifier: Apache-2.0
1616
# Copyright (c) OWASP Foundation. All Rights Reserved.
1717

18+
from collections.abc import Generator
1819
from glob import iglob
1920
from itertools import chain
2021
from os.path import join
21-
from typing import Generator
2222
from unittest import TestCase
2323

2424
from ddt import data, ddt, idata, unpack
@@ -69,7 +69,7 @@ def test_throws_with_unsupported_schema_version(self, schema_version: SchemaVers
6969
@unpack
7070
def test_validate_no_none(self, schema_version: SchemaVersion, test_data_file: str) -> None:
7171
validator = JsonValidator(schema_version)
72-
with open(join(test_data_file), 'r') as tdfh:
72+
with open(join(test_data_file)) as tdfh:
7373
test_data = tdfh.read()
7474
try:
7575
validation_error = validator.validate_str(test_data)
@@ -84,7 +84,7 @@ def test_validate_no_none(self, schema_version: SchemaVersion, test_data_file: s
8484
@unpack
8585
def test_validate_expected_error(self, schema_version: SchemaVersion, test_data_file: str) -> None:
8686
validator = JsonValidator(schema_version)
87-
with open(join(test_data_file), 'r') as tdfh:
87+
with open(join(test_data_file)) as tdfh:
8888
test_data = tdfh.read()
8989
try:
9090
validation_error = validator.validate_str(test_data)
@@ -109,7 +109,7 @@ def test_throws_with_unsupported_schema_version(self, schema_version: SchemaVers
109109
@unpack
110110
def test_validate_no_none(self, schema_version: SchemaVersion, test_data_file: str) -> None:
111111
validator = JsonStrictValidator(schema_version)
112-
with open(join(test_data_file), 'r') as tdfh:
112+
with open(join(test_data_file)) as tdfh:
113113
test_data = tdfh.read()
114114
try:
115115
validation_error = validator.validate_str(test_data)
@@ -124,7 +124,7 @@ def test_validate_no_none(self, schema_version: SchemaVersion, test_data_file: s
124124
@unpack
125125
def test_validate_expected_error(self, schema_version: SchemaVersion, test_data_file: str) -> None:
126126
validator = JsonStrictValidator(schema_version)
127-
with open(join(test_data_file), 'r') as tdfh:
127+
with open(join(test_data_file)) as tdfh:
128128
test_data = tdfh.read()
129129
try:
130130
validation_error = validator.validate_str(test_data)

0 commit comments

Comments
 (0)