From bb87ba300f483bfcf7aad639811a3b732ccb2c5a Mon Sep 17 00:00:00 2001 From: "Uwe L. Korn" Date: Mon, 15 Jan 2024 20:59:02 +0100 Subject: [PATCH 1/2] Support Python-version-specific deps in poetry --- grayskull/strategy/py_toml.py | 34 ++++++++++++++++++++++++++-------- tests/data/poetry/poetry.toml | 4 ++++ tests/test_poetry.py | 2 ++ 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/grayskull/strategy/py_toml.py b/grayskull/strategy/py_toml.py index 4a27b15f3..b0df790ad 100644 --- a/grayskull/strategy/py_toml.py +++ b/grayskull/strategy/py_toml.py @@ -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 diff --git a/tests/data/poetry/poetry.toml b/tests/data/poetry/poetry.toml index 9756a8ab6..cdc60d811 100644 --- a/tests/data/poetry/poetry.toml +++ b/tests/data/poetry/poetry.toml @@ -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" diff --git a/tests/test_poetry.py b/tests/test_poetry.py index 24227ff4e..06031a1c1 100644 --- a/tests/test_poetry.py +++ b/tests/test_poetry.py @@ -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]", ] From aeb718c811118bb9ff05cfb0d0cd35b0efbe2beb Mon Sep 17 00:00:00 2001 From: "Uwe L. Korn" Date: Tue, 16 Jan 2024 13:25:26 +0100 Subject: [PATCH 2/2] Extend tests --- grayskull/strategy/py_toml.py | 3 +++ tests/data/poetry/langchain-expected.yaml | 11 +++++------ 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/grayskull/strategy/py_toml.py b/grayskull/strategy/py_toml.py index b0df790ad..1ef834a2c 100644 --- a/grayskull/strategy/py_toml.py +++ b/grayskull/strategy/py_toml.py @@ -172,6 +172,9 @@ def __get_constrained_dep_dict(dep_spec: dict, dep_name: str): ) 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']}" diff --git a/tests/data/poetry/langchain-expected.yaml b/tests/data/poetry/langchain-expected.yaml index 3ecdd157b..32e45d409 100644 --- a/tests/data/poetry/langchain-expected.yaml +++ b/tests/data/poetry/langchain-expected.yaml @@ -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 @@ -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