22Inline script metadata
33======================
44
5- .. warning ::
6- This specification has been **provisionally accepted **. It is subject
7- to being changed or abandoned. See the
8- `PEP 723 conditional acceptance thread <pep723-thread _>`_ for details.
9-
10- .. _pep723-thread : https://discuss.python.org/t/36763
11-
125This specification defines a metadata format that can be embedded in single-file
136Python scripts to assist launchers, IDEs and other external tools which may need
147to interact with such scripts.
@@ -73,20 +66,22 @@ In circumstances where there is a discrepancy between the text specification
7366and the regular expression, the text specification takes precedence.
7467
7568Tools MUST NOT read from metadata blocks with types that have not been
76- standardized by this PEP or future ones .
69+ standardized by this specification .
7770
78- pyproject type
79- --------------
71+ script type
72+ -----------
8073
81- The first type of metadata block is named ``pyproject `` which represents
82- content similar to what one would see in a `` pyproject.toml `` file .
74+ The first type of metadata block is named ``script ``, which contains
75+ script metadata (dependency data and tool configuration) .
8376
84- This document MAY include the ``[run] `` and ``[tool] `` tables.
77+ This document MAY include the top-level fields ``dependencies `` and ``requires-python ``,
78+ and MAY optionally include a ``[tool] `` table.
8579
86- The :ref: `tool table <pyproject-tool-table >` MAY be used by any tool,
87- script runner or otherwise, to configure behavior.
80+ The ``[tool] `` MAY be used by any tool, script runner or otherwise, to configure
81+ behavior. It has the same semantics as the :ref: `[tool] table in pyproject.toml
82+ <pyproject-tool-table>`.
8883
89- The `` [run] `` table MAY include the following optional fields:
84+ The top-level fields are :
9085
9186* ``dependencies ``: A list of strings that specifies the runtime dependencies
9287 of the script. Each entry MUST be a valid
@@ -95,24 +90,18 @@ The ``[run]`` table MAY include the following optional fields:
9590 the script is compatible. The value of this field MUST be a valid
9691 :ref: `version specifier <version-specifiers >`.
9792
98- Any future specifications that define additional fields for the ``[run] `` table
99- when used in a ``pyproject.toml `` file MUST include the aforementioned fields
100- exactly as specified. The fields defined by this specification are equally as
101- applicable to full-fledged projects as they are to single-file scripts.
102-
10393Script runners MUST error if the specified ``dependencies `` cannot be provided.
10494Script runners SHOULD error if no version of Python that satisfies the specified
10595``requires-python `` can be provided.
10696
10797Example
10898-------
10999
110- The following is an example of a script with an embedded `` pyproject.toml `` :
100+ The following is an example of a script with embedded metadata :
111101
112102.. code :: python
113103
114- # /// pyproject
115- # [run]
104+ # /// script
116105 # requires-python = ">=3.11"
117106 # dependencies = [
118107 # "requests<3",
@@ -127,23 +116,6 @@ The following is an example of a script with an embedded ``pyproject.toml``:
127116 data = resp.json()
128117 pprint([(k, v[" title" ]) for k, v in data.items()][:10 ])
129118
130- The following is an example of a proposed syntax for single-file Rust
131- projects that embeds their equivalent of ``pyproject.toml ``, which is called
132- ``Cargo.toml ``:
133-
134- .. code :: rust
135-
136- #!/usr/bin/env cargo
137-
138- //! ```cargo
139- //! [dependencies]
140- //! regex = "1.8.0"
141- //! ```
142-
143- fn main() {
144- let re = Regex::new(r"^\d{4}-\d{2}-\d{2}$").unwrap();
145- println!("Did our date match? {}", re.is_match("2014-01-01"));
146- }
147119
148120 Reference Implementation
149121========================
@@ -159,7 +131,7 @@ higher.
159131 REGEX = r ' (?m) ^ # /// ( ?P<type> [a-zA-Z0-9- ]+ ) $ \s ( ?P<content> ( ^ #( | . * ) $ \s ) + ) ^ # ///$ '
160132
161133 def read (script : str ) -> dict | None :
162- name = ' pyproject '
134+ name = ' script '
163135 matches = list (
164136 filter (lambda m : m.group(' type' ) == name, re.finditer(REGEX , script))
165137 )
@@ -196,7 +168,7 @@ __ https://tomlkit.readthedocs.io/en/latest/
196168 )
197169
198170 config = tomlkit.parse(content)
199- config[' project ' ][ ' dependencies' ].append(dependency)
171+ config[' dependencies' ].append(dependency)
200172 new_content = ' ' .join(
201173 f ' # { line} ' if line.strip() else f ' # { line} '
202174 for line in tomlkit.dumps(config).splitlines(keepends = True )
@@ -239,3 +211,7 @@ History
239211=======
240212
241213- October 2023: This specification was conditionally approved through :pep: `723 `.
214+ - January 2024: Through amendments to :pep: `723 `, the ``pyproject `` metadata
215+ block type was renamed to ``script ``, and the ``[run] `` table was dropped,
216+ making the ``dependencies `` and ``requires-python `` keys
217+ top-level. Additionally, the specification is no longer provisional.
0 commit comments