-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Use dataclasses more liberally throughout the codebase #12571
Merged
Merged
Changes from 17 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
c7c4043
Convert Scheme into a dataclass
ichard26 a643731
Convert InstallationCandidate into a dataclass
ichard26 6bfc426
Remove Link's dependency on KeyBasedCompareMixin
ichard26 a70b593
Remove KeyBasedCompareMixin
ichard26 42c1b69
Convert IndexContent and CandidatePreferences to dataclasses
ichard26 534dab3
Convert most of models.direct_url to dataclasses
ichard26 924e0f0
Convert SearchScope to a dataclass
ichard26 d3596d0
Add TODO for SelectionPreferences
ichard26 7cd8b0e
Convert prepare.File to a dataclass
ichard26 4f317b2
Convert RequirementParts and InstallationResult to dataclasses
ichard26 39970e6
Convert Constraint to a dataclass
ichard26 29a452c
Convert HiddenText to a dataclass
ichard26 7f67070
Convert RevOptions into a dataclass
ichard26 39af6fe
📰
ichard26 3738d25
Freeze locations' Scheme dataclass
ichard26 b8cf8fa
Trim unnecessary lines
ichard26 bde667d
Restore HiddenText.__eq__ and associated test
ichard26 d8afb77
Merge branch 'main' into cleanup-dataclasses
ichard26 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Convert numerous internal classes to dataclasses for readability and stricter | ||
enforcement of immutability across the codebase. A conservative approach was | ||
taken in selecting which classes to convert. Classes which did not convert | ||
cleanly into a dataclass or were "too complex" (e.g. maintains interconnected | ||
state) were left alone. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,25 @@ | ||
from dataclasses import dataclass | ||
|
||
from pip._vendor.packaging.version import Version | ||
from pip._vendor.packaging.version import parse as parse_version | ||
|
||
from pip._internal.models.link import Link | ||
from pip._internal.utils.models import KeyBasedCompareMixin | ||
|
||
|
||
class InstallationCandidate(KeyBasedCompareMixin): | ||
@dataclass(frozen=True) | ||
class InstallationCandidate: | ||
"""Represents a potential "candidate" for installation.""" | ||
|
||
__slots__ = ["name", "version", "link"] | ||
|
||
name: str | ||
version: Version | ||
link: Link | ||
|
||
def __init__(self, name: str, version: str, link: Link) -> None: | ||
self.name = name | ||
self.version = parse_version(version) | ||
self.link = link | ||
|
||
super().__init__( | ||
key=(self.name, self.version, self.link), | ||
defining_class=InstallationCandidate, | ||
) | ||
|
||
def __repr__(self) -> str: | ||
return "<InstallationCandidate({!r}, {!r}, {!r})>".format( | ||
self.name, | ||
self.version, | ||
self.link, | ||
) | ||
object.__setattr__(self, "name", name) | ||
object.__setattr__(self, "version", parse_version(version)) | ||
object.__setattr__(self, "link", link) | ||
|
||
def __str__(self) -> str: | ||
return f"{self.name!r} candidate (version {self.version} at {self.link})" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,8 @@ | |
import json | ||
import re | ||
import urllib.parse | ||
from typing import Any, Dict, Iterable, Optional, Type, TypeVar, Union | ||
from dataclasses import dataclass | ||
from typing import Any, ClassVar, Dict, Iterable, Optional, Type, TypeVar, Union | ||
|
||
__all__ = [ | ||
"DirectUrl", | ||
|
@@ -64,18 +65,13 @@ def _filter_none(**kwargs: Any) -> Dict[str, Any]: | |
return {k: v for k, v in kwargs.items() if v is not None} | ||
|
||
|
||
@dataclass | ||
class VcsInfo: | ||
Comment on lines
+69
to
70
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I opted to leave the direct_url classes non-frozen as some tests broke and it would be more consistent with |
||
name = "vcs_info" | ||
name: ClassVar = "vcs_info" | ||
|
||
def __init__( | ||
self, | ||
vcs: str, | ||
commit_id: str, | ||
requested_revision: Optional[str] = None, | ||
) -> None: | ||
self.vcs = vcs | ||
self.requested_revision = requested_revision | ||
self.commit_id = commit_id | ||
vcs: str | ||
commit_id: str | ||
requested_revision: Optional[str] = None | ||
|
||
@classmethod | ||
def _from_dict(cls, d: Optional[Dict[str, Any]]) -> Optional["VcsInfo"]: | ||
|
@@ -139,14 +135,11 @@ def _to_dict(self) -> Dict[str, Any]: | |
return _filter_none(hash=self.hash, hashes=self.hashes) | ||
|
||
|
||
@dataclass | ||
class DirInfo: | ||
name = "dir_info" | ||
name: ClassVar = "dir_info" | ||
|
||
def __init__( | ||
self, | ||
editable: bool = False, | ||
) -> None: | ||
self.editable = editable | ||
editable: bool = False | ||
|
||
@classmethod | ||
def _from_dict(cls, d: Optional[Dict[str, Any]]) -> Optional["DirInfo"]: | ||
|
@@ -161,16 +154,11 @@ def _to_dict(self) -> Dict[str, Any]: | |
InfoType = Union[ArchiveInfo, DirInfo, VcsInfo] | ||
|
||
|
||
@dataclass | ||
class DirectUrl: | ||
def __init__( | ||
self, | ||
url: str, | ||
info: InfoType, | ||
subdirectory: Optional[str] = None, | ||
) -> None: | ||
self.url = url | ||
self.info = info | ||
self.subdirectory = subdirectory | ||
url: str | ||
info: InfoType | ||
subdirectory: Optional[str] = None | ||
|
||
def _remove_auth_from_netloc(self, netloc: str) -> str: | ||
if "@" not in netloc: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The codebase does assign to these attributes from time to time...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sigh