Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions .github/workflows/python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,30 @@
TESTS_REPORTS_ARTIFACT: tests-reports

jobs:
pyupgrade:
name: Find Upgradable CodingStandards
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- name: Checkout
# see https://github.com/actions/checkout
uses: actions/checkout@v4
- name: Setup Python Environment
# see https://github.com/actions/setup-python
uses: actions/setup-python@v5
with:
python-version: ${{ env.PYTHON_VERSION_DEFAULT }}
architecture: 'x64'
- name: Install poetry
# see https://github.com/marketplace/actions/setup-poetry
uses: Gr1N/setup-poetry@v9
with:
poetry-version: ${{ env.POETRY_VERSION }}
- name: Install dependencies
run: poetry install --no-root
- name: Run tox
run: poetry run tox run -e pyupgrade -s false

coding-standards:
name: Linting & CodingStandards
runs-on: ubuntu-latest
Expand Down
9 changes: 6 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@ poetry install --all-extras

## Code style

THis project loves latest python features.
This project uses [PEP8] Style Guide for Python Code.
This project loves sorted imports.
This project loves sorted imports.

Get it all applied via:

```shell
poetry run isort .
poetry run autopep8 -ir cyclonedx/ tests/ typings/ examples/
poetry run -- tox r -e pyupgrade -- --exit-zero-even-if-changed
poetry run -- tox r -e isort
poetry run -- tox r -e autopep8
```

This project prefers `f'strings'` over `'string'.format()`.
Expand Down
4 changes: 2 additions & 2 deletions cyclonedx/_internal/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class ComparableDict(ComparableTuple):
"""

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


class ComparablePackageURL(ComparableTuple):
Expand All @@ -73,7 +73,7 @@ class ComparablePackageURL(ComparableTuple):
"""

