Skip to content

Commit bb0f7a5

Browse files
authored
style: streamline code quality (#472)
- raised some dev tools - added more quality checkers and rules - documented and applied additional code standards --------- Signed-off-by: Jan Kowalleck <[email protected]>
1 parent d09ac36 commit bb0f7a5

35 files changed

+237
-219
lines changed

.flake8

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[flake8]
2+
## https://flake8.pycqa.org/en/latest/user/configuration.html
3+
## keep in sync with isort config - in `isort.cfg` file
4+
5+
exclude =
6+
build,dist,__pycache__,.eggs,*.egg-info*,
7+
*_cache,*.cache,
8+
.git,.tox,.venv,venv,.venv*,venv*,
9+
_OLD,_TEST,
10+
docs
11+
12+
max-line-length = 120
13+
14+
max-complexity = 20
15+
16+
ignore =
17+
# ignore `self`, `cls` markers of flake8-annotations>=2.0
18+
ANN101,ANN102
19+
# ignore ANN401 for dynamically typed *args and **kwargs
20+
ANN401

.isort.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[settings]
22
## read the docs: https://pycqa.github.io/isort/docs/configuration/options.html
3-
## keep in sync with flake8 config - in `tox.ini` file
3+
## keep in sync with flake8 config - in `.flake8` file
44
known_first_party = cyclonedx
55
skip_gitignore = false
66
skip_glob =

CONTRIBUTING.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ poetry run isort .
2626
poetry run autopep8 -ir cyclonedx/ tests/ typings/ examples/
2727
```
2828

29+
This project prefers `f'strings'` over `'string'.format()`.
30+
This project prefers `'single quotes'` over `"double quotes"`.
31+
This project prefers `lower_snake_case` variable names.
32+
2933
## Documentation
3034

3135
This project uses [Sphinx] to generate documentation which is automatically published to [readthedocs.io].

cyclonedx/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@
2020

2121
# !! version is managed by semantic_release
2222
# do not use typing here, or else `semantic_release` might have issues finding the variable
23+
# flake8: noqa
2324
__version__ = "5.0.0-rc.1"

cyclonedx/exception/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919
"""
2020

2121

22-
class CycloneDxException(Exception):
22+
class CycloneDxException(Exception): # noqa: N818
2323
"""
2424
Root exception thrown by this library.
2525
"""
2626
pass
2727

2828

29-
class MissingOptionalDependencyException(CycloneDxException):
29+
class MissingOptionalDependencyException(CycloneDxException): # noqa: N818
3030
"""Validation did not happen, due to missing dependencies."""
3131
pass

cyclonedx/model/__init__.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import serializable
2424
from sortedcontainers import SortedSet
2525

26-
from .. import __version__ as __ThisToolVersion
26+
from .. import __version__ as __ThisToolVersion # noqa: N812
2727
from ..exception.model import (
2828
InvalidLocaleTypeException,
2929
InvalidUriException,
@@ -57,7 +57,7 @@ def sha1sum(filename: str) -> str:
5757
"""
5858
h = sha1()
5959
with open(filename, 'rb') as f:
60-
for byte_block in iter(lambda: f.read(4096), b""):
60+
for byte_block in iter(lambda: f.read(4096), b''):
6161
h.update(byte_block)
6262
return h.hexdigest()
6363

@@ -102,10 +102,10 @@ class DataFlow(str, Enum):
102102
.. note::
103103
See the CycloneDX Schema: https://cyclonedx.org/docs/1.4/xml/#type_dataFlowType
104104
"""
105-
INBOUND = "inbound"
106-
OUTBOUND = "outbound"
107-
BI_DIRECTIONAL = "bi-directional"
108-
UNKNOWN = "unknown"
105+
INBOUND = 'inbound'
106+
OUTBOUND = 'outbound'
107+
BI_DIRECTIONAL = 'bi-directional'
108+
UNKNOWN = 'unknown'
109109

110110

111111
@serializable.serializable_class
@@ -325,16 +325,16 @@ def from_composite_str(composite_hash: str) -> 'HashType':
325325
)
326326
elif algorithm_prefix[0:3] == 'sha':
327327
return HashType(
328-
alg=getattr(HashAlgorithm, 'SHA_{}'.format(algorithm_prefix[3:])),
328+
alg=getattr(HashAlgorithm, f'SHA_{algorithm_prefix[3:]}'),
329329
content=parts[1].lower()
330330
)
331331
elif algorithm_prefix[0:6] == 'blake2':
332332
return HashType(
333-
alg=getattr(HashAlgorithm, 'BLAKE2b_{}'.format(algorithm_prefix[6:])),
333+
alg=getattr(HashAlgorithm, f'BLAKE2b_{algorithm_prefix[6:]}'),
334334
content=parts[1].lower()
335335
)
336336

337-
raise UnknownHashTypeException(f"Unable to determine hash type from '{composite_hash}'")
337+
raise UnknownHashTypeException(f'Unable to determine hash type from {composite_hash!r}')
338338

339339
def __init__(self, *, alg: HashAlgorithm, content: str) -> None:
340340
self.alg = alg
@@ -542,7 +542,7 @@ def type(self, type: ExternalReferenceType) -> None:
542542
@serializable.view(SchemaVersion1Dot3)
543543
@serializable.view(SchemaVersion1Dot4)
544544
@serializable.xml_array(serializable.XmlArraySerializationType.NESTED, 'hash')
545-
def hashes(self) -> "SortedSet[HashType]":
545+
def hashes(self) -> 'SortedSet[HashType]':
546546
"""
547547
The hashes of the external reference (if applicable).
548548
@@ -779,9 +779,9 @@ def locale(self, locale: Optional[str]) -> None:
779779
if not re.search(Note._LOCALE_TYPE_REGEX, locale):
780780
self._locale = None
781781
raise InvalidLocaleTypeException(
782-
f"Supplied locale '{locale}' is not a valid locale. "
783-
f"Locale string should be formatted as the ISO-639 (or higher) language code and optional "
784-
f"ISO-3166 (or higher) country code. according to ISO-639 format. Examples include: 'en', 'en-US'."
782+
f'Supplied locale {locale!r} is not a valid locale.'
783+
' Locale string should be formatted as the ISO-639 (or higher) language code and optional'
784+
" ISO-3166 (or higher) country code. according to ISO-639 format. Examples include: 'en', 'en-US'."
785785
)
786786

787787
def __eq__(self, other: object) -> bool:
@@ -922,7 +922,7 @@ def name(self, name: Optional[str]) -> None:
922922
@serializable.json_name('url')
923923
@serializable.xml_array(serializable.XmlArraySerializationType.FLAT, 'url')
924924
@serializable.xml_sequence(2)
925-
def urls(self) -> "SortedSet[XsUri]":
925+
def urls(self) -> 'SortedSet[XsUri]':
926926
"""
927927
Get a list of URLs of the organization. Multiple URLs are allowed.
928928
@@ -939,7 +939,7 @@ def urls(self, urls: Iterable[XsUri]) -> None:
939939
@serializable.json_name('contact')
940940
@serializable.xml_array(serializable.XmlArraySerializationType.FLAT, 'contact')
941941
@serializable.xml_sequence(3)
942-
def contacts(self) -> "SortedSet[OrganizationalContact]":
942+
def contacts(self) -> 'SortedSet[OrganizationalContact]':
943943
"""
944944
Get a list of contact person at the organization. Multiple contacts are allowed.
945945
@@ -1037,7 +1037,7 @@ def version(self, version: Optional[str]) -> None:
10371037
@property
10381038
@serializable.xml_array(serializable.XmlArraySerializationType.NESTED, 'hash')
10391039
@serializable.xml_sequence(4)
1040-
def hashes(self) -> "SortedSet[HashType]":
1040+
def hashes(self) -> 'SortedSet[HashType]':
10411041
"""
10421042
The hashes of the tool (if applicable).
10431043
@@ -1054,7 +1054,7 @@ def hashes(self, hashes: Iterable[HashType]) -> None:
10541054
@serializable.view(SchemaVersion1Dot4)
10551055
@serializable.xml_array(serializable.XmlArraySerializationType.NESTED, 'reference')
10561056
@serializable.xml_sequence(5)
1057-
def external_references(self) -> "SortedSet[ExternalReference]":
1057+
def external_references(self) -> 'SortedSet[ExternalReference]':
10581058
"""
10591059
External References provide a way to document systems, sites, and information that may be relevant but which
10601060
are not included with the BOM.

cyclonedx/model/bom.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def timestamp(self, timestamp: datetime) -> None:
9494
@property
9595
@serializable.xml_array(serializable.XmlArraySerializationType.NESTED, 'tool')
9696
@serializable.xml_sequence(2)
97-
def tools(self) -> "SortedSet[Tool]":
97+
def tools(self) -> 'SortedSet[Tool]':
9898
"""
9999
Tools used to create this BOM.
100100
@@ -110,7 +110,7 @@ def tools(self, tools: Iterable[Tool]) -> None:
110110
@property
111111
@serializable.xml_array(serializable.XmlArraySerializationType.NESTED, 'author')
112112
@serializable.xml_sequence(3)
113-
def authors(self) -> "SortedSet[OrganizationalContact]":
113+
def authors(self) -> 'SortedSet[OrganizationalContact]':
114114
"""
115115
The person(s) who created the BOM.
116116
@@ -207,7 +207,7 @@ def licenses(self, licenses: Iterable[License]) -> None:
207207
@serializable.view(SchemaVersion1Dot4)
208208
@serializable.xml_array(serializable.XmlArraySerializationType.NESTED, 'property')
209209
@serializable.xml_sequence(8)
210-
def properties(self) -> "SortedSet[Property]":
210+
def properties(self) -> 'SortedSet[Property]':
211211
"""
212212
Provides the ability to document properties in a key/value store. This provides flexibility to include data not
213213
officially supported in the standard without having to use additional namespaces or create extensions.
@@ -335,7 +335,7 @@ def metadata(self, metadata: BomMetaData) -> None:
335335
@serializable.include_none(SchemaVersion1Dot1)
336336
@serializable.xml_array(serializable.XmlArraySerializationType.NESTED, 'component')
337337
@serializable.xml_sequence(2)
338-
def components(self) -> "SortedSet[Component]":
338+
def components(self) -> 'SortedSet[Component]':
339339
"""
340340
Get all the Components currently in this Bom.
341341
@@ -348,7 +348,7 @@ def components(self) -> "SortedSet[Component]":
348348
def components(self, components: Iterable[Component]) -> None:
349349
self._components = SortedSet(components)
350350

351-
def get_component_by_purl(self, purl: Optional["PackageURL"]) -> Optional[Component]:
351+
def get_component_by_purl(self, purl: Optional['PackageURL']) -> Optional[Component]:
352352
"""
353353
Get a Component already in the Bom by its PURL
354354
@@ -394,7 +394,7 @@ def has_component(self, component: Component) -> bool:
394394
@serializable.view(SchemaVersion1Dot4)
395395
@serializable.xml_array(serializable.XmlArraySerializationType.NESTED, 'service')
396396
@serializable.xml_sequence(3)
397-
def services(self) -> "SortedSet[Service]":
397+
def services(self) -> 'SortedSet[Service]':
398398
"""
399399
Get all the Services currently in this Bom.
400400
@@ -414,7 +414,7 @@ def services(self, services: Iterable[Service]) -> None:
414414
@serializable.view(SchemaVersion1Dot4)
415415
@serializable.xml_array(serializable.XmlArraySerializationType.NESTED, 'reference')
416416
@serializable.xml_sequence(4)
417-
def external_references(self) -> "SortedSet[ExternalReference]":
417+
def external_references(self) -> 'SortedSet[ExternalReference]':
418418
"""
419419
Provides the ability to document external references related to the BOM or to the project the BOM describes.
420420
@@ -438,7 +438,7 @@ def _get_all_components(self) -> Set[Component]:
438438

439439
return components
440440

441-
def get_vulnerabilities_for_bom_ref(self, bom_ref: BomRef) -> "SortedSet[Vulnerability]":
441+
def get_vulnerabilities_for_bom_ref(self, bom_ref: BomRef) -> 'SortedSet[Vulnerability]':
442442
"""
443443
Get all known Vulnerabilities that affect the supplied bom_ref.
444444
@@ -469,7 +469,7 @@ def has_vulnerabilities(self) -> bool:
469469
@serializable.view(SchemaVersion1Dot4)
470470
@serializable.xml_array(serializable.XmlArraySerializationType.NESTED, 'vulnerability')
471471
@serializable.xml_sequence(8)
472-
def vulnerabilities(self) -> "SortedSet[Vulnerability]":
472+
def vulnerabilities(self) -> 'SortedSet[Vulnerability]':
473473
"""
474474
Get all the Vulnerabilities in this BOM.
475475
@@ -497,7 +497,7 @@ def version(self, version: int) -> None:
497497
@serializable.view(SchemaVersion1Dot4)
498498
@serializable.xml_array(serializable.XmlArraySerializationType.NESTED, 'dependency')
499499
@serializable.xml_sequence(5)
500-
def dependencies(self) -> "SortedSet[Dependency]":
500+
def dependencies(self) -> 'SortedSet[Dependency]':
501501
return self._dependencies
502502

503503
@dependencies.setter
@@ -564,7 +564,7 @@ def validate(self) -> bool:
564564
f'The Component this BOM is describing {self.metadata.component.purl} has no defined dependencies '
565565
f'which means the Dependency Graph is incomplete - you should add direct dependencies to this '
566566
f'"root" Component to complete the Dependency Graph data.',
567-
UserWarning
567+
category=UserWarning, stacklevel=1
568568
)
569569

570570
# 3. If a LicenseExpression is set, then there must be no other license.

0 commit comments

Comments
 (0)