Skip to content

Commit eba1a85

Browse files
committed
Fix linting errors
Signed-off-by: Tushar Goel <[email protected]>
1 parent c1a1291 commit eba1a85

File tree

3 files changed

+73
-41
lines changed

3 files changed

+73
-41
lines changed

src/packageurl/__init__.py

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,13 @@
2424

2525
from __future__ import annotations
2626

27+
import dataclasses
2728
import re
2829
import string
2930
from collections import namedtuple
3031
from collections.abc import Mapping
32+
from dataclasses import dataclass
33+
from enum import Enum
3134
from typing import TYPE_CHECKING
3235
from typing import Any
3336
from typing import Optional
@@ -58,6 +61,19 @@
5861
"""
5962

6063

64+
class ValidationSeverity(str, Enum):
65+
ERROR = "error"
66+
WARNING = "warning"
67+
INFO = "info"
68+
69+
70+
@dataclass
71+
class ValidationMessage:
72+
severity: ValidationSeverity
73+
message: str
74+
to_dict = dataclasses.asdict
75+
76+
6177
def quote(s: AnyStr) -> str:
6278
"""
6379
Return a percent-encoded unicode string, except for colon :, given an `s`
@@ -524,38 +540,38 @@ def to_string(self, encode: bool | None = True) -> str:
524540

525541
return "".join(purl)
526542

527-
def validate(self, strict: bool = False) -> list:
543+
def validate(self, strict: bool = False) -> list["ValidationMessage"]:
528544
"""
529545
Validate this PackageURL object and return a list of validation error messages.
530546
"""
531547
from packageurl.validate import DEFINITIONS_BY_TYPE
532-
from packageurl.validate import ValidationMessage
533-
from packageurl.validate import ValidationSeverity
534548

535549
validator_class = DEFINITIONS_BY_TYPE.get(self.type)
536550
if not validator_class:
537-
return [ValidationMessage(
538-
severity=ValidationSeverity.ERROR,
539-
message=f"Unexpected purl type: expected {self.type!r}",
540-
)]
551+
return [
552+
ValidationMessage(
553+
severity=ValidationSeverity.ERROR,
554+
message=f"Unexpected purl type: expected {self.type!r}",
555+
)
556+
]
541557
return list(validator_class.validate(purl=self, strict=strict)) # type: ignore[no-untyped-call]
542-
558+
543559
@classmethod
544-
def validate_string(cls, purl: str, strict: bool = False) -> list:
560+
def validate_string(cls, purl: str, strict: bool = False) -> list["ValidationMessage"]:
545561
"""
546562
Validate a PURL string and return a list of validation error messages.
547563
"""
548-
from packageurl.validate import ValidationMessage
549-
from packageurl.validate import ValidationSeverity
550-
551564
try:
552-
purl = cls.from_string(purl, normalize_purl=not strict)
565+
purl_obj = cls.from_string(purl, normalize_purl=not strict)
566+
assert isinstance(purl_obj, PackageURL)
567+
return purl_obj.validate(strict=strict)
553568
except ValueError as e:
554-
return [ValidationMessage(
555-
severity=ValidationSeverity.ERROR,
556-
message=str(e),
557-
)]
558-
return purl.validate(strict=strict)
569+
return [
570+
ValidationMessage(
571+
severity=ValidationSeverity.ERROR,
572+
message=str(e),
573+
)
574+
]
559575

560576
@classmethod
561577
def from_string(cls, purl: str, normalize_purl: bool = True) -> Self:

src/packageurl/validate.py

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,12 @@
2626
Validate each type according to the PURL spec type definitions
2727
"""
2828

29-
from enum import Enum
30-
from dataclasses import dataclass
31-
import dataclasses
32-
33-
34-
class ValidationSeverity(str, Enum):
35-
ERROR = "error"
36-
WARNING = "warning"
37-
INFO = "info"
38-
39-
40-
@dataclass
41-
class ValidationMessage:
42-
severity: ValidationSeverity
43-
message: str
44-
to_dict = dataclasses.asdict
4529

4630
class BasePurlType:
4731
"""
4832
Base class for all PURL type classes
4933
"""
34+
5035
type: str
5136
"""The type string for this Package-URL type."""
5237

@@ -86,13 +71,16 @@ def validate(cls, purl, strict=False):
8671
Validate a PackageURL instance or string.
8772
Yields ValidationMessage and performs strict validation if strict=True
8873
"""
74+
from packageurl import ValidationMessage
75+
from packageurl import ValidationSeverity
76+
8977
if not purl:
9078
yield ValidationMessage(
9179
severity=ValidationSeverity.ERROR,
9280
message="No purl provided",
9381
)
9482
return
95-
83+
9684
from packageurl import PackageURL
9785

