Skip to content

Commit 1e19fd2

Browse files
committed
Refactor Distribution.files to use pass_none to handle a None value from _read_files*.
1 parent 5875f39 commit 1e19fd2

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

importlib_metadata/__init__.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
install,
2222
pypy_partial,
2323
)
24-
from ._functools import method_cache
24+
from ._functools import method_cache, pass_none
2525
from ._itertools import always_iterable, unique_everseen
2626
from ._meta import PackageMetadata, SimplePath
2727

@@ -635,7 +635,6 @@ def files(self):
635635
missing.
636636
Result may be empty if the metadata exists but is empty.
637637
"""
638-
file_lines = self._read_files_distinfo() or self._read_files_egginfo()
639638

640639
def make_file(name, hash=None, size_str=None):
641640
result = PackagePath(name)
@@ -644,7 +643,11 @@ def make_file(name, hash=None, size_str=None):
644643
result.dist = self
645644
return result
646645

647-
return file_lines and list(starmap(make_file, csv.reader(file_lines)))
646+
@pass_none
647+
def make_files(lines):
648+
return list(starmap(make_file, csv.reader(lines)))
649+
650+
return make_files(self._read_files_distinfo() or self._read_files_egginfo())
648651

649652
def _read_files_distinfo(self):
650653
"""

importlib_metadata/_functools.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,22 @@ def wrapper(self, *args, **kwargs):
8383
wrapper.cache_clear = lambda: None
8484

8585
return wrapper
86+
87+
88+
# From jaraco.functools 3.3
89+
def pass_none(func):
90+
"""
91+
Wrap func so it's not called if its first param is None
92+
93+
>>> print_text = pass_none(print)
94+
>>> print_text('text')
95+
text
96+
>>> print_text(None)
97+
"""
98+
99+
@functools.wraps(func)
100+
def wrapper(param, *args, **kwargs):
101+
if param is not None:
102+
return func(param, *args, **kwargs)
103+
104+
return wrapper

0 commit comments

Comments
 (0)