8
8
9
9
10
10
class VersionedObject (_Common ):
11
- """Definition of a versioned object, eg " foo-1.0" .
11
+ """Definition of a versioned object, eg `` foo-1.0`` .
12
12
13
- " foo" is also a valid object definiton - when there is no version part, we
13
+ `` foo`` is also a valid object definiton. When there is no version part, we
14
14
are defining an unversioned object.
15
15
16
- Note that '-', '@' or '#' can be used as the seperator between object name
17
- and version, however this is purely cosmetic - "foo-1" is the same as "foo@1".
16
+ .. note::
17
+ Note that ``-``, ``@`` or ``#`` can be used as the seperator between object name
18
+ and version, however this is purely cosmetic. ``foo-1`` is the same as ``foo@1``.
18
19
"""
19
20
sep_regex_str = r'[-@#]'
20
21
sep_regex = re .compile (sep_regex_str )
21
22
22
23
def __init__ (self , s ):
24
+ """
25
+ Args:
26
+ s (str):
27
+ """
23
28
self .name_ = None
24
29
self .version_ = None
25
30
self .sep_ = '-'
@@ -42,8 +47,8 @@ def construct(cls, name, version=None):
42
47
"""Create a VersionedObject directly from an object name and version.
43
48
44
49
Args:
45
- name: Object name string.
46
- version: Version object.
50
+ name (str) : Object name string.
51
+ version (typing.Optional[Version]) : Version object.
47
52
"""
48
53
other = VersionedObject (None )
49
54
other .name_ = name
@@ -52,19 +57,27 @@ def construct(cls, name, version=None):
52
57
53
58
@property
54
59
def name (self ):
55
- """Name of the object."""
60
+ """Name of the object.
61
+
62
+ Returns:
63
+ str:
64
+ """
56
65
return self .name_
57
66
58
67
@property
59
68
def version (self ):
60
- """Version of the object."""
69
+ """Version of the object.
70
+
71
+ Returns:
72
+ Version:
73
+ """
61
74
return self .version_
62
75
63
76
def as_exact_requirement (self ):
64
77
"""Get the versioned object, as an exact requirement string.
65
78
66
79
Returns:
67
- Equivalent requirement string, eg " maya==2016.1"
80
+ str: Equivalent requirement string, eg `` maya==2016.1``
68
81
"""
69
82
sep_str = ''
70
83
ver_str = ''
@@ -91,42 +104,46 @@ def __str__(self):
91
104
92
105
93
106
class Requirement (_Common ):
94
- """Requirement for a versioned object.
95
-
96
- Examples of valid requirement strings:
97
-
98
- foo-1.0
99
-
100
- foo#1.0
101
- foo-1+
102
- foo-1+<4.3
103
- foo<3
104
- foo==1.0.1
105
-
106
- Defines a requirement for an object. For example, "foo-5+" means that you
107
- require any version of "foo", version 5 or greater. An unversioned
108
- requirement can also be used ("foo"), this means you require any version of
107
+ """
108
+ Defines a requirement for an object. For example, ``foo-5+`` means that you
109
+ require any version of ``foo``, version 5 or greater. An unversioned
110
+ requirement can also be used (``foo``), this means you require any version of
109
111
foo. You can drop the hyphen between object name and version range if the
110
- version range starts with a non-alphanumeric character - eg " foo<2" .
112
+ version range starts with a non-alphanumeric character - eg `` foo<2`` .
111
113
112
114
There are two different prefixes that can be applied to a requirement:
113
115
114
- - "!" : The conflict requirement. This means that you require this version
116
+ - ``!`` : The conflict requirement. This means that you require this version
115
117
range of an object NOT to be present. To conflict with all versions of an
116
118
object, use "!foo".
117
-
118
- - "~": This is known as a "weak reference", and means, "I do not require this
119
+ - ``~``: This is known as a "weak reference", and means, "I do not require this
119
120
object, but if present, it must be within this range." It is equivalent to
120
121
the *conflict of the inverse* of the given version range.
121
122
122
- There is one subtle case to be aware of. " ~foo" is a requirement that has no
123
- effect - ie, it means "I do not require foo, but if foo is present, it can
123
+ There is one subtle case to be aware of. `` ~foo`` is a requirement that has no
124
+ effect. It means "I do not require foo, but if foo is present, it can
124
125
be any version." This statement is still valid, but will produce a
125
126
Requirement object with a None range.
127
+
128
+ Examples of valid requirement strings:
129
+
130
+ - ``foo-1.0``
131
+
132
+ - ``foo#1.0``
133
+ - ``foo-1+``
134
+ - ``foo-1+<4.3``
135
+ - ``foo<3``
136
+ - ``foo==1.0.1``
126
137
"""
127
138
sep_regex = re .compile (r'[-@#=<>]' )
128
139
129
140
def __init__ (self , s , invalid_bound_error = True ):
141
+ """
142
+ Args:
143
+ s (str): Requirement string
144
+ invalid_bound_error (bool): If True, raise :exc:`VersionError` if an
145
+ impossible range is given, such as ``3+<2``.
146
+ """
130
147
self .name_ = None
131
148
self .range_ = None
132
149
self .negate_ = False
@@ -170,8 +187,8 @@ def construct(cls, name, range=None):
170
187
"""Create a requirement directly from an object name and VersionRange.
171
188
172
189
Args:
173
- name: Object name string.
174
- range: VersionRange object. If None, an unversioned requirement is
190
+ name (str) : Object name string.
191
+ range (typing.Optional[VersionRange]): If None, an unversioned requirement is
175
192
created.
176
193
"""
177
194
other = Requirement (None )
@@ -181,38 +198,61 @@ def construct(cls, name, range=None):
181
198
182
199
@property
183
200
def name (self ):
184
- """Name of the required object."""
201
+ """Name of the required object.
202
+
203
+ Returns:
204
+ str:
205
+ """
185
206
return self .name_
186
207
187
208
@property
188
209
def range (self ):
189
- """VersionRange of the requirement."""
210
+ """Version range of the requirement.
211
+
212
+ Returns:
213
+ VersionRange:
214
+ """
190
215
return self .range_
191
216
192
217
@property
193
218
def conflict (self ):
194
219
"""True if the requirement is a conflict requirement, eg "!foo", "~foo-1".
220
+
221
+ Returns:
222
+ bool:
195
223
"""
196
224
return self .conflict_
197
225
198
226
@property
199
227
def weak (self ):
200
228
"""True if the requirement is weak, eg "~foo".
201
229
202
- Note that weak requirements are also conflict requirements, but not
203
- necessarily the other way around.
230
+ .. note::
231
+ Note that weak requirements are also conflict requirements, but not
232
+ necessarily the other way around.
233
+
234
+ Returns:
235
+ bool:
204
236
"""
205
237
return self .negate_
206
238
207
239
def safe_str (self ):
208
240
"""Return a string representation that is safe for the current filesystem,
209
241
and guarantees that no two different Requirement objects will encode to
210
- the same value."""
242
+ the same value.
243
+
244
+ Returns:
245
+ str:
246
+ """
211
247
return str (self )
212
248
213
249
def conflicts_with (self , other ):
214
- """Returns True if this requirement conflicts with another `Requirement`
215
- or `VersionedObject`."""
250
+ """Returns True if this requirement conflicts with another :class:`Requirement`
251
+ or :class:`VersionedObject`.
252
+
253
+ Returns:
254
+ bool:
255
+ """
216
256
if isinstance (other , Requirement ):
217
257
if (self .name_ != other .name_ ) or (self .range is None ) \
218
258
or (other .range is None ):
@@ -233,16 +273,20 @@ def conflicts_with(self, other):
233
273
return (other .version_ not in self .range_ )
234
274
235
275
def merged (self , other ):
236
- """Returns the merged result of two requirements.
276
+ """Merge two requirements.
237
277
238
278
Two requirements can be in conflict and if so, this function returns
239
- None. For example, requests for " foo-4" and " foo-6" are in conflict,
279
+ None. For example, requests for `` foo-4`` and `` foo-6`` are in conflict,
240
280
since both cannot be satisfied with a single version of foo.
241
281
242
282
Some example successful requirements merges are:
243
- - "foo-3+" and "!foo-5+" == "foo-3+<5"
244
- - "foo-1" and "foo-1.5" == "foo-1.5"
245
- - "!foo-2" and "!foo-5" == "!foo-2|5"
283
+
284
+ - ``foo-3+`` and ``!foo-5+`` == ``foo-3+<5``
285
+ - ``foo-1`` and ``foo-1.5`` == ``foo-1.5``
286
+ - ``!foo-2`` and ``!foo-5`` == ``!foo-2|5``
287
+
288
+ Returns:
289
+ Requirement: the merged result of two requirements.
246
290
"""
247
291
if self .name_ != other .name_ :
248
292
return None # cannot merge across object names
@@ -327,10 +371,9 @@ class RequirementList(_Common):
327
371
is retained.
328
372
"""
329
373
def __init__ (self , requirements ):
330
- """Create a RequirementList.
331
-
374
+ """
332
375
Args:
333
- requirements: List of Requirement objects .
376
+ requirements (list[Requirement]) : List of requirements .
334
377
"""
335
378
self .requirements_ = []
336
379
self .conflict_ = None
@@ -370,6 +413,9 @@ def __init__(self, requirements):
370
413
def requirements (self ):
371
414
"""Returns optimised list of requirements, or None if there are
372
415
conflicts.
416
+
417
+ Returns:
418
+ list[Requirement]:
373
419
"""
374
420
return self .requirements_
375
421
@@ -378,28 +424,41 @@ def conflict(self):
378
424
"""Get the requirement conflict, if any.
379
425
380
426
Returns:
381
- None if there is no conflict, otherwise a 2-tuple containing the
382
- conflicting Requirement objects.
427
+ typing.Optional[tuple[Requirement]]: None if there is no conflict, otherwise a
428
+ 2-tuple containing the conflicting requirement objects.
383
429
"""
384
430
return self .conflict_
385
431
386
432
@property
387
433
def names (self ):
388
434
"""Set of names of requirements, not including conflict requirements.
435
+
436
+ Returns:
437
+ set[str]:
389
438
"""
390
439
return self .names_
391
440
392
441
@property
393
442
def conflict_names (self ):
394
- """Set of conflict requirement names."""
443
+ """Set of conflict requirement names.
444
+
445
+ Returns:
446
+ set[str]:
447
+ """
395
448
return self .conflict_names_
396
449
397
450
def __iter__ (self ):
398
451
for requirement in (self .requirements_ or []):
399
452
yield requirement
400
453
401
454
def get (self , name ):
402
- """Returns the Requirement for the given object, or None.
455
+ """Returns the requirement for the given object, or None.
456
+
457
+ Args:
458
+ name (str): requirement to get.
459
+
460
+ Returns:
461
+ Requirement:
403
462
"""
404
463
return self .requirements_dict .get (name )
405
464
0 commit comments