From 1b6ffc12c0d66dae15960a0089c8cedc7291ac88 Mon Sep 17 00:00:00 2001 From: serge-sans-paille Date: Sat, 23 Nov 2024 22:12:44 +0100 Subject: [PATCH] Provide user hint when trying to use c-style type names in pythran spec --- pythran/spec.py | 15 +++++++++++++-- pythran/tests/notebooks/export.ipynb | 2 +- pythran/tests/test_spec_parser.py | 5 +++++ 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/pythran/spec.py b/pythran/spec.py index 694094083..838f99ccd 100644 --- a/pythran/spec.py +++ b/pythran/spec.py @@ -454,12 +454,23 @@ def PythranSpecError(self, msg, lexpos=None): def p_error(self, p): if p.type == 'IDENTIFIER': + alt = {'double': 'float64', + 'void': 'None', + 'char': 'int8', + 'short': 'int16', + 'long': 'int64', + }.get(p.value) + if alt: + hint = " Did you mean `{}`?".format(alt) + else: + hint = '' raise self.PythranSpecError( - "Unexpected identifier `{}` at that point".format(p.value), + "Unsupported identifier `{}` at that point.{}".format(p.value, + hint), p.lexpos) else: raise self.PythranSpecError( - "Unexpected token `{}` at that point".format(p.value), + "Unexpected token `{}` at that point.".format(p.value), p.lexpos) def __init__(self): diff --git a/pythran/tests/notebooks/export.ipynb b/pythran/tests/notebooks/export.ipynb index 77997dd11..ea8245280 100644 --- a/pythran/tests/notebooks/export.ipynb +++ b/pythran/tests/notebooks/export.ipynb @@ -812,7 +812,7 @@ "name": "stdout", "output_type": "stream", "text": [ - ":2:27 error: Unexpected token `[` at that point\n" + ":2:27 error: Unexpected token `[` at that point.\n" ] } ], diff --git a/pythran/tests/test_spec_parser.py b/pythran/tests/test_spec_parser.py index b06f7b645..ef89f1368 100644 --- a/pythran/tests/test_spec_parser.py +++ b/pythran/tests/test_spec_parser.py @@ -72,6 +72,11 @@ def test_invalid_specs5(self): with self.assertRaises(pythran.syntax.PythranSyntaxError): pythran.compile_pythrancode("dumber", code) + def test_invalid_specs_with_hint(self): + code = '#pythran export bar(double)\ndef bar(x):pass' + with self.assertRaises(pythran.syntax.PythranSyntaxError) as e: + pythran.compile_pythrancode("dumber", code) + self.assertTrue('float64' in e.exception.msg) def test_multiline_spec0(self): code = '''