Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 81c3c5e

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

File tree

1 file changed

+42
-19
lines changed

1 file changed

+42
-19
lines changed

Doc/library/importlib.resources.rst

Lines changed: 42 additions & 19 deletions
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,35 @@ 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 passing the anchor positionally or
79+
using ``importlib_resources >= 5.10`` for a compatible interface
80+
on older Pythons.
81+
7382
.. function:: as_file(traversable)
7483

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

8796
.. versionadded:: 3.9
8897

98+
8999
Deprecated functions
90100
--------------------
91101

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

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

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

103125
The ``Resource`` type is defined as ``Union[str, os.PathLike]``.
104126

127+
105128
.. function:: open_binary(package, resource)
106129

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

0 commit comments

Comments
 (0)