Skip to content

feat: python tools requirement#1040

Open
akihikokuroda wants to merge 1 commit intogenerative-computing:mainfrom
akihikokuroda:pythonrequirement
Open

feat: python tools requirement#1040
akihikokuroda wants to merge 1 commit intogenerative-computing:mainfrom
akihikokuroda:pythonrequirement

Conversation

@akihikokuroda
Copy link
Copy Markdown
Member

@akihikokuroda akihikokuroda commented May 7, 2026

Requirement PR

Use this template when adding or modifying requirements in mellea/stdlib/requirements/.

Description

Add requirements for Python code generation.

Implementation Checklist

Base Class

  • Extends appropriate base class:
    • Requirement - standard requirement
    • ALoraRequirement - uses specialized Intrinsic/Adapter for generation-based validation

Validation Logic

  • validation_fn defined (if using Python-based validation)
    • re-usable functionality within the validation_fn should be separated out into mellea/stdlib/tools/
  • validate returns a ValidationResult with
    • a thunk and context if using a backend to generate
    • a specific reason and score when possible

Integration

  • Requirement exported in mellea/stdlib/requirements/__init__.py or, if you are adding a library of requirements, from your sub-module

Testing

  • Tests added to tests/requirements/
  • New code has 100% coverage
  • Ensure existing tests and github automation passes (a maintainer will kick off the github automation when the rest of the PR is populated)

Attribution

  • AI coding assistants used: claude

Signed-off-by: Akihiko Kuroda <akihikokuroda2020@gmail.com>
@akihikokuroda akihikokuroda requested a review from a team as a code owner May 7, 2026 21:22
@github-actions github-actions Bot added the enhancement New feature or request label May 7, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 7, 2026

The PR description has been updated. Please fill out the template for your PR to be reviewed.

result=False,
reason="Your code creates plots with pyplot but never calls `plt.savefig()` to save them.\n\n"
"Add this before your plotting code or at the end:\n"
" plt.savefig('{output_path}')\n"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think this should match the approach in _make_output_artifacts_validator in the way it handles path/output_path.

I assume this was intended to be a f" string instead of a " string.

"Fix this by adding to the top of your code:\n"
" import matplotlib\n"
" matplotlib.use('Agg')\n\n"
"Then replace `plt.show()` with `plt.savefig('{output_path}'); plt.close()`",
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Same as below ie

I think this should match the approach in _make_output_artifacts_validator in the way it handles path/output_path.

I assume this was intended to be a f" string instead of a " string.

return None


def _get_unauthorized_imports(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

return validate


def _make_output_limit_validator(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Does this have an associated test?

Bit hazy on the stdout and stderr here - in this case the ctx.last_output() is a ModelOutputThunk I think? Does that have stdout/err?

print("=" * 70)
print("Testing Granite 4.1's ability to repair plotting failures")
print("=" * 70)
print(f"Task: Create a plot of sin(x) and save to {output_path}\n")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

suggest using a task variable to share the task string used in actual description with here.


async def main():
"""Run the canonical plotting repair example."""
import tempfile
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

move all the imports to the top

module_name = node.module.split(".")[0]
if module_name not in allowed_imports:
unauthorized.append(module_name)
except (SyntaxError, ValueError):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

why not raise these?
This pass means that the function did not do its job.

Most likely this code is unusable elsewhere anyway, but if that external assumption is true it just snuck past the unauthorized import check.

headless_backends = ("Agg", "Svg", "Cairo", "PDF", "PS", "WebAgg", "nbAgg")
for backend in headless_backends:
if (
f"matplotlib.use('{backend}')" in code
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

commented out code would be a false positive.

You might consider using python tokenize to strip comments.
Probably strip docstring too.

m = mellea.start_session()

# Create requirements bundle for plotting validation
# Allows matplotlib import (no output_path = skip file creation check)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This example is too confusing. It always fail w/o output_path because there is a bunch of code to ensure that the output file exists. Then I add output_path and still consistently fail to get a file.

This might be intentional (fail to write file), but if so it needs to be clearer because right now it looks like a bad example that needs fixing/debugging.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Requirements library for the Python tool

3 participants