Skip to content

Commit 0b49678

Browse files
committed
Update documentation to match the implementation with Anchor object being preferred and Package object being deprecated.
1 parent 9f533cb commit 0b49678

File tree

1 file changed

+41
-19
lines changed

1 file changed

+41
-19
lines changed

Doc/library/importlib.resources.rst

+41-19
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ This module leverages Python's import system to provide access to *resources*
1414
within *packages*.
1515

1616
"Resources" are file-like resources associated with a module or package in
17-
Python. The resources may be contained directly in a package or within a
18-
subdirectory contained in that package. Resources may be text or binary. As a
19-
result, Python module sources (.py) of a package and compilation artifacts
20-
(pycache) are technically de-facto resources of that package. In practice,
21-
however, resources are primarily those non-Python artifacts exposed
22-
specifically by the package author.
17+
Python. The resources may be contained directly in a package, within a
18+
subdirectory contained in that package, or adjacent to modules outside a
19+
package. Resources may be text or binary. As a result, Python module sources
20+
(.py) of a package and compilation artifacts (pycache) are technically
21+
de-facto resources of that package. In practice, however, resources are
22+
primarily those non-Python artifacts exposed specifically by the package
23+
author.
2324

2425
Resources can be opened or read in either binary or text mode.
2526

@@ -49,27 +50,34 @@ for example, a package and its resources can be imported from a zip file using
4950
``get_resource_reader(fullname)`` method as specified by
5051
:class:`importlib.resources.abc.ResourceReader`.
5152

52-
.. data:: Package
53+
.. data:: Anchor
5354

54-
Whenever a function accepts a ``Package`` argument, you can pass in
55-
either a :class:`module object <types.ModuleType>` or a module name
56-
as a string. You can only pass module objects whose
57-
``__spec__.submodule_search_locations`` is not ``None``.
55+
Represents an anchor for resources, either a :class:`module object
56+
<types.ModuleType>` or a module name as a string. Defined as
57+
``Union[str, ModuleType]``.
5858

59-
The ``Package`` type is defined as ``Union[str, ModuleType]``.
60-
61-
.. function:: files(package)
59+
.. function:: files(anchor: Optional[Anchor] = None)
6260

6361
Returns a :class:`~importlib.resources.abc.Traversable` object
64-
representing the resource container for the package (think directory)
65-
and its resources (think files). A Traversable may contain other
66-
containers (think subdirectories).
62+
representing the resource container (think directory) and its resources
63+
(think files). A Traversable may contain other containers (think
64+
subdirectories).
6765

68-
*package* is either a name or a module object which conforms to the
69-
:data:`Package` requirements.
66+
*anchor* is an optional :data:`Anchor`. If the anchor is a
67+
package, resources are resolved from that package. If a module,
68+
resources are resolved adjacent to that module (in the same package
69+
or the package root). If the anchor is omitted, the caller's module
70+
is used.
7071

7172
.. versionadded:: 3.9
7273

74+
.. versionchanged:: 3.12
75+
"package" parameter was renamed to "anchor". "anchor" can now
76+
be a non-package module and if omitted will default to the caller's
77+
module. "package" is still accepted for compatibility but will raise
78+
a DeprecationWarning. Consider using ``importlib_resources >= 5.10``
79+
for a compatible interface on older Pythons.
80+
7381
.. function:: as_file(traversable)
7482

7583
Given a :class:`~importlib.resources.abc.Traversable` object representing
@@ -86,6 +94,7 @@ for example, a package and its resources can be imported from a zip file using
8694

8795
.. versionadded:: 3.9
8896

97+
8998
Deprecated functions
9099
--------------------
91100

@@ -94,6 +103,18 @@ scheduled for removal in a future version of Python.
94103
The main drawback of these functions is that they do not support
95104
directories: they assume all resources are located directly within a *package*.
96105

106+
.. data:: Package
107+
108+
Whenever a function accepts a ``Package`` argument, you can pass in
109+
either a :class:`module object <types.ModuleType>` or a module name
110+
as a string. You can only pass module objects whose
111+
``__spec__.submodule_search_locations`` is not ``None``.
112+
113+
The ``Package`` type is defined as ``Union[str, ModuleType]``.
114+
115+
.. deprecated:: 3.12
116+
117+
97118
.. data:: Resource
98119

99120
For *resource* arguments of the functions below, you can pass in
@@ -102,6 +123,7 @@ directories: they assume all resources are located directly within a *package*.
102123

103124
The ``Resource`` type is defined as ``Union[str, os.PathLike]``.
104125

126+
105127
.. function:: open_binary(package, resource)
106128

107129
Open for binary reading the *resource* within *package*.

0 commit comments

Comments
 (0)