Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infer type from default option #569

Merged
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 13 additions & 3 deletions strax/config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import builtins
import typing as ty

import numbers
from immutabledict import immutabledict

import strax
Expand Down Expand Up @@ -110,15 +110,25 @@ def __init__(self,
# f" which will soon stop working!",
# DeprecationWarning)

type = builtins.type
if sum([self.default is not OMITTED,
self.default_factory is not OMITTED,
self.default_by_run is not OMITTED]) > 1:
raise RuntimeError(f"Tried to specify more than one default "
f"for option {self.name}.")

if type is OMITTED and default is not OMITTED:
self.type = type(default)
for ntype in [numbers.Integral, numbers.Number]:
# first check if its a number otherwise numpy numbers
# will fail type checking when checked against int and float.
# numbers.Integral, numbers.Number are safe to use
# since numpy registers them as super-classes.
# left as a loop since we may want to add other exceptions as
# they are discovered.
if isinstance(default, ntype):
self.type = ntype
break
else:
self.type = builtins.type(default)

def get_default(self, run_id, run_defaults: dict = None):
"""Return default value for the option"""
Expand Down
2 changes: 1 addition & 1 deletion strax/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
strax.Option(name='free_options', default=tuple(),
help='Do not warn if any of these options are passed, '
'even when no registered plugin takes them.'),
strax.Option(name='apply_data_function', default=tuple(),
strax.Option(name='apply_data_function', default=tuple(), type=(tuple,list,ty.Callable),
help='Apply a function to the data prior to returning the'
'data. The function should take three positional arguments: '
'func(<data>, <run_id>, <targets>).'),
Expand Down