Skip to content

Commit 63dc58c

Browse files
committed
helpers and pythonic
Signed-off-by: Jan Kowalleck <jan.kowalleck@gmail.com>
1 parent 0119417 commit 63dc58c

4 files changed

Lines changed: 12 additions & 17 deletions

File tree

cyclonedx/model/bom.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ def manufacture(self, manufacture: Optional[OrganizationalEntity]) -> None:
292292
we should set this data on `.component.manufacturer`.
293293
"""
294294
if manufacture is not None:
295-
warn(*DeprecationWarning1Dot6._prepw('bom.metadata.manufacture', 'bom.metadata.component.manufacturer'))
295+
DeprecationWarning1Dot6._warn('bom.metadata.manufacture', 'bom.metadata.component.manufacturer')
296296
self._manufacture = manufacture
297297

298298
@property

cyclonedx/model/component.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,7 +1186,7 @@ def author(self) -> Optional[str]:
11861186
@author.setter
11871187
def author(self, author: Optional[str]) -> None:
11881188
if author is not None:
1189-
warn(*DeprecationWarning1Dot6._prepw('@.author', '@.authors` or `@.manufacturer'))
1189+
DeprecationWarning1Dot6._warn('@.author', '@.authors` or `@.manufacturer')
11901190
self._author = author
11911191

11921192
@property
@@ -1467,7 +1467,7 @@ def modified(self) -> bool:
14671467
@modified.setter
14681468
def modified(self, modified: bool) -> None:
14691469
if modified:
1470-
warn(*DeprecationWarning1Dot3._prepw('@.modified', '@.pedigree'))
1470+
DeprecationWarning1Dot3._warn('@.modified', '@.pedigree')
14711471
self._modified = modified
14721472

14731473
@property

cyclonedx/model/tool.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ def tools(self) -> 'SortedSet[Tool]':
241241
@tools.setter
242242
def tools(self, tools: Iterable[Tool]) -> None:
243243
if tools:
244-
warn(*DeprecationWarning1Dot5._prepw('@.tools', '@.components` and `@.services'))
244+
DeprecationWarning1Dot5._warn('@.tools', '@.components` and `@.services')
245245
self._tools = SortedSet(tools)
246246

247247
def __len__(self) -> int:

cyclonedx/schema/deprecation.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
from abc import ABC
1919
from typing import ClassVar, Optional
20+
from warnings import warn
2021

2122
from . import SchemaVersion
2223

@@ -36,19 +37,13 @@ class SchemaDeprecationWarning(DeprecationWarning, ABC):
3637
SCHEMA_VERSION: ClassVar[SchemaVersion]
3738

3839
@classmethod
39-
def _prepw(cls, deprecated: str, instead: Optional[str] = None) -> tuple[str, type[SchemaDeprecationWarning]]:
40-
"""Prepare the warning message and category for schema deprecations.
41-
42-
Internal API. Not part of the public interface.
43-
44-
Intended to be used as:
45-
46-
warnings.warn(*SchemaDeprecationWarning._prepw("foo", "bar"))
47-
"""
48-
w = f'`{deprecated}` is deprecated from CycloneDX v{cls.SCHEMA_VERSION.to_version()} onwards.'
49-
if instead is not None:
50-
w += f' Please use `{instead}` instead.'
51-
return w, cls
40+
def _warn(cls, deprecated: str, instead: Optional[str] = None,
41+
*, stacklevel: int = 1) -> None:
42+
"""Internal API. Not part of the public interface."""
43+
msg = f'`{deprecated}` is deprecated from CycloneDX v{cls.SCHEMA_VERSION.to_version()} onwards.'
44+
if instead:
45+
msg += f' Please use `{instead}` instead.'
46+
warn(msg, cls, stacklevel=stacklevel + 1)
5247

5348

5449
class DeprecationWarning1Dot7(SchemaDeprecationWarning):

0 commit comments

Comments
 (0)