Skip to content

Commit d96295c

Browse files
authored
Define pkg as a PackageURL class attribute (#184)
1 parent c42e618 commit d96295c

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/packageurl/__init__.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
if TYPE_CHECKING:
3838
from collections.abc import Callable
3939
from collections.abc import Iterable
40+
from typing import ClassVar
4041

4142
from typing_extensions import Literal
4243
from typing_extensions import Self
@@ -314,6 +315,8 @@ class PackageURL(
314315
https://github.com/package-url/purl-spec
315316
"""
316317

318+
SCHEME: ClassVar[str] = "pkg"
319+
317320
type: str
318321
namespace: str | None
319322
name: str
@@ -409,7 +412,7 @@ def to_string(self) -> str:
409412
encode=True,
410413
)
411414

412-
purl = ["pkg:", type, "/"]
415+
purl = [self.SCHEME, ":", type, "/"]
413416

414417
if namespace:
415418
purl.extend((namespace, "/"))
@@ -440,8 +443,10 @@ def from_string(cls, purl: str) -> Self:
440443
raise ValueError("A purl string argument is required.")
441444

442445
scheme, sep, remainder = purl.partition(":")
443-
if not sep or scheme != "pkg":
444-
raise ValueError(f'purl is missing the required "pkg" scheme component: {purl!r}.')
446+
if not sep or scheme != cls.SCHEME:
447+
raise ValueError(
448+
f'purl is missing the required "{cls.SCHEME}" scheme component: {purl!r}.'
449+
)
445450

446451
# this strip '/, // and /// as possible in :// or :///
447452
remainder = remainder.strip().lstrip("/")

0 commit comments

Comments
 (0)