Skip to content

Commit

Permalink
Error on invalid --min-date/--max-date
Browse files Browse the repository at this point in the history
The previous change to support relative dates also refactored so that an invalid date does not raise any error, which is unwanted.
Prior to that change, there was one try/catch so treetime.utils.numeric_date would handle all these invalid dates.

Explicitly raising from augur.filter.numeric_date is one step in a better direction, though still not a good solution since argparse does not expose these errors.
More: https://stackoverflow.com/q/38340252
  • Loading branch information
victorlin committed Apr 6, 2022
1 parent eafadef commit 6a88293
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
4 changes: 4 additions & 0 deletions augur/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -1735,6 +1735,10 @@ def numeric_date(date):
except (ValueError, isodate.ISO8601Error):
pass

# This currently doesn't get exposed since argparse raises a SystemExit on invalid arguments.
# TODO: find a way to provide better errors for invalid dates.
raise ValueError(f"Unable to determine date from {date}.")


def calculate_sequences_per_group(target_max_value, counts_per_group, allow_probabilistic=True):
"""Calculate the number of sequences per group for a given maximum number of
Expand Down
16 changes: 16 additions & 0 deletions tests/test_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,3 +356,19 @@ def test_filter_relative_dates(self, tmpdir, argparser, argparse_params, metadat
with open(out_fn) as f:
output_sorted = sorted(line.rstrip() for line in f)
assert output_sorted == output_sorted_expected

@freeze_time("2020-03-25")
@pytest.mark.parametrize(
"argparse_params",
[
"--max-date 3000Y",
"--max-date invalid",
],
)
def test_filter_relative_dates_error(self, tmpdir, argparser, argparse_params):
"""Test that invalid dates fail"""
out_fn = str(tmpdir / "filtered.txt")
meta_fn = write_metadata(tmpdir, (("strain","date"),
("SEQ_1","2020-03-23")))
with pytest.raises(SystemExit):
argparser(f'--metadata {meta_fn} --output-strains {out_fn} {argparse_params}')

0 comments on commit 6a88293

Please sign in to comment.