Skip to content

Commit 6958b87

Browse files
authored
Complete type annotations: pip/_internal/models (#10138)
1 parent 23eb69f commit 6958b87

11 files changed

+115
-170
lines changed

news/10138.trivial.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Convert type commentaries to annotations on ``pip/_internal/models``.

src/pip/_internal/models/candidate.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ class InstallationCandidate(KeyBasedCompareMixin):
1010

1111
__slots__ = ["name", "version", "link"]
1212

13-
def __init__(self, name, version, link):
14-
# type: (str, str, Link) -> None
13+
def __init__(self, name: str, version: str, link: Link) -> None:
1514
self.name = name
1615
self.version = parse_version(version)
1716
self.link = link
@@ -21,14 +20,12 @@ def __init__(self, name, version, link):
2120
defining_class=InstallationCandidate
2221
)
2322

24-
def __repr__(self):
25-
# type: () -> str
23+
def __repr__(self) -> str:
2624
return "<InstallationCandidate({!r}, {!r}, {!r})>".format(
2725
self.name, self.version, self.link,
2826
)
2927

30-
def __str__(self):
31-
# type: () -> str
28+
def __str__(self) -> str:
3229
return '{!r} candidate (version {} at {})'.format(
3330
self.name, self.version, self.link,
3431
)

src/pip/_internal/models/direct_url.py

Lines changed: 35 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ class DirectUrlValidationError(Exception):
2222
pass
2323

2424

25-
def _get(d, expected_type, key, default=None):
26-
# type: (Dict[str, Any], Type[T], str, Optional[T]) -> Optional[T]
25+
def _get(
26+
d: Dict[str, Any], expected_type: Type[T], key: str, default: Optional[T] = None
27+
) -> Optional[T]:
2728
"""Get value from dictionary and verify expected type."""
2829
if key not in d:
2930
return default
@@ -37,16 +38,16 @@ def _get(d, expected_type, key, default=None):
3738
return value
3839

3940

40-
def _get_required(d, expected_type, key, default=None):
41-
# type: (Dict[str, Any], Type[T], str, Optional[T]) -> T
41+
def _get_required(
42+
d: Dict[str, Any], expected_type: Type[T], key: str, default: Optional[T] = None
43+
) -> T:
4244
value = _get(d, expected_type, key, default)
4345
if value is None:
4446
raise DirectUrlValidationError(f"{key} must have a value")
4547
return value
4648

4749

48-
def _exactly_one_of(infos):
49-
# type: (Iterable[Optional[InfoType]]) -> InfoType
50+
def _exactly_one_of(infos: Iterable[Optional["InfoType"]]) -> "InfoType":
5051
infos = [info for info in infos if info is not None]
5152
if not infos:
5253
raise DirectUrlValidationError(
@@ -60,8 +61,7 @@ def _exactly_one_of(infos):
6061
return infos[0]
6162

6263

63-
def _filter_none(**kwargs):
64-
# type: (Any) -> Dict[str, Any]
64+
def _filter_none(**kwargs: Any) -> Dict[str, Any]:
6565
"""Make dict excluding None values."""
6666
return {k: v for k, v in kwargs.items() if v is not None}
6767

@@ -71,21 +71,20 @@ class VcsInfo:
7171

7272
def __init__(
7373
self,
74-
vcs, # type: str
75-
commit_id, # type: str
76-
requested_revision=None, # type: Optional[str]
77-
resolved_revision=None, # type: Optional[str]
78-
resolved_revision_type=None, # type: Optional[str]
79-
):
74+
vcs: str,
75+
commit_id: str,
76+
requested_revision: Optional[str] = None,
77+
resolved_revision: Optional[str] = None,
78+
resolved_revision_type: Optional[str] = None,
79+
) -> None:
8080
self.vcs = vcs
8181
self.requested_revision = requested_revision
8282
self.commit_id = commit_id
8383
self.resolved_revision = resolved_revision
8484
self.resolved_revision_type = resolved_revision_type
8585

