Skip to content

Commit

Permalink
Merge pull request #96 from pfmoore/pep723
Browse files Browse the repository at this point in the history
Implement PEP 723 support (script dependencies in a TOML block)
  • Loading branch information
jaraco committed Jan 20, 2024
2 parents 2e64d7a + 42ae216 commit 3166619
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,12 @@ variable or ``# Requirements:`` section to the script:
# Requirements:
# requests
# or (PEP 723)
# /// script
# dependencies = ['requests']
# ///
import requests
req = requests.get('https://pypi.org/project/pip-run')
Expand Down
1 change: 1 addition & 0 deletions newsfragments/+96.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add support for script dependencies in a TOML block per PEP 723.
8 changes: 8 additions & 0 deletions pip_run/compat/py310.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"""
Compatibility for Python 3.10 and earlier.
"""

try:
import tomllib # type: ignore
except ImportError: # pragma: no cover
import tomli as tomllib # type: ignore
37 changes: 36 additions & 1 deletion pip_run/scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
from jaraco.context import suppress
from jaraco.functools import compose

from .compat.py310 import tomllib


ValidRequirementString = compose(str, packaging.requirements.Requirement)


Expand Down Expand Up @@ -73,7 +76,39 @@ def search(cls, params):
return cls.try_read(next(files, None)).params()

def read(self):
return self.read_comments() or self.read_python()
return self.read_toml() or self.read_comments() or self.read_python()

def read_toml(self):
r"""
>>> DepsReader('# /// script\n# dependencies = ["foo", "bar"]\n# ///\n').read()
['foo', 'bar']
>>> DepsReader('# /// pyproject\n# dependencies = ["foo", "bar"]\n# ///\n').read_toml()
[]
>>> DepsReader('# /// pyproject\n#dependencies = ["foo", "bar"]\n# ///\n').read_toml()
[]
>>> DepsReader('# /// script\n# dependencies = ["foo", "bar"]\n').read_toml()
[]
>>> DepsReader('# /// script\n# ///\n\n# /// script\n# ///').read_toml()
Traceback (most recent call last):
...
ValueError: Multiple script blocks found
"""
TOML_BLOCK_REGEX = r'(?m)^# /// (?P<type>[a-zA-Z0-9-]+)$\s(?P<content>(^#(| .*)$\s)*)^# ///$'
name = 'script'
matches = list(
filter(lambda m: m.group('type') == name, re.finditer(TOML_BLOCK_REGEX, self.script))
)
if len(matches) > 1:
raise ValueError(f'Multiple {name} blocks found')
elif len(matches) == 1:
content = ''.join(
line[2:] if line.startswith('# ') else line[1:]
for line in matches[0].group('content').splitlines(keepends=True)
)
deps = tomllib.loads(content).get("dependencies", [])
else:
deps = []
return Dependencies.load(deps)

def read_comments(self):
r"""
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ install_requires =
jaraco.context
jaraco.text
platformdirs
tomli; python_version < "3.11"
importlib_resources; python_version < "3.9"
jaraco.functools >= 3.7
jaraco.env
Expand Down
13 changes: 13 additions & 0 deletions tests/test_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,19 @@ def test_comment_style(self):
reqs = scripts.DepsReader(script).read()
assert reqs == ['foo==3.1']

def test_toml_style(self):
script = textwrap.dedent(
"""
#! shebang
# /// script
# dependencies = ["foo == 3.1"]
# ///
"""
)
reqs = scripts.DepsReader(script).read()
assert reqs == ['foo==3.1']

def test_search_long_parameter(self):
"""
A parameter that is too long to be a filename should not fail.
Expand Down

0 comments on commit 3166619

Please sign in to comment.