@@ -480,7 +480,7 @@ def from_native(cls, string):
480
480
return cls (constraints = constraints )
481
481
482
482
483
- def split_req (string , comparators , comparators_rear = {}, default = None , strip = "" ):
483
+ def split_req (string , comparators , default = None , strip = "" ):
484
484
"""
485
485
Return a tuple of (vers comparator, version) strings given an common version
486
486
requirement``string`` such as "> 2.3" or "<= 2.3" using the ``comparators``
@@ -501,8 +501,6 @@ def split_req(string, comparators, comparators_rear={}, default=None, strip=""):
501
501
>>> assert split_req(">= 2.3", comparators=comps) == (">=", "2.3",)
502
502
>>> assert split_req("<= 2.3", comparators=comps) == ("<=", "2.3",)
503
503
>>> assert split_req("(< = 2.3 )", comparators=comps, strip=")(") == ("<=", "2.3",)
504
- >>> comps_rear = {")": "<", "]": "<="}
505
- >>> assert split_req(" 2.3 ]", comparators=comps, comparators_rear=comps_rear) == ("<=", "2.3",)
506
504
507
505
With a default, we return the default comparator::
508
506
@@ -523,12 +521,6 @@ def split_req(string, comparators, comparators_rear={}, default=None, strip=""):
523
521
version = constraint_string .lstrip (native_comparator )
524
522
return vers_comparator , version
525
523
526
- # Some bracket notation comparators starts from end.
527
- for native_comparator , vers_comparator in comparators_rear .items ():
528
- if constraint_string .endswith (native_comparator ):
529
- version = constraint_string .rstrip (native_comparator )
530
- return vers_comparator , version
531
-
532
524
if default :
533
525
return default , constraint_string
534
526
@@ -1300,14 +1292,35 @@ def build_range_from_github_advisory_constraint(scheme: str, string: Union[str,
1300
1292
">=" : ">=" ,
1301
1293
"<" : "<" ,
1302
1294
">" : ">" ,
1303
- "(" : ">" ,
1304
- "[" : ">=" ,
1305
1295
}
1306
1296
1307
- vers_by_snyk_native_comparators_rear = {
1308
- ")" : "<" ,
1309
- "]" : "<=" ,
1310
- }
1297
+
1298
+ def split_req_bracket_notation (string ):
1299
+ """
1300
+ Return a tuple of (vers comparator, version) strings given an bracket notation
1301
+ version requirement ``string`` such as "(2.3" or "3.9]"
1302
+
1303
+ For example::
1304
+
1305
+ >>> assert split_req_bracket_notation(" 2.3 ]") == ("<=", "2.3")
1306
+ >>> assert split_req_bracket_notation("( 3.9") == (">", "3.9")
1307
+ """
1308
+ comparators_front = {"(" : ">" , "[" : ">=" }
1309
+ comparators_rear = {")" : "<" , "]" : "<=" }
1310
+
1311
+ constraint_string = remove_spaces (string ).strip ()
1312
+
1313
+ for native_comparator , vers_comparator in comparators_front .items ():
1314
+ if constraint_string .startswith (native_comparator ):
1315
+ version = constraint_string .lstrip (native_comparator )
1316
+ return vers_comparator , version
1317
+
1318
+ for native_comparator , vers_comparator in comparators_rear .items ():
1319
+ if constraint_string .endswith (native_comparator ):
1320
+ version = constraint_string .rstrip (native_comparator )
1321
+ return vers_comparator , version
1322
+
1323
+ raise ValueError (f"Unknown comparator in version requirement: { string !r} " )
1311
1324
1312
1325
1313
1326
def build_range_from_snyk_advisory_string (scheme : str , string : Union [str , List ]):
@@ -1346,11 +1359,13 @@ def build_range_from_snyk_advisory_string(scheme: str, string: Union[str, List])
1346
1359
constraints = snyk_constraints .split (" " )
1347
1360
1348
1361
for constraint in constraints :
1349
- comparator , version = split_req (
1350
- string = constraint ,
1351
- comparators = vers_by_snyk_native_comparators ,
1352
- comparators_rear = vers_by_snyk_native_comparators_rear ,
1353
- )
1362
+ if any (comp in constraint for comp in "[]()" ):
1363
+ comparator , version = split_req_bracket_notation (string = constraint )
1364
+ else :
1365
+ comparator , version = split_req (
1366
+ string = constraint ,
1367
+ comparators = vers_by_snyk_native_comparators ,
1368
+ )
1354
1369
if comparator and version :
1355
1370
version = vrc .version_class (version )
1356
1371
version_constraints .append (
@@ -1382,10 +1397,7 @@ def build_range_from_discrete_version_string(scheme: str, string: Union[str, Lis
1382
1397
string = [string ]
1383
1398
1384
1399
for item in string :
1385
- version = item .strip ().lstrip ("vV" )
1386
- if version == "0" :
1387
- continue
1388
-
1400
+ version = item .strip ()
1389
1401
version = vrc .version_class (version )
1390
1402
version_constraints .append (
1391
1403
VersionConstraint (
0 commit comments