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

fix: use modern PEP 723 syntax, fix mistake in name #1180

Merged
merged 5 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
18 changes: 16 additions & 2 deletions src/pipx/commands/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,14 +328,28 @@ def _get_requirements_from_script(content: str) -> Optional[List[str]]:
Supports inline script metadata.
"""

name = "pyproject"
name = "script"

# Windows is currently getting un-normalized line endings, so normalize
content = content.replace("\r\n", "\n")

matches = [m for m in INLINE_SCRIPT_METADATA.finditer(content) if m.group("type") == name]

if not matches:
pyproject_matches = [m for m in INLINE_SCRIPT_METADATA.finditer(content) if m.group("type") == "pyproject"]
if pyproject_matches:
logger.error(
pipx_wrap(
f"""
{hazard} Using old form of requirements table. Use updated PEP
723 syntax by replacing `# /// pyproject` with `# /// script`
and `run.dependencies` (or `run.requirements`) with
`dependencies`.
""",
subsequent_indent=" " * 4,
)
)
raise ValueError("Old 'pyproject' table found")
return None

if len(matches) > 1:
Expand All @@ -348,7 +362,7 @@ def _get_requirements_from_script(content: str) -> Optional[List[str]]:
pyproject = tomllib.loads(content)

requirements = []
for requirement in pyproject.get("run", {}).get("requirements", []):
for requirement in pyproject.get("dependencies", []):
# Validate the requirement
try:
req = Requirement(requirement)
Expand Down
38 changes: 32 additions & 6 deletions tests/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ def test_run_with_requirements(caplog, pipx_temp_env, tmp_path):
script.write_text(
textwrap.dedent(
f"""
# /// pyproject
# run.requirements = ["requests==2.31.0"]
# /// script
# dependencies = ["requests==2.31.0"]
# ///

# Check requests can be imported
Expand All @@ -217,6 +217,32 @@ def test_run_with_requirements(caplog, pipx_temp_env, tmp_path):
assert out.read_text() == "2.31.0"


@mock.patch("os.execvpe", new=execvpe_mock)
def test_run_with_requirements_old(caplog, pipx_temp_env, tmp_path):
script = tmp_path / "test.py"
out = tmp_path / "output.txt"
script.write_text(
textwrap.dedent(
f"""
# /// pyproject
# run.requirements = ["requests==2.31.0"]
# ///

# Check requests can be imported
import requests
# Check dependencies of requests can be imported
import certifi
# Check the installed version
from pathlib import Path
Path({repr(str(out))}).write_text(requests.__version__)
"""
).strip(),
encoding="utf-8",
)
with pytest.raises(ValueError):
run_pipx_cli_exit(["run", script.as_uri()])


@mock.patch("os.execvpe", new=execvpe_mock)
def test_run_with_args(caplog, pipx_temp_env, tmp_path):
script = tmp_path / "test.py"
Expand All @@ -241,8 +267,8 @@ def test_run_with_requirements_and_args(caplog, pipx_temp_env, tmp_path):
script.write_text(
textwrap.dedent(
f"""
# /// pyproject
# run.requirements = ["packaging"]
# /// script
# dependencies = ["packaging"]
# ///
import packaging
import sys
Expand All @@ -261,8 +287,8 @@ def test_run_with_invalid_requirement(capsys, pipx_temp_env, tmp_path):
script.write_text(
textwrap.dedent(
"""
# /// pyproject
# run.requirements = ["this is an invalid requirement"]
# /// script
# dependencies = ["this is an invalid requirement"]
# ///
print()
"""
Expand Down
Loading