Skip to content

Commit

Permalink
validation on translation dicts (#160)
Browse files Browse the repository at this point in the history
  • Loading branch information
jagerber48 authored Feb 18, 2024
2 parents 1b70190 + 57abe96 commit d4c6b4c
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ Changed
sub-package.
* Sort tests into feature and unit tests.

* Extra translations dictionaries are now checked so that keys must be
integers and values must consist of only English alphabetic
characters.
[`#157 <https://github.com/jagerber48/sciform/issues/157>`_]

----

0.35.0 (2024-02-16)
Expand Down
3 changes: 3 additions & 0 deletions docs/source/options.rst
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,9 @@ force that exponent to not be translated.
>>> print(formatter(3e-9))
3e-09

Keys into the extra translations dictionaries must be integers and
values must consist of only English alphabetic characters.

Two helper options exist to add additional SI prefix translations
corresponding to::

Expand Down
27 changes: 27 additions & 0 deletions src/sciform/options/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import annotations

import re
from typing import TYPE_CHECKING, get_args

from sciform.options import option_types
Expand All @@ -19,6 +20,7 @@ def validate_options(
validate_sig_fig_round_mode(options)
validate_exp_val(options)
validate_separator_options(options)
validate_extra_translations(options)


def validate_sig_fig_round_mode(
Expand Down Expand Up @@ -99,3 +101,28 @@ def validate_separator_options(
f"not {options.lower_separator}."
)
raise ValueError(msg)


def validate_extra_translations(
options: InputOptions | PopulatedOptions | FinalizedOptions,
) -> None:
"""Validate translation dictionary have int keys and alphabetic values."""
translations_dicts = [
options.extra_si_prefixes,
options.extra_iec_prefixes,
options.extra_parts_per_forms,
]

for translation_dict in translations_dicts:
if translation_dict is not None:
for key, value in translation_dict.items():
if not isinstance(key, int):
msg = f'Translation dictionary keys must be integers, not "{key}".'
raise TypeError(msg)
if value is not None and not re.match(r"[a-zA-Z]+", value):
msg = (
f"Translation dictionary values may only contain lower or "
f"uppercase ASCII characters from the English alphabet, not "
f'"{value}".'
)
raise ValueError(msg)
14 changes: 14 additions & 0 deletions tests/unit/test_invalid_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,3 +278,17 @@ def test_parse_mantissa_from_ascii_exp_str(self):
parse_mantissa_from_ascii_exp_str,
"1.23c+04",
)

def test_invalid_translation_key(self):
self.assertRaises(
TypeError,
Formatter,
extra_si_prefixes={"3": "k"},
)

def test_invalid_translation_value(self):
self.assertRaises(
ValueError,
Formatter,
extra_si_prefixes={-10: "Å"},
)

0 comments on commit d4c6b4c

Please sign in to comment.