File tree 3 files changed +47
-3
lines changed
3 files changed +47
-3
lines changed Original file line number Diff line number Diff line change 6
6
module is passed, resources will be resolved adjacent to
7
7
those modules, even for modules not found in any package.
8
8
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.
10
12
11
13
v5.9.0
12
14
======
Original file line number Diff line number Diff line change 5
5
import contextlib
6
6
import types
7
7
import importlib
8
+ import warnings
8
9
9
10
from typing import Union , Optional , cast
10
11
from .abc import ResourceReader , Traversable
15
16
Anchor = Package
16
17
17
18
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 :
19
45
"""
20
46
Get a Traversable resource for an anchor.
21
47
"""
22
- return from_package (resolve (package ))
48
+ return from_package (resolve (anchor ))
23
49
24
50
25
51
def get_resource_reader (package : types .ModuleType ) -> Optional [ResourceReader ]:
Original file line number Diff line number Diff line change 1
1
import typing
2
2
import unittest
3
+ import warnings
3
4
import contextlib
4
5
5
6
import importlib_resources as resources
10
11
from ._compat import os_helper , import_helper
11
12
12
13
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
+
13
21
class FilesTests :
14
22
def test_read_bytes (self ):
15
23
files = resources .files (self .data )
@@ -28,6 +36,14 @@ def test_read_text(self):
28
36
def test_traversable (self ):
29
37
assert isinstance (resources .files (self .data ), Traversable )
30
38
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
+
31
47
32
48
class OpenDiskTests (FilesTests , unittest .TestCase ):
33
49
def setUp (self ):
You can’t perform that action at this time.
0 commit comments