packageurl-python icon indicating copy to clipboard operation
packageurl-python copied to clipboard

setting attributes of a packageurl object

Open armijnhemel opened this issue 2 years ago • 1 comments

Is it possible to set attributes (such as version) in a packageurl object?

>>> import packageurl
>>> purl = packageurl.PackageURL.from_string('pkg:generic/busybox')
>>> purl.version
>>> purl.version = 3
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: can't set attribute

armijnhemel avatar May 11 '22 08:05 armijnhemel

See also #82

@armijnhemel The PackageURL class is actually generated from a namedtuple and since namedtuple objects are immutable, you cannot modified their values once initialized.

As a workaround, you can re-use the function evolve_purl available at https://github.com/nexB/vulnerablecode/commit/039babfaa0c712dfcc5eaf6d16f7c5d835b35b5d#diff-938a299f8406c1d3defaec48838bc4f6f1307635f5d2ba36e9777227cedbb383R144

Once implemented, your code would look something like:

>>> from packageurl import PackageURL
>>> purl = PackageURL.from_string('pkg:generic/busybox')
>>> evolve_purl(purl, version="3")
PackageURL(type='generic', namespace=None, name='busybox', version='3', qualifiers={}, subpath=None)

@pombredanne what's your though on refactoring PackageURL from a namedtuple to a proper class? In any case, in the short term, it would make sense to implement evolve_purl on the PackageURL class as suggested in #82. What about a simple update() method, instead of the evolve_purl() name?

tdruez avatar Jun 27 '22 15:06 tdruez