Skip to content

Commit c3c068a

Browse files
committed
pylock: use frozen dataclasses
Immutable objects are good. In Python 3.9, it's a bit messy, though.
1 parent 8cad294 commit c3c068a

File tree

1 file changed

+56
-56
lines changed

1 file changed

+56
-56
lines changed

src/pip/_internal/models/pylock.py

Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ class PylockUnsupportedVersionError(PylockValidationError):
256256
pass
257257

258258

259-
@dataclass
259+
@dataclass(frozen=True)
260260
class PackageVcs:
261261
type: str
262262
url: Optional[str] # = None
@@ -276,12 +276,12 @@ def __init__(
276276
subdirectory: Optional[str] = None,
277277
) -> None:
278278
# In Python 3.10+ make dataclass kw_only=True and remove __init__
279-
self.type = type
280-
self.url = url
281-
self.path = path
282-
self.requested_revision = requested_revision
283-
self.commit_id = commit_id
284-
self.subdirectory = subdirectory
279+
object.__setattr__(self, "type", type)
280+
object.__setattr__(self, "url", url)
281+
object.__setattr__(self, "path", path)
282+
object.__setattr__(self, "requested_revision", requested_revision)
283+
object.__setattr__(self, "commit_id", commit_id)
284+
object.__setattr__(self, "subdirectory", subdirectory)
285285
# __post_init__ in Python 3.10+
286286
_validate_path_url(self.path, self.url)
287287

@@ -297,7 +297,7 @@ def from_dict(cls, d: Dict[str, Any]) -> "Self":
297297
)
298298

299299

300-
@dataclass
300+
@dataclass(frozen=True)
301301
class PackageDirectory:
302302
path: str
303303
editable: Optional[bool] = None
@@ -311,9 +311,9 @@ def __init__(
311311
subdirectory: Optional[str] = None,
312312
) -> None:
313313
# In Python 3.10+ make dataclass kw_only=True and remove __init__
314-
self.path = path
315-
self.editable = editable
316-
self.subdirectory = subdirectory
314+
object.__setattr__(self, "path", path)
315+
object.__setattr__(self, "editable", editable)
316+
object.__setattr__(self, "subdirectory", subdirectory)
317317

318318
@classmethod
319319
def from_dict(cls, d: Dict[str, Any]) -> "Self":
@@ -324,7 +324,7 @@ def from_dict(cls, d: Dict[str, Any]) -> "Self":
324324
)
325325

326326

327-
@dataclass
327+
@dataclass(frozen=True)
328328
class PackageArchive:
329329
url: Optional[str] # = None
330330
path: Optional[str] # = None
@@ -344,12 +344,12 @@ def __init__(
344344
subdirectory: Optional[str] = None,
345345
) -> None:
346346
# In Python 3.10+ make dataclass kw_only=True and remove __init__
347-
self.url = url
348-
self.path = path
349-
self.size = size
350-
self.upload_time = upload_time
351-
self.hashes = hashes
352-
self.subdirectory = subdirectory
347+
object.__setattr__(self, "url", url)
348+
object.__setattr__(self, "path", path)
349+
object.__setattr__(self, "size", size)
350+
object.__setattr__(self, "upload_time", upload_time)
351+
object.__setattr__(self, "hashes", hashes)
352+
object.__setattr__(self, "subdirectory", subdirectory)
353353
# __post_init__ in Python 3.10+
354354
_validate_path_url(self.path, self.url)
355355
_validate_hashes(self.hashes)
@@ -366,7 +366,7 @@ def from_dict(cls, d: Dict[str, Any]) -> "Self":
366366
)
367367

368368

369-
@dataclass
369+
@dataclass(frozen=True)
370370
class PackageSdist:
371371
name: str
372372
upload_time: Optional[datetime] # = None
@@ -386,12 +386,12 @@ def __init__(
386386
size: Optional[int] = None,
387387
) -> None:
388388
# In Python 3.10+ make dataclass kw_only=True and remove __init__
389-
self.name = name
390-
self.upload_time = upload_time
391-
self.url = url
392-
self.path = path
393-
self.size = size
394-
self.hashes = hashes
389+
object.__setattr__(self, "name", name)
390+
object.__setattr__(self, "upload_time", upload_time)
391+
object.__setattr__(self, "url", url)
392+
object.__setattr__(self, "path", path)
393+
object.__setattr__(self, "size", size)
394+
object.__setattr__(self, "hashes", hashes)
395395
# __post_init__ in Python 3.10+
396396
_validate_path_url(self.path, self.url)
397397
_validate_hashes(self.hashes)
@@ -408,7 +408,7 @@ def from_dict(cls, d: Dict[str, Any]) -> "Self":
408408
)
409409

