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

WIP: check for unknown kwargs in Index.select #2663

Open
wants to merge 2 commits into
base: try-submanifest
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/sourmash/index/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@
flatten_and_downsample_num,
flatten_and_intersect_scaled)

def _check_for_unknown_select_params(kwargs):
legit_param_names = { 'ksize', 'moltype', 'scaled', 'num', 'abund',
'containment', 'picklist', 'filter_manifest' } # @CTB picklist
kk = set(kwargs.keys())
assert kk.issubset(legit_param_names), kk - legit_param_names


Comment on lines +52 to +58
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be relevant: in #2726 I defined a Selection TypedDict to be able to

  1. use type checking
  2. being able to more easily convert a selection to Rust (the _selection_as_rust function).

Then the method signature for select becomes
def select(self, **kwargs: Unpack[Selection]):

(This is slightly annoying because the select method needs to extract the values from kwargs...)

PEP-692 has more details

# generic return tuple for Index.search and Index.gather
IndexSearchResult = namedtuple('Result', 'score, signature, location')

Expand Down Expand Up @@ -413,6 +420,8 @@ def select(self, **kwargs):

Does not raise ValueError, but may return an empty Index.
"""
_check_for_unknown_select_params(kwargs)

siglist = []
for ss in self._signatures:
if select_signature(ss, **kwargs):
Expand Down Expand Up @@ -484,6 +493,8 @@ def select(self, **kwargs):

Does not raise ValueError, but may return an empty Index.
"""
_check_for_unknown_select_params(kwargs)

selection_dict = dict(self.selection_dict)
for k, v in kwargs.items():
if k in selection_dict:
Expand Down Expand Up @@ -644,6 +655,7 @@ def signatures(self):

def select(self, **kwargs):
"Select signatures in zip file based on ksize/moltype/etc."
_check_for_unknown_select_params(kwargs)

# if we have a manifest, run 'select' on the manifest.
manifest = self.manifest
Expand Down Expand Up @@ -1055,6 +1067,8 @@ def save(self, *args):

def select(self, **kwargs):
"Run 'select' on the manifest."
_check_for_unknown_select_params(kwargs)

new_manifest = self.manifest.select_to_manifest(**kwargs)
return MultiIndex(new_manifest, self.parent,
prepend_location=self.prepend_location)
Expand Down Expand Up @@ -1164,6 +1178,8 @@ def insert(self, *args):

def select(self, **kwargs):
"Run 'select' on the manifest."
_check_for_unknown_select_params(kwargs)

new_manifest = self.manifest.select_to_manifest(**kwargs)
return StandaloneManifestIndex(new_manifest, self._location,
prefix=self.prefix)
3 changes: 2 additions & 1 deletion src/sourmash/sbt.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ def _signatures_with_internal(self):
yield ss, k._path

def select(self, ksize=None, moltype=None, num=0, scaled=0,
containment=False, abund=None, picklist=None, filter_manifest=None):
containment=False, abund=None, picklist=None,
filter_manifest=None):
"""Make sure this database matches the requested requirements.

Will always raise ValueError if a requirement cannot be met.
Expand Down