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

feat: add hint for new noarch: python syntax #2115

Merged
merged 9 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
25 changes: 25 additions & 0 deletions conda_smithy/lint_recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from conda_smithy.linter import conda_recipe_v1_linter
from conda_smithy.linter.hints import (
hint_check_spdx,
hint_noarch_python_use_python_min,
hint_pip_no_build_backend,
hint_pip_usage,
hint_shellcheck_usage,
Expand Down Expand Up @@ -469,6 +470,20 @@ def run_conda_forge_specific(
meta, "outputs", lints, recipe_version=recipe_version
)

build_section = get_section(meta, "build", lints, recipe_version)
noarch_value = build_section.get("noarch")

if recipe_version == 1:
test_section = get_section(meta, "tests", lints, recipe_version)
test_reqs = []
for test_element in test_section:
test_reqs += (test_element.get("requirements") or {}).get(
"run"
) or []
else:
test_section = get_section(meta, "test", lints, recipe_version)
test_reqs = test_section.get("requires") or []

# Fetch list of recipe maintainers
maintainers = extra_section.get("recipe-maintainers", [])

Expand Down Expand Up @@ -580,6 +595,16 @@ def run_conda_forge_specific(
"The ``conda-forge.yml`` file is not allowed to have duplicate keys."
)

# 10: check for proper noarch python syntax
hint_noarch_python_use_python_min(
requirements_section.get("host") or [],
requirements_section.get("run") or [],
test_reqs,
outputs_section,
noarch_value,
hints,
)


def _format_validation_msg(error: jsonschema.ValidationError):
"""Use the data on the validation error to generate improved reporting.
Expand Down
26 changes: 26 additions & 0 deletions conda_smithy/linter/hints.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,29 @@ def hint_pip_no_build_backend(host_or_build_section, package_name, hints):
"If your recipe has built with only `pip` in the `host` section in the past, you likely should "
"add `setuptools` to the `host` section of your recipe."
)


def hint_noarch_python_use_python_min(
host_reqs, run_reqs, test_reqs, outputs_section, noarch_value, hints
):
if noarch_value == "python" and not outputs_section:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't hint multi output recipes? 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not currently no. IDK why.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@isuruf added similar code in b7514aa

There's no PR attached to that blame so I'm not sure where it came from or whether there was some discussion. We should probably make it compatible with multi output recipes but I won't block if you consider it's out of scope.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is out of scope for this PR given that it is already in the code base.

for section_name, syntax, reqs in [
("host", "python {{ python_min }}.*", host_reqs),
("run", "python >={{ python_min }}", run_reqs),
("test.requires", "python ={{ python_min }}", test_reqs),
]:
for req in reqs:
if (
req.strip().split()[0] == "python"
and req != "python"
and syntax in req
):
break
else:
hints.append(
f"noarch: python recipes should almost always follow the syntax in "
f"[CFEP-25](https://conda-forge.org/docs/maintainer/knowledge_base/#noarch-python). "
beckermr marked this conversation as resolved.
Show resolved Hide resolved
f"For the `{section_name}` section of the recipe, you should almost always use `{syntax}` "
f"for the `python` entry. You may need to override the `python_min` variable if the package "
f"requires a newer Python version than the currently supported minimum version on `conda-forge`."
)
23 changes: 23 additions & 0 deletions news/2115-noarch-python-hints.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
**Added:**

* Added hints for new ``noarch: python`` syntax for CFEP-25. (#2115)

**Changed:**

* <news item>

**Deprecated:**

* <news item>

**Removed:**

* <news item>

**Fixed:**

* <news item>

**Security:**

* <news item>
188 changes: 188 additions & 0 deletions tests/test_lint_recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -3002,5 +3002,193 @@ def test_hint_pip_no_build_backend(
), lints


@pytest.mark.parametrize(
"meta_str,expected_hints",
[
(
textwrap.dedent(
"""
package:
name: python

requirements:
run:
- python
"""
),
[],
),
(
textwrap.dedent(
"""
package:
name: python

build:
noarch: python

requirements:
run:
- python
"""
),
[
"python {{ python_min }}.*",
"python >={{ python_min }}",
"python ={{ python_min }}",
],
),
(
textwrap.dedent(
"""
package:
name: python

build:
noarch: python

requirements:
host:
- python
"""
),
[
"python {{ python_min }}.*",
"python >={{ python_min }}",
"python ={{ python_min }}",
],
),
(
textwrap.dedent(
"""
package:
name: python

build:
noarch: python

test:
requires:
- python
"""
),
[
"python {{ python_min }}.*",
"python >={{ python_min }}",
"python ={{ python_min }}",
],
),
(
textwrap.dedent(
"""
package:
name: python

build:
noarch: python

requirements:
run:
- python >={{ python_min }}
"""
),
[
"python {{ python_min }}.*",
"python ={{ python_min }}",
],
),
(
textwrap.dedent(
"""
package:
name: python

build:
noarch: python

requirements:
host:
- python {{ python_min }}.*
run:
- python >={{ python_min }}
"""
),
[
"python ={{ python_min }}",
],
),
(
textwrap.dedent(
"""
package:
name: python

build:
noarch: python

requirements:
host:
- python {{ python_min }}.*
run:
- python >={{ python_min }}

test:
requires:
- python ={{ python_min }}
"""
),
[],
),
(
textwrap.dedent(
"""
package:
name: python

build:
noarch: python

requirements:
host:
- python {{ python_min }}.*
run:
- python

test:
requires:
- python ={{ python_min }}
"""
),
["python >={{ python_min }}"],
),
],
)
def test_hint_noarch_python_use_python_min(
meta_str,
expected_hints,
):
meta = get_yaml().load(meta_str)
lints = []
hints = []
linter.run_conda_forge_specific(
meta,
None,
lints,
hints,
recipe_version=0,
)

# make sure we have the expected hints
if expected_hints:
for expected_hint in expected_hints:
assert any(expected_hint in hint for hint in hints), hints
else:
assert all(
"noarch: python recipes should almost always follow the syntax in"
not in hint
for hint in hints
)


if __name__ == "__main__":
unittest.main()
Loading