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 1 commit
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
22 changes: 18 additions & 4 deletions src/pipx/commands/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,19 @@ def run_script(
use_cache: bool,
) -> NoReturn:
requirements = _get_requirements_from_script(content)
if requirements is None:
requirements = _get_requirements_from_script(content, script=False)
if requirements is not None:
logger.warning(
pipx_wrap(
f"""
{hazard} Using old form of requirements table. Use updated PEP
723 syntax by replacing `# /// pyproject` with `# /// script`
and `run.requirements` with `requirements`.
""",
subsequent_indent=" " * 4,
)
)
if requirements is None:
exec_app([python, "-c", content, *app_args])
else:
Expand Down Expand Up @@ -323,12 +336,12 @@ def _http_get_request(url: str) -> str:
INLINE_SCRIPT_METADATA = re.compile(r"(?m)^# /// (?P<type>[a-zA-Z0-9-]+)$\s(?P<content>(^#(| .*)$\s)+)^# ///$")


def _get_requirements_from_script(content: str) -> Optional[List[str]]:
def _get_requirements_from_script(content: str, *, script: bool = True) -> Optional[List[str]]:
"""
Supports inline script metadata.
Supports inline script metadata. `script=True` is the updated version of PEP 723.
"""

name = "pyproject"
name = "script" if script else "pyproject"

# Windows is currently getting un-normalized line endings, so normalize
content = content.replace("\r\n", "\n")
Expand All @@ -348,7 +361,8 @@ def _get_requirements_from_script(content: str) -> Optional[List[str]]:
pyproject = tomllib.loads(content)

requirements = []
for requirement in pyproject.get("run", {}).get("requirements", []):
req_table = pyproject if script else pyproject.get("run", {})
for requirement in req_table.get("requirements", []):
henryiii marked this conversation as resolved.
Show resolved Hide resolved
# Validate the requirement
try:
req = Requirement(requirement)
Expand Down
34 changes: 30 additions & 4 deletions tests/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,32 @@ def test_run_without_requirements(caplog, pipx_temp_env, tmp_path):

@mock.patch("os.execvpe", new=execvpe_mock)
def test_run_with_requirements(caplog, pipx_temp_env, tmp_path):
script = tmp_path / "test.py"
out = tmp_path / "output.txt"
script.write_text(
textwrap.dedent(
f"""
# /// script
# 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",
)
run_pipx_cli_exit(["run", script.as_uri()])
assert out.read_text() == "2.31.0"


@mock.patch("os.execvpe", new=execvpe_mock)
def test_run_with_requirements_deprecated(caplog, pipx_temp_env, tmp_path):
script = tmp_path / "test.py"
out = tmp_path / "output.txt"
script.write_text(
Expand Down Expand Up @@ -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
# requirements = ["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
# requirements = ["this is an invalid requirement"]
# ///
print()
"""
Expand Down