410410

411-
@dataclass
411+
@dataclass(frozen=True)
412412
class PackageWheel:
413413
name: str
414414
upload_time: Optional[datetime] # = None
@@ -428,12 +428,12 @@ def __init__(
428428
size: Optional[int] = None,
429429
) -> None:
430430
# In Python 3.10+ make dataclass kw_only=True and remove __init__
431-
self.name = name
432-
self.upload_time = upload_time
433-
self.url = url
434-
self.path = path
435-
self.size = size
436-
self.hashes = hashes
431+
object.__setattr__(self, "name", name)
432+
object.__setattr__(self, "upload_time", upload_time)
433+
object.__setattr__(self, "url", url)
434+
object.__setattr__(self, "path", path)
435+
object.__setattr__(self, "size", size)
436+
object.__setattr__(self, "hashes", hashes)
437437
# __post_init__ in Python 3.10+
438438
_validate_path_url(self.path, self.url)
439439
_validate_hashes(self.hashes)
@@ -451,7 +451,7 @@ def from_dict(cls, d: Dict[str, Any]) -> "Self":
451451
return wheel
452452

453453

454-
@dataclass
454+
@dataclass(frozen=True)
455455
class Package:
456456
name: str
457457
version: Optional[Version] = None
@@ -485,19 +485,19 @@ def __init__(
485485
tool: Optional[Dict[str, Any]] = None,
486486
) -> None:
487487
# In Python 3.10+ make dataclass kw_only=True and remove __init__
488-
self.name = name
489-
self.version = version
490-
self.marker = marker
491-
self.requires_python = requires_python
492-
self.dependencies = dependencies
493-
self.vcs = vcs
494-
self.directory = directory
495-
self.archive = archive
496-
self.index = index
497-
self.sdist = sdist
498-
self.wheels = wheels
499-
self.attestation_identities = attestation_identities
500-
self.tool = tool
488+
object.__setattr__(self, "name", name)
489+
object.__setattr__(self, "version", version)
490+
object.__setattr__(self, "marker", marker)
491+
object.__setattr__(self, "requires_python", requires_python)
492+
object.__setattr__(self, "dependencies", dependencies)
493+
object.__setattr__(self, "vcs", vcs)
494+
object.__setattr__(self, "directory", directory)
495+
object.__setattr__(self, "archive", archive)
496+
object.__setattr__(self, "index", index)
497+
object.__setattr__(self, "sdist", sdist)
498+
object.__setattr__(self, "wheels", wheels)
499+
object.__setattr__(self, "attestation_identities", attestation_identities)
500+
object.__setattr__(self, "tool", tool)
501501
# __post_init__ in Python 3.10+
502502
if self.sdist or self.wheels:
503503
if any([self.vcs, self.directory, self.archive]):
@@ -533,7 +533,7 @@ def from_dict(cls, d: Dict[str, Any]) -> "Self":
533533
return package
534534

535535

536-
@dataclass
536+
@dataclass(frozen=True)
537537
class Pylock:
538538
lock_version: Version
539539
environments: Optional[List[Marker]] # = None
@@ -559,15 +559,15 @@ def __init__(
559559
tool: Optional[Dict[str, Any]] = None,
560560
) -> None:
561561
# In Python 3.10+ make dataclass kw_only=True and remove __init__
562-
self.lock_version = lock_version
563-
self.environments = environments
564-
self.requires_python = requires_python
565-
self.extras = extras or []
566-
self.dependency_groups = dependency_groups or []
567-
self.default_groups = default_groups or []
568-
self.created_by = created_by
569-
self.packages = packages
570-
self.tool = tool
562+
object.__setattr__(self, "lock_version", lock_version)
563+
object.__setattr__(self, "environments", environments)
564+
object.__setattr__(self, "requires_python", requires_python)
565+
object.__setattr__(self, "extras", extras or [])
566+
object.__setattr__(self, "dependency_groups", dependency_groups or [])
567+
object.__setattr__(self, "default_groups", default_groups or [])
568+
object.__setattr__(self, "created_by", created_by)
569+
object.__setattr__(self, "packages", packages)
570+
object.__setattr__(self, "tool", tool)
571571
# __post_init__ in Python 3.10+
572572
if self.lock_version < Version("1") or self.lock_version >= Version("2"):
573573
raise PylockUnsupportedVersionError(

0 commit comments

Comments
 (0)