Skip to content

Commit

Permalink
clarify docstring, move deprecation warning
Browse files Browse the repository at this point in the history
  • Loading branch information
lukavdplas committed Nov 14, 2024
1 parent 810316d commit fea5636
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
27 changes: 17 additions & 10 deletions ianalyzer_readers/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,31 @@ class Extractor(object):
An extractor contains a method that can be used to gather data for a field.
Parameters:
applicable: optional predicate that takes metadata and decides whether this
extractor is applicable. If left as `None`, the extractor is always
applicable.
transform: optional function to transform the postprocess the extracted value.
applicable:
optional argument to check whether the extractor can be used. This should
be another extractor, which is applied first; the containing extractor
is only applied if the result is truthy. Any extractor can be used, as long as
it's supported by the Reader in which it's used. If left as `None`, this
extractor is always applicable.
transform: optional function to transform or postprocess the extracted value.
'''

def __init__(self,
applicable: Optional[Callable[[Dict], bool]] = None,
applicable: Union['Extractor', Callable[[Dict], bool], None] = None,
transform: Optional[Callable] = None
):

if callable(applicable):
warnings.warn(
'Using a callable as "applicable" argument is deprecated; provide an '
'Extractor instead',
DeprecationWarning,
)

self.transform = transform
self.applicable = applicable


def apply(self, *nargs, **kwargs):
'''
Test if the extractor is applicable to the given arguments and if so,
Expand Down Expand Up @@ -94,11 +106,6 @@ def _is_applicable(self, *nargs, **kwargs) -> bool:
if isinstance(self.applicable, Extractor):
return bool(self.applicable.apply(*nargs, **kwargs))
if callable(self.applicable):
warnings.warn(
'Using a callable as "applicable" argument is deprecated; provide an '
'Extractor instead',
DeprecationWarning,
)
return self.applicable(kwargs.get('metadata'))
return TypeError(
f'Unsupported type for "applicable" parameter: {type(self.applicable)}'
Expand Down
6 changes: 3 additions & 3 deletions tests/test_generic_extractors.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def test_extractor_applicable_extractor():


def test_extractor_applicable_callable():
extractor = Constant('test', applicable=lambda metadata: metadata['testing'])
with pytest.warns(DeprecationWarning):
assert extractor.apply(metadata={'testing': True}) == 'test'
assert extractor.apply(metadata={'testing': False}) == None
extractor = Constant('test', applicable=lambda metadata: metadata['testing'])
assert extractor.apply(metadata={'testing': True}) == 'test'
assert extractor.apply(metadata={'testing': False}) == None

0 comments on commit fea5636

Please sign in to comment.