Skip to content

Commit f6eee56

Browse files
committed
Rely on passthrough to designate a wrapper for its side effect.
1 parent 3c9510b commit f6eee56

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

importlib_metadata/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
NullFinder,
3636
install,
3737
)
38-
from ._functools import method_cache, noop, pass_none
38+
from ._functools import method_cache, noop, pass_none, passthrough
3939
from ._itertools import always_iterable, bucket, unique_everseen
4040
from ._meta import PackageMetadata, SimplePath
4141
from ._typing import md_none
@@ -787,6 +787,7 @@ def find_distributions(self, context=Context()) -> Iterable[Distribution]:
787787
"""
788788

789789

790+
@passthrough
790791
def _clear_after_fork(cached):
791792
"""Ensure ``func`` clears cached state after ``fork`` when supported.
792793
@@ -798,7 +799,6 @@ def _clear_after_fork(cached):
798799
on its own cache.
799800
"""
800801
getattr(os, 'register_at_fork', noop)(after_in_child=cached.cache_clear)
801-
return cached
802802

803803

804804
class FastPath:
@@ -817,7 +817,7 @@ class FastPath:
817817
True
818818
"""
819819

820-
@_clear_after_fork
820+
@_clear_after_fork # type: ignore[misc]
821821
@functools.lru_cache()
822822
def __new__(cls, root):
823823
return super().__new__(cls)

importlib_metadata/_functools.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import functools
22
import types
3+
from typing import Callable, TypeVar
34

45

56
# from jaraco.functools 3.3
@@ -111,3 +112,24 @@ def noop(*args, **kwargs):
111112
112113
>>> noop(1, 2, three=3)
113114
"""
115+
116+
117+
_T = TypeVar('_T')
118+
119+
120+
# From jaraco.functools 4.4
121+
def passthrough(func: Callable[..., object]) -> Callable[[_T], _T]:
122+
"""
123+
Wrap the function to always return the first parameter.
124+
125+
>>> passthrough(print)('3')
126+
3
127+
'3'
128+
"""
129+
130+
@functools.wraps(func)
131+
def wrapper(first: _T, *args, **kwargs) -> _T:
132+
func(first, *args, **kwargs)
133+
return first
134+
135+
return wrapper # type: ignore[return-value]

0 commit comments

Comments
 (0)