File tree Expand file tree Collapse file tree 3 files changed +47
-3
lines changed
Expand file tree Collapse file tree 3 files changed +47
-3
lines changed Original file line number Diff line number Diff line change 66 module is passed, resources will be resolved adjacent to
77 those modules, even for modules not found in any package.
88 For example, ``files(import_module('mod.py')) `` will
9- resolve resources found at the root.
9+ resolve resources found at the root. The parameter to
10+ files was renamed from 'package' to 'anchor', with a
11+ compatibility shim for those passing by keyword.
1012
1113v5.9.0
1214======
Original file line number Diff line number Diff line change 55import contextlib
66import types
77import importlib
8+ import warnings
89
910from typing import Union , Optional , cast
1011from .abc import ResourceReader , Traversable
1516Anchor = Package
1617
1718
18- def files (package : Anchor ) -> Traversable :
19+ def package_to_anchor (func ):
20+ """
21+ Replace 'package' parameter as 'anchor' and warn about the change.
22+ """
23+ undefined = object ()
24+
25+ @functools .wraps (func )
26+ def wrapper (anchor = undefined , package = undefined ):
27+ if package is not undefined :
28+ if anchor is not undefined :
29+ return func (anchor , package )
30+ warnings .warn (
31+ "First parameter to files is renamed to 'anchor'" ,
32+ DeprecationWarning ,
33+ stacklevel = 2 ,
34+ )
35+ return func (package )
36+ elif anchor is undefined :
37+ return func ()
38+ return func (anchor )
39+
40+ return wrapper
41+
42+
43+ @package_to_anchor
44+ def files (anchor : Anchor ) -> Traversable :
1945 """
2046 Get a Traversable resource for an anchor.
2147 """
22- return from_package (resolve (package ))
48+ return from_package (resolve (anchor ))
2349
2450
2551def get_resource_reader (package : types .ModuleType ) -> Optional [ResourceReader ]:
Original file line number Diff line number Diff line change 11import typing
22import unittest
3+ import warnings
34import contextlib
45
56import importlib_resources as resources
1011from ._compat import os_helper , import_helper
1112
1213
14+ @contextlib .contextmanager
15+ def suppress_known_deprecation ():
16+ with warnings .catch_warnings (record = True ) as ctx :
17+ warnings .simplefilter ('default' , category = DeprecationWarning )
18+ yield ctx
19+
20+
1321class FilesTests :
1422 def test_read_bytes (self ):
1523 files = resources .files (self .data )
@@ -28,6 +36,14 @@ def test_read_text(self):
2836 def test_traversable (self ):
2937 assert isinstance (resources .files (self .data ), Traversable )
3038
39+ def test_old_parameter (self ):
40+ """
41+ Files used to take a 'package' parameter. Make sure anyone
42+ passing by name is still supported.
43+ """
44+ with suppress_known_deprecation ():
45+ resources .files (package = self .data )
46+
3147
3248class OpenDiskTests (FilesTests , unittest .TestCase ):
3349 def setUp (self ):
You can’t perform that action at this time.
0 commit comments