-
Notifications
You must be signed in to change notification settings - Fork 256
/
Copy pathhelpers.py
64 lines (50 loc) · 1.55 KB
/
helpers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from __future__ import annotations
from typing import TYPE_CHECKING
from poetry.core.semver.helpers import parse_constraint
from poetry.core.semver.version import Version
from poetry.core.semver.version_union import VersionUnion
if TYPE_CHECKING:
from poetry.core.semver.version_constraint import VersionConstraint
PYTHON_VERSION = [
"2.7.*",
"3.0.*",
"3.1.*",
"3.2.*",
"3.3.*",
"3.4.*",
"3.5.*",
"3.6.*",
"3.7.*",
"3.8.*",
"3.9.*",
"3.10.*",
]
def format_python_constraint(constraint: VersionConstraint) -> str:
"""
This helper will help in transforming
disjunctive constraint into proper constraint.
"""
if isinstance(constraint, Version):
if constraint.precision >= 3:
return f"=={str(constraint)}"
# Transform 3.6 or 3
if constraint.precision == 2:
# 3.6
constraint = parse_constraint(f"~{constraint.major}.{constraint.minor}")
else:
constraint = parse_constraint(f"^{constraint.major}.0")
if not isinstance(constraint, VersionUnion):
return str(constraint)
formatted = []
accepted = []
for version in PYTHON_VERSION:
version_constraint = parse_constraint(version)
matches = constraint.allows_any(version_constraint)
if not matches:
formatted.append("!=" + version)
else:
accepted.append(version)
# Checking lower bound
low = accepted[0]
formatted.insert(0, ">=" + ".".join(low.split(".")[:2]))
return ", ".join(formatted)