Skip to content

Commit e15a6b7

Browse files
committed
Implement caller inference to allow files() to be called without any parameter.
1 parent 38f789d commit e15a6b7

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

importlib_resources/_common.py

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
import contextlib
66
import types
77
import importlib
8+
import inspect
89
import warnings
10+
import itertools
911

1012
from typing import Union, Optional, cast
1113
from .abc import ResourceReader, Traversable
@@ -22,12 +24,9 @@ def package_to_anchor(func):
2224
2325
Other errors should fall through.
2426
25-
>>> files()
26-
Traceback (most recent call last):
27-
TypeError: files() missing 1 required positional argument: 'anchor'
2827
>>> files('a', 'b')
2928
Traceback (most recent call last):
30-
TypeError: files() takes 1 positional argument but 2 were given
29+
TypeError: files() takes from 0 to 1 positional arguments but 2 were given
3130
"""
3231
undefined = object()
3332

@@ -50,7 +49,7 @@ def wrapper(anchor=undefined, package=undefined):
5049

5150

5251
@package_to_anchor
53-
def files(anchor: Anchor) -> Traversable:
52+
def files(anchor: Optional[Anchor] = None) -> Traversable:
5453
"""
5554
Get a Traversable resource for an anchor.
5655
"""
@@ -74,7 +73,7 @@ def get_resource_reader(package: types.ModuleType) -> Optional[ResourceReader]:
7473

7574

7675
@functools.singledispatch
77-
def resolve(cand: Anchor) -> types.ModuleType:
76+
def resolve(cand: Optional[Anchor]) -> types.ModuleType:
7877
return cast(types.ModuleType, cand)
7978

8079

@@ -83,6 +82,28 @@ def _(cand: str) -> types.ModuleType:
8382
return importlib.import_module(cand)
8483

8584

85+
@resolve.register
86+
def _(cand: None) -> types.ModuleType:
87+
return resolve(_infer_caller().f_globals['__name__'])
88+
89+
90+
def _infer_caller():
91+
"""
92+
Walk the stack and find the frame of the first caller not in this module.
93+
"""
94+
95+
def is_this_file(frame_info):
96+
return frame_info.filename == __file__
97+
98+
def is_wrapper(frame_info):
99+
return frame_info.function == 'wrapper'
100+
101+
not_this_file = itertools.filterfalse(is_this_file, inspect.stack())
102+
# also exclude 'wrapper' due to singledispatch in the call stack
103+
callers = itertools.filterfalse(is_wrapper, not_this_file)
104+
return next(callers).frame
105+
106+
86107
def from_package(package: types.ModuleType):
87108
"""
88109
Return a Traversable object for the given package.

importlib_resources/tests/test_files.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ def test_module_resources(self):
8989

9090

9191
class ImplicitContextFilesTests(SiteDir, unittest.TestCase):
92-
@__import__('pytest').mark.xfail(reason="work in progress")
9392
def test_implicit_files(self):
9493
"""
9594
Without any parameter, files() will infer the location as the caller.

0 commit comments

Comments
 (0)