Skip to content

Commit eb7f123

Browse files
committed
Rename the 'package' parameter to 'anchor'.
1 parent 4c672fb commit eb7f123

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

CHANGES.rst

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ v5.10.0
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

1113
v5.9.0
1214
======

importlib_resources/_common.py

+28-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import contextlib
66
import types
77
import importlib
8+
import warnings
89

910
from typing import Union, Optional, cast
1011
from .abc import ResourceReader, Traversable
@@ -15,11 +16,36 @@
1516
Anchor = 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

2551
def get_resource_reader(package: types.ModuleType) -> Optional[ResourceReader]:

importlib_resources/tests/test_files.py

+16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import typing
22
import unittest
3+
import warnings
34
import contextlib
45

56
import importlib_resources as resources
@@ -10,6 +11,13 @@
1011
from ._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+
1321
class 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

3248
class OpenDiskTests(FilesTests, unittest.TestCase):
3349
def setUp(self):

0 commit comments

Comments
 (0)