8686
@classmethod
87-
def _from_dict(cls, d):
88-
# type: (Optional[Dict[str, Any]]) -> Optional[VcsInfo]
87+
def _from_dict(cls, d: Optional[Dict[str, Any]]) -> Optional["VcsInfo"]:
8988
if d is None:
9089
return None
9190
return cls(
@@ -96,8 +95,7 @@ def _from_dict(cls, d):
9695
resolved_revision_type=_get(d, str, "resolved_revision_type"),
9796
)
9897

99-
def _to_dict(self):
100-
# type: () -> Dict[str, Any]
98+
def _to_dict(self) -> Dict[str, Any]:
10199
return _filter_none(
102100
vcs=self.vcs,
103101
requested_revision=self.requested_revision,
@@ -112,19 +110,17 @@ class ArchiveInfo:
112110

113111
def __init__(
114112
self,
115-
hash=None, # type: Optional[str]
116-
):
113+
hash: Optional[str] = None,
114+
) -> None:
117115
self.hash = hash
118116

119117
@classmethod
120-
def _from_dict(cls, d):
121-
# type: (Optional[Dict[str, Any]]) -> Optional[ArchiveInfo]
118+
def _from_dict(cls, d: Optional[Dict[str, Any]]) -> Optional["ArchiveInfo"]:
122119
if d is None:
123120
return None
124121
return cls(hash=_get(d, str, "hash"))
125122

126-
def _to_dict(self):
127-
# type: () -> Dict[str, Any]
123+
def _to_dict(self) -> Dict[str, Any]:
128124
return _filter_none(hash=self.hash)
129125

130126

@@ -133,21 +129,19 @@ class DirInfo:
133129

134130
def __init__(
135131
self,
136-
editable=False, # type: bool
137-
):
132+
editable: bool = False,
133+
) -> None:
138134
self.editable = editable
139135

140136
@classmethod
141-
def _from_dict(cls, d):
142-
# type: (Optional[Dict[str, Any]]) -> Optional[DirInfo]
137+
def _from_dict(cls, d: Optional[Dict[str, Any]]) -> Optional["DirInfo"]:
143138
if d is None:
144139
return None
145140
return cls(
146141
editable=_get_required(d, bool, "editable", default=False)
147142
)
148143

149-
def _to_dict(self):
150-
# type: () -> Dict[str, Any]
144+
def _to_dict(self) -> Dict[str, Any]:
151145
return _filter_none(editable=self.editable or None)
152146

153147

@@ -158,16 +152,15 @@ class DirectUrl:
158152

159153
def __init__(
160154
self,
161-
url, # type: str
162-
info, # type: InfoType
163-
subdirectory=None, # type: Optional[str]
164-
):
155+
url: str,
156+
info: InfoType,
157+
subdirectory: Optional[str] = None,
158+
) -> None:
165159
self.url = url
166160
self.info = info
167161
self.subdirectory = subdirectory
168162

169-
def _remove_auth_from_netloc(self, netloc):
170-
# type: (str) -> str
163+
def _remove_auth_from_netloc(self, netloc: str) -> str:
171164
if "@" not in netloc:
172165
return netloc
173166
user_pass, netloc_no_user_pass = netloc.split("@", 1)
@@ -182,8 +175,7 @@ def _remove_auth_from_netloc(self, netloc):
182175
return netloc_no_user_pass
183176

