Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Python-version-specific deps in poetry #523

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 29 additions & 8 deletions grayskull/strategy/py_toml.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,25 +164,46 @@ 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)}]"
elif m := re.match(r"\^(\d)\.(\d+)", dep_spec["python"]):
next_major = (int(m.group(1)) + 1) * 100
python_selector = f" # [py>={m.group(1)}{m.group(2)} and py<{next_major}]"
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
11 changes: 5 additions & 6 deletions tests/data/poetry/langchain-expected.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,16 @@ source:
build:
entry_points:
- langchain-server = langchain.server:main
noarch: python
script: {{ PYTHON }} -m pip install . -vv --no-deps --no-build-isolation
number: 0

requirements:
host:
- python >=3.8,<4.0
- python
- poetry-core
- pip
run:
- python >=3.8.1,<4.0
- python
- pydantic >=1.0.0,<2.0.0
- sqlalchemy >=1.0.0,<2.0.0
- requests >=2.0.0,<3.0.0
Expand All @@ -44,14 +43,14 @@ requirements:
- beautifulsoup4 >=4.0.0,<5.0.0
- pytorch >=1.0.0,<2.0.0
- jinja2 >=3.0.0,<4.0.0
- tiktoken >=0.0.0,<1.0.0
- tiktoken >=0.0.0,<1.0.0 # [py>=39 and py<400]
- pinecone-client >=2.0.0,<3.0.0
- weaviate-client >=3.0.0,<4.0.0
- google-api-python-client 2.70.0
- wolframalpha 5.0.0
- anthropic >=0.2.2,<0.3.0
- qdrant-client >=1.0.4,<2.0.0
- tensorflow-text >=2.11.0,<3.0.0
- qdrant-client >=1.0.4,<2.0.0 # [py>=38]
- tensorflow-text >=2.11.0,<3.0.0 # [py>=310 and py<400]
- cohere >=3.0.0,<4.0.0
- openai >=0.0.0,<1.0.0
- nlpcloud >=1.0.0,<2.0.0
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
Loading