Skip to content

Commit 389e821

Browse files
PEP561: Clarify stub-only namespace package behavior (#2083)
Co-authored-by: Ethan Smith <[email protected]>
1 parent 22bf5e5 commit 389e821

File tree

1 file changed

+45
-4
lines changed

1 file changed

+45
-4
lines changed

pep-0561.rst

+45-4
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,10 @@ For namespace packages (see PEP 420), the ``py.typed`` file should be in the
123123
submodules of the namespace, to avoid conflicts and for clarity.
124124

125125
This PEP does not support distributing typing information as part of
126-
module-only distributions. The code should be refactored into a package-based
127-
distribution and indicate that the package supports typing as described
126+
module-only distributions or single-file modules within namespace packages.
127+
128+
The single-file module should be refactored into a package
129+
and indicate that the package supports typing as described
128130
above.
129131

130132
Stub-only Packages
@@ -156,6 +158,28 @@ in pip 9.0, if you update ``flyingcircus-stubs``, it will update
156158
``--upgrade-strategy=only-if-needed`` flag. In pip 10.0 this is the default
157159
behavior.
158160

161+
For namespace packages (see PEP 420), stub-only packages should
162+
use the ``-stubs`` suffix on only the root namespace package.
163+
All stub-only namespace packages should omit ``__init__.pyi`` files. ``py.typed``
164+
marker files are not necessary for stub-only packages, but similarly
165+
to packages with inline types, if used, they should be in submodules of the namespace to
166+
avoid conflicts and for clarity.
167+
168+
For example, if the ``pentagon`` and ``hexagon`` are separate distributions
169+
installing within the namespace package ``shapes.polygons``
170+
The corresponding types-only distributions should produce packages
171+
laid out as follows::
172+
173+
shapes-stubs
174+
└── polygons
175+
└── pentagon
176+
└── __init__.pyi
177+
178+
shapes-stubs
179+
└── polygons
180+
└── hexagon
181+
   └── __init__.pyi
182+
159183

160184
Type Checker Module Resolution Order
161185
------------------------------------
@@ -180,6 +204,11 @@ resolve modules containing type information:
180204
5. Typeshed (if used) - Provides the stdlib types and several third party
181205
libraries.
182206

207+
If typecheckers identify a stub-only namespace package without the desired module
208+
in step 3, they should continue to step 4/5. Typecheckers should identify namespace packages
209+
by the absence of ``__init__.pyi``. This allows different subpackages to
210+
independently opt for inline vs stub-only.
211+
183212
Type checkers that check a different Python version than the version they run
184213
on MUST find the type information in the ``site-packages``/``dist-packages``
185214
of that Python version. This can be queried e.g.
@@ -204,8 +233,15 @@ typeshed folder and type checking the combined directory structure. Thus type
204233
checkers MUST maintain the normal resolution order of checking ``*.pyi`` before
205234
``*.py`` files.
206235

207-
If a stub package is partial it MUST include ``partial\n`` in a top level
208-
``py.typed`` file.
236+
If a stub package distribution is partial it MUST include ``partial\n`` in a
237+
``py.typed`` file. For stub-packages distributing within a namespace
238+
package (see PEP 420), the ``py.typed`` file should be in the
239+
submodules of the namespace.
240+
241+
Type checkers should treat namespace packages within stub-packages as
242+
incomplete since multiple distributions may populate them.
243+
Regular packages within namespace packages in stub-package distributions
244+
are considered complete unless a ``py.typed`` with ``partial\n`` is included.
209245

210246

211247
Implementation
@@ -236,6 +272,11 @@ Vlasovskikh, Nathaniel Smith, and Guido van Rossum.
236272
Version History
237273
===============
238274

275+
* 2021-09-20
276+
277+
* Clarify expectations and typechecker behavior for stub-only namespace packages
278+
* Clarify handling of single-file modules within namespace packages.
279+
239280
* 2018-07-09
240281

241282
* Add links to sample stub-only packages

0 commit comments

Comments
 (0)