184177
@property
185-
def redacted_url(self):
186-
# type: () -> str
178+
def redacted_url(self) -> str:
187179
"""url with user:password part removed unless it is formed with
188180
environment variables as specified in PEP 610, or it is ``git``
189181
in the case of a git URL.
@@ -195,13 +187,11 @@ def redacted_url(self):
195187
)
196188
return surl
197189

198-
def validate(self):
199-
# type: () -> None
190+
def validate(self) -> None:
200191
self.from_dict(self.to_dict())
201192

202193
@classmethod
203-
def from_dict(cls, d):
204-
# type: (Dict[str, Any]) -> DirectUrl
194+
def from_dict(cls, d: Dict[str, Any]) -> "DirectUrl":
205195
return DirectUrl(
206196
url=_get_required(d, str, "url"),
207197
subdirectory=_get(d, str, "subdirectory"),
@@ -214,8 +204,7 @@ def from_dict(cls, d):
214204
),
215205
)
216206

217-
def to_dict(self):
218-
# type: () -> Dict[str, Any]
207+
def to_dict(self) -> Dict[str, Any]:
219208
res = _filter_none(
220209
url=self.redacted_url,
221210
subdirectory=self.subdirectory,
@@ -224,10 +213,8 @@ def to_dict(self):
224213
return res
225214

226215
@classmethod
227-
def from_json(cls, s):
228-
# type: (str) -> DirectUrl
216+
def from_json(cls, s: str) -> "DirectUrl":
229217
return cls.from_dict(json.loads(s))
230218

231-
def to_json(self):
232-
# type: () -> str
219+
def to_json(self) -> str:
233220
return json.dumps(self.to_dict(), sort_keys=True)

src/pip/_internal/models/format_control.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@ class FormatControl:
1111

1212
__slots__ = ["no_binary", "only_binary"]
1313

14-
def __init__(self, no_binary=None, only_binary=None):
15-
# type: (Optional[Set[str]], Optional[Set[str]]) -> None
14+
def __init__(
15+
self,
16+
no_binary: Optional[Set[str]] = None,
17+
only_binary: Optional[Set[str]] = None
18+
) -> None:
1619
if no_binary is None:
1720
no_binary = set()
1821
if only_binary is None:
@@ -21,8 +24,7 @@ def __init__(self, no_binary=None, only_binary=None):
2124
self.no_binary = no_binary
2225
self.only_binary = only_binary
2326

24-
def __eq__(self, other):
25-
# type: (object) -> bool
27+
def __eq__(self, other: object) -> bool:
2628
if not isinstance(other, self.__class__):
2729
return NotImplemented
2830

@@ -34,17 +36,15 @@ def __eq__(self, other):
3436
for k in self.__slots__
3537
)
3638

37-
def __repr__(self):
38-
# type: () -> str
39+
def __repr__(self) -> str:
3940
return "{}({}, {})".format(
4041
self.__class__.__name__,
4142
self.no_binary,
4243
self.only_binary
4344
)
4445

4546
@staticmethod
46-
def handle_mutual_excludes(value, target, other):
47-
# type: (str, Set[str], Set[str]) -> None
47+
def handle_mutual_excludes(value: str, target: Set[str], other: Set[str]) -> None:
4848
if value.startswith('-'):
4949
raise CommandError(
5050
"--no-binary / --only-binary option requires 1 argument."
@@ -66,8 +66,7 @@ def handle_mutual_excludes(value, target, other):
6666
other.discard(name)
6767
target.add(name)
6868

69-
def get_allowed_formats(self, canonical_name):
70-
# type: (str) -> FrozenSet[str]
69+
def get_allowed_formats(self, canonical_name: str) -> FrozenSet[str]:
7170
result = {"binary", "source"}
7271
if canonical_name in self.only_binary:
7372
result.discard('source')
@@ -79,8 +78,7 @@ def get_allowed_formats(self, canonical_name):
7978
result.discard('binary')
8079
return frozenset(result)
8180

82-
def disallow_binaries(self):
83-
# type: () -> None
81+
def disallow_binaries(self) -> None:
8482
self.handle_mutual_excludes(
8583
':all:', self.no_binary, self.only_binary,
8684
)

src/pip/_internal/models/index.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ class PackageIndex:
88
__slots__ = ['url', 'netloc', 'simple_url', 'pypi_url',
99
'file_storage_domain']
1010

11-
def __init__(self, url, file_storage_domain):
12-
# type: (str, str) -> None
11+
def __init__(self, url: str, file_storage_domain: str) -> None:
1312
super().__init__()
1413
self.url = url
1514
self.netloc = urllib.parse.urlsplit(url).netloc
@@ -21,8 +20,7 @@ def __init__(self, url, file_storage_domain):
2120
# block such packages themselves
2221
self.file_storage_domain = file_storage_domain
2322

24-
def _url_for_path(self, path):
25-
# type: (str) -> str
23+
def _url_for_path(self, path: str) -> str:
2624
return urllib.parse.urljoin(self.url, path)
2725

2826

0 commit comments

Comments
 (0)