def __new__(cls, p: 'PackageURL') -> 'ComparablePackageURL':
return super(ComparablePackageURL, cls).__new__(cls, (
return super().__new__(cls, (
p.type,
p.namespace,
p.version,
Expand Down
2 changes: 1 addition & 1 deletion cyclonedx/spdx.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
__IDS: set[str] = set(json_load(schema).get('enum', []))
assert len(__IDS) > 0, 'known SPDX-IDs should be non-empty set'

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

__SPDX_EXPRESSION_LICENSING: 'Licensing' = get_spdx_licensing()

Expand Down
3 changes: 1 addition & 2 deletions cyclonedx/validation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,7 @@ def make_schemabased_validator(output_format: OutputFormat, schema_version: 'Sch
Raises error when no instance could be made.
"""
if TYPE_CHECKING: # pragma: no cover
from typing import Type
Validator: Type[BaseSchemabasedValidator] # noqa:N806
Validator: type[BaseSchemabasedValidator] # noqa:N806
if OutputFormat.JSON is output_format:
from .json import JsonValidator as Validator
elif OutputFormat.XML is output_format:
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ tomli = { version = "2.2.1", python = "<3.11" }
tox = "4.26.0"
xmldiff = "2.7.0"
bandit = "1.8.3"
pyupgrade = "3.20.0"

[tool.semantic_release]
# see https://python-semantic-release.readthedocs.io/en/latest/configuration.html
Expand Down
2 changes: 1 addition & 1 deletion tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def writeSnapshot(cls, snapshot_name: str, data: str) -> None: # noqa: N802

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

def assertEqualSnapshot(self: Union[TestCase, 'SnapshotMixin'], # noqa: N802
Expand Down
2 changes: 1 addition & 1 deletion tests/test_deserialize_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def test_prepared(self, get_bom: Callable[[], Bom], *_: Any, **__: Any) -> None:
# only latest schema will have all data populated in serialized form
snapshot_name = mksname(get_bom, SchemaVersion.V1_6, OutputFormat.XML)
expected = get_bom()
with open(self.getSnapshotFile(snapshot_name), 'r') as s:
with open(self.getSnapshotFile(snapshot_name)) as s:
bom = Bom.from_xml(s)
self.assertBomDeepEqual(expected, bom,
fuzzy_deps=get_bom in all_get_bom_funct_with_incomplete_deps)
3 changes: 1 addition & 2 deletions tests/test_enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@ def dp_cases_from_json_schema(sf: str, jsonpointer: Iterable[str]) -> Generator[
data = data[pp]
except KeyError:
return
for value in data['enum']:
yield value
yield from data['enum']


def dp_cases_from_json_schemas(*jsonpointer: str) -> Generator[str, None, None]:
Expand Down
10 changes: 5 additions & 5 deletions tests/test_validation_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
# SPDX-License-Identifier: Apache-2.0
# Copyright (c) OWASP Foundation. All Rights Reserved.

from collections.abc import Generator
from glob import iglob
from itertools import chain
from os.path import join
from typing import Generator
from unittest import TestCase

from ddt import data, ddt, idata, unpack
Expand Down Expand Up @@ -69,7 +69,7 @@ def test_throws_with_unsupported_schema_version(self, schema_version: SchemaVers
@unpack
def test_validate_no_none(self, schema_version: SchemaVersion, test_data_file: str) -> None:
validator = JsonValidator(schema_version)
with open(join(test_data_file), 'r') as tdfh:
with open(join(test_data_file)) as tdfh:
test_data = tdfh.read()
try:
validation_error = validator.validate_str(test_data)
Expand All @@ -84,7 +84,7 @@ def test_validate_no_none(self, schema_version: SchemaVersion, test_data_file: s
@unpack
def test_validate_expected_error(self, schema_version: SchemaVersion, test_data_file: str) -> None:
validator = JsonValidator(schema_version)
with open(join(test_data_file), 'r') as tdfh:
with open(join(test_data_file)) as tdfh:
test_data = tdfh.read()
try:
validation_error = validator.validate_str(test_data)
Expand All @@ -109,7 +109,7 @@ def test_throws_with_unsupported_schema_version(self, schema_version: SchemaVers
@unpack
def test_validate_no_none(self, schema_version: SchemaVersion, test_data_file: str) -> None:
validator = JsonStrictValidator(schema_version)
with open(join(test_data_file), 'r') as tdfh:
with open(join(test_data_file)) as tdfh:
test_data = tdfh.read()
try:
validation_error = validator.validate_str(test_data)
Expand All @@ -124,7 +124,7 @@ def test_validate_no_none(self, schema_version: SchemaVersion, test_data_file: s
@unpack
def test_validate_expected_error(self, schema_version: SchemaVersion, test_data_file: str) -> None:
validator = JsonStrictValidator(schema_version)
with open(join(test_data_file), 'r') as tdfh:
with open(join(test_data_file)) as tdfh:
test_data = tdfh.read()
try:
validation_error = validator.validate_str(test_data)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_validation_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def test_throws_with_unsupported_schema_version(self, schema_version: SchemaVers
@unpack
def test_validate_no_none(self, schema_version: SchemaVersion, test_data_file: str) -> None:
validator = XmlValidator(schema_version)
with open(join(test_data_file), 'r') as tdfh:
with open(join(test_data_file)) as tdfh:
test_data = tdfh.read()
try:
validation_error = validator.validate_str(test_data)
Expand All @@ -84,7 +84,7 @@ def test_validate_no_none(self, schema_version: SchemaVersion, test_data_file: s
@unpack
def test_validate_expected_error(self, schema_version: SchemaVersion, test_data_file: str) -> None:
validator = XmlValidator(schema_version)
with open(join(test_data_file), 'r') as tdfh:
with open(join(test_data_file)) as tdfh:
test_data = tdfh.read()
try:
validation_error = validator.validate_str(test_data)
Expand Down
2 changes: 1 addition & 1 deletion tools/schema-downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
target = dspec['targetPattern'].replace('%s', version)
tempfile, _ = urlretrieve(source) # nosec B310
print(source, '->', target)
with open(tempfile, 'r') as tmpf:
with open(tempfile) as tmpf:
text = tmpf.read()
with open(target, 'w', newline='\n') as tarf:
for search, replace in dspec['replace']:
Expand Down
10 changes: 10 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,14 @@ commands =
commands =
poetry run bandit -c bandit.yml -v -r cyclonedx tests examples tools

[testenv:pyupgrade]
allowlist_externals = poetry, sh
commands = sh -c "\
find cyclonedx typings tests tools examples -type f \( -name '*.py' -or -name '*.pyi' \) -print0 \
| xargs -0 poetry run pyupgrade --py39-plus {posargs} "

[testenv:isort]
commands = poetry run isort .

[testenv:autopep8]
commands = poetry run autopep8 --in-place -r cyclonedx typings tests tools examples
4 changes: 2 additions & 2 deletions typings/sortedcontainers.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
# The contents of this file were obtained from
# https://github.com/althonos/python-sortedcontainers/blob/d0a225d7fd0fb4c54532b8798af3cbeebf97e2d5/sortedcontainers/sortedset.pyi

from collections.abc import Callable, Iterable, MutableSet, Sequence
from typing import Any, Hashable, Optional, TypeVar, Union, overload # Iterator,; Tuple,; Type, Set
from collections.abc import Callable, Hashable, Iterable, MutableSet, Sequence
from typing import Any, Optional, TypeVar, Union, overload # Iterator,; Tuple,; Type, Set

# --- Global

Expand Down