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

add test and update error message for functional.py::load_sp_model #984

Merged
merged 4 commits into from
Sep 16, 2020
Merged
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
8 changes: 8 additions & 0 deletions test/data/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ def test_sentencepiece_tokenizer(self):
self.assertEqual(list(spm_generator([test_sample]))[0],
ref_results)

def test_sentencepiece_unsupported_input_type(self):
with self.assertRaisesRegex(
TypeError,
'Unsupported type for spm argument: dict. '
'Supported types are: str, io.BufferedReader'
):
load_sp_model(dict())

# TODO(Nayef211): remove decorator once https://github.com/pytorch/pytorch/issues/38207 is closed
@unittest.skipIf(platform.system() == "Windows", "Test is known to fail on Windows.")
def test_BasicEnglishNormalize(self):
Expand Down
7 changes: 6 additions & 1 deletion torchtext/data/functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,12 @@ def load_sp_model(spm):
elif isinstance(spm, io.BufferedReader):
return torch.ops.torchtext.load_sp_model_string(spm.read())
else:
raise RuntimeError('the input of the load_sp_model func is not supported.')
raise TypeError(
f'Unsupported type for spm argument: {type(spm).__name__}. ' +
'Supported types are: ' +
', '.join([
'str', 'io.BufferedReader'
]))


def sentencepiece_numericalizer(sp_model):
Expand Down