Skip to content

Commit

Permalink
Support Python-version-specific deps in poetry
Browse files Browse the repository at this point in the history
  • Loading branch information
xhochy committed Jan 15, 2024
1 parent 96b9790 commit bb87ba3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
34 changes: 26 additions & 8 deletions grayskull/strategy/py_toml.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,25 +164,43 @@ def get_constrained_dep(dep_spec, dep_name):
@get_constrained_dep.register
def __get_constrained_dep_dict(dep_spec: dict, dep_name: str):
conda_version = encode_poetry_version(dep_spec.get("version", ""))
return f"{dep_name} {conda_version}".strip()
python_selector = ""
if "python" in dep_spec:
if m := re.match(r">=(\d)\.(\d+),<(\d)\.(\d+)", dep_spec["python"]):
python_selector = (
f" # [py>={m.group(1)}{m.group(2)} and py<{m.group(3)}{m.group(4)}]"
)
elif m := re.match(r">=(\d)\.(\d+)", dep_spec["python"]):
python_selector = f" # [py>={m.group(1)}{m.group(2)}]"
else:
raise ValueError(
f"Unsupported Python version expression: {dep_spec['python']}"
)
yield f"{dep_name} {conda_version}{python_selector}".strip()


@get_constrained_dep.register
def __get_constrained_dep_list(dep_spec: list, dep_name: str):
for dep_spec_item in dep_spec:
yield from get_constrained_dep(dep_spec_item, dep_name)


@get_constrained_dep.register
def __get_constrained_dep_str(dep_spec: str, dep_name: str):
conda_version = encode_poetry_version(dep_spec)
return f"{dep_name} {conda_version}"
yield f"{dep_name} {conda_version}"


def encode_poetry_deps(poetry_deps: dict) -> Tuple[list, list]:
run = []
run_constrained = []
for dep_name, dep_spec in poetry_deps.items():
constrained_dep = get_constrained_dep(dep_spec, dep_name)
try:
assert dep_spec.get("optional", False)
run_constrained.append(constrained_dep)
except (AttributeError, AssertionError):
run.append(constrained_dep)
for constrained_dep in get_constrained_dep(dep_spec, dep_name):
try:
assert dep_spec.get("optional", False)
run_constrained.append(constrained_dep)
except (AttributeError, AssertionError):
run.append(constrained_dep)
return run, run_constrained


Expand Down
4 changes: 4 additions & 0 deletions tests/data/poetry/poetry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ python = "^3.7"
cleo = "^2.0.0"
html5lib = "^1.0"
urllib3 = "^1.26.0"
scikit-learn = [
{version="^1.0.2", python=">=3.7,<3.8"},
{version="^1.2", python=">=3.8,<3.12"}
]

[tool.poetry.group.dev.dependencies]
pre-commit = "^2.6"
Expand Down
2 changes: 2 additions & 0 deletions tests/test_poetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ def test_poetry_dependencies():
"cleo >=2.0.0,<3.0.0",
"html5lib >=1.0.0,<2.0.0",
"urllib3 >=1.26.0,<2.0.0",
"scikit-learn >=1.0.2,<2.0.0 # [py>=37 and py<38]",
"scikit-learn >=1.2.0,<2.0.0 # [py>=38 and py<312]",
]


Expand Down

0 comments on commit bb87ba3

Please sign in to comment.