Skip to content

fix: display DeprecationWarnings for deprecated properties properly #837

@KAWAHARA-souta

Description

@KAWAHARA-souta

related to: #760

Currently, the implementation for handling deprecated member variables in certain classes issues a DeprecationWarning directly within the class's constructor.
For example, in the BomMetaData class (cyclonedx/model/bom.py):

@serializable.serializable_class
class BomMetaData:
    def __init__(
        self, *,
        ... many other args ...                        
        # Deprecated as of v1.6                                                       
        manufacture: Optional[OrganizationalEntity] = None,                           
    ) -> None:                                                                        
        ... many other codes ...
        self.manufacture = manufacture
        if manufacture:                                         
            warn(                                        
                '`bom.metadata.manufacture` is deprecated from CycloneDX v1.6 onwards. '
                'Please use `bom.metadata.component.manufacturer` instead.',
                DeprecationWarning)

However, implementing this logic within the constructor prevents the DeprecationWarning from being issued when the value is set via a setter after the instance has been generated.
Here's an example:

from cyclonedx.model.bom import BomMetaData
from cyclonedx.model.contact import OrganizationalEntity

org = OrganizationalEntity(name='me')

print('DeprecationWarning will be triggerd within construictor')
data2 = BomMetaData(manufacture=org)

print("DeprecationWarning won't be triggerd using setter after instance generated")
data2 = BomMetaData()
data2.manufacture = org

Therefore, this logic needs to be implemented within the corresponding setter.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions