Skip to content

Define pkg as a PackageURL class attribute #184

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 6, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Define pkg as a PackageURL class attribute
  • Loading branch information
jaimergp committed Mar 6, 2025
commit a164b8c1a9643b74b9a6bcd9785ffcfc7e2135b2
9 changes: 6 additions & 3 deletions src/packageurl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ class PackageURL(
A purl is a package URL as defined at
https://github.com/package-url/purl-spec
"""
SCHEME: str = "pkg"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if using lower-caps scheme for consistency with existing vars would make more sense. I don't see anything declared as upper-caps in that module.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used uppercase because it's supposed to be constant for the class, maybe I can indicate that with ClassVar[str] instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to ClassVar[str]


type: str
namespace: str | None
Expand Down Expand Up @@ -409,7 +410,7 @@ def to_string(self) -> str:
encode=True,
)

purl = ["pkg:", type, "/"]
purl = [f"{self.SCHEME}:", type, "/"]

if namespace:
purl.extend((namespace, "/"))
Expand Down Expand Up @@ -440,8 +441,10 @@ def from_string(cls, purl: str) -> Self:
raise ValueError("A purl string argument is required.")

scheme, sep, remainder = purl.partition(":")
if not sep or scheme != "pkg":
raise ValueError(f'purl is missing the required "pkg" scheme component: {purl!r}.')
if not sep or scheme != cls.SCHEME:
raise ValueError(
f'purl is missing the required "{cls.SCHEME}" scheme component: {purl!r}.'
)

# this strip '/, // and /// as possible in :// or :///
remainder = remainder.strip().lstrip("/")
Expand Down
Loading