9886
if not isinstance(purl, PackageURL):
@@ -103,11 +91,11 @@ def validate(cls, purl, strict=False):
10391
severity=ValidationSeverity.ERROR,
10492
message=f"Invalid purl {purl!r} string: {e}",
10593
)
106-
return
94+
return
10795

10896
if not strict:
10997
purl = cls.normalize(purl)
110-
98+
11199
yield from cls._validate_namespace(purl)
112100
yield from cls._validate_name(purl)
113101
yield from cls._validate_version(purl)
@@ -117,9 +105,12 @@ def validate(cls, purl, strict=False):
117105
messages = cls.validate_using_type_rules(purl, strict=strict)
118106
if messages:
119107
yield from messages
120-
108+
121109
@classmethod
122110
def _validate_namespace(cls, purl):
111+
from packageurl import ValidationMessage
112+
from packageurl import ValidationSeverity
113+
123114
if cls.namespace_requirement == "prohibited" and purl.namespace:
124115
yield ValidationMessage(
125116
severity=ValidationSeverity.ERROR,
@@ -148,18 +139,24 @@ def _validate_namespace(cls, purl):
148139
severity=ValidationSeverity.WARNING,
149140
message=f"Namespace is not lowercased for purl type: {cls.type!r}",
150141
)
151-
142+
152143
@classmethod
153144
def _validate_name(cls, purl):
154145
if not cls.name_case_sensitive and purl.name and purl.name.lower() != purl.name:
146+
from packageurl import ValidationMessage
147+
from packageurl import ValidationSeverity
148+
155149
yield ValidationMessage(
156150
severity=ValidationSeverity.WARNING,
157151
message=f"Name is not lowercased for purl type: {cls.type!r}",
158152
)
159-
153+
160154
@classmethod
161155
def _validate_version(cls, purl):
162156
if not cls.version_case_sensitive and purl.version and purl.version.lower() != purl.version:
157+
from packageurl import ValidationMessage
158+
from packageurl import ValidationSeverity
159+
163160
yield ValidationMessage(
164161
severity=ValidationSeverity.WARNING,
165162
message=f"Version is not lowercased for purl type: {cls.type!r}",
@@ -211,6 +208,9 @@ def _validate_qualifiers(cls, purl):
211208
disallowed = purl_qualifiers_keys - allowed_qualifiers_set
212209

213210
if disallowed:
211+
from packageurl import ValidationMessage
212+
from packageurl import ValidationSeverity
213+
214214
yield ValidationMessage(
215215
severity=ValidationSeverity.INFO,
216216
message=(
@@ -361,6 +361,9 @@ class CpanTypeDefinition(BasePurlType):
361361

362362
@classmethod
363363
def validate_using_type_rules(cls, purl, strict=False):
364+
from packageurl import ValidationMessage
365+
from packageurl import ValidationSeverity
366+
364367
if purl.namespace and "::" in purl.name:
365368
yield ValidationMessage(
366369
severity=ValidationSeverity.ERROR,
@@ -489,6 +492,9 @@ class HackageTypeDefinition(BasePurlType):
489492

490493
@classmethod
491494
def validate_using_type_rules(cls, purl, strict=False):
495+
from packageurl import ValidationMessage
496+
from packageurl import ValidationSeverity
497+
492498
if "_" in purl.name:
493499
yield ValidationMessage(
494500
severity=ValidationSeverity.WARNING,
@@ -626,6 +632,9 @@ class PubTypeDefinition(BasePurlType):
626632

627633
@classmethod
628634
def validate_using_type_rules(cls, purl, strict=False):
635+
from packageurl import ValidationMessage
636+
from packageurl import ValidationSeverity
637+
629638
if not all(c.isalnum() or c == "_" for c in purl.name):
630639
yield ValidationMessage(
631640
severity=ValidationSeverity.WARNING,
@@ -657,6 +666,9 @@ class PypiTypeDefinition(BasePurlType):
657666

658667
@classmethod
659668
def validate_using_type_rules(cls, purl, strict=False):
669+
from packageurl import ValidationMessage
670+
from packageurl import ValidationSeverity
671+
660672
if "_" in purl.name:
661673
yield ValidationMessage(
662674
severity=ValidationSeverity.WARNING,

tests/test_purl_spec.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,13 @@
2525
import json
2626
import os
2727
from dataclasses import dataclass
28-
from typing import Any, Dict, Optional, List
28+
from typing import Any
29+
from typing import Dict
30+
from typing import List
31+
from typing import Optional
2932

3033
import pytest
34+
3135
from packageurl import PackageURL
3236

3337

0 commit comments

Comments
 (0)