Skip to content

Commit 3de1345

Browse files
committed
Fix invalid import statement error messages
1 parent b6f3d6b commit 3de1345

2 files changed

Lines changed: 23 additions & 10 deletions

File tree

ymmsl/v0_2/imports.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,37 +105,40 @@ def _yatiml_sweeten(self, node: yatiml.Node) -> None:
105105

106106
@classmethod
107107
def _parse_string_representation(cls, text: str) -> Tuple[str, str, str]:
108-
help_text = 'Import statements should look like "from a.b.c import d"'
108+
help_text = 'Import statements should look like "from a.b.c import <kind> d"'
109109

110110
parts = text.split()
111111

112112
if len(parts) > 0 and parts[0] != 'from':
113113
raise RuntimeError(
114-
'Import statement "{text}" does not start with "from".\n' +
114+
f'Import statement "{text}" does not start with "from".\n' +
115115
help_text)
116116

117117
if len(parts) > 1:
118118
module = parts[1]
119119

120120
if len(parts) > 2 and parts[2] != 'import':
121121
raise RuntimeError(
122-
'Import statement "{text}" does not have "import" part.\n' +
122+
f'Import statement "{text}" does not have "import" part.\n' +
123123
help_text)
124124

125125
if len(parts) > 3:
126-
try:
127-
kind = parts[3]
128-
except KeyError:
129-
raise RuntimeError(
130-
f'Tried to import an invalid kind of object "{parts[3]}" in' +
131-
f' import statement "{text}".\n' + help_text)
126+
kind = parts[3]
127+
else:
128+
raise RuntimeError(
129+
f'Import statement "{text}" does not specify which kind of thing'
130+
' to import.\n' + help_text)
132131

133132
if len(parts) > 4:
134133
name = parts[4]
134+
else:
135+
raise RuntimeError(
136+
f'Import statement "{text}" does not specify the name of the thing'
137+
' to import.\n' + help_text)
135138

136139
if len(parts) > 5:
137140
raise RuntimeError(
138-
'Extra text found at end of import statement "{text}".\n' +
141+
f'Extra text found at end of import statement "{text}".\n' +
139142
help_text)
140143

141144
return module, kind, name

ymmsl/v0_2/tests/test_imports.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ def test_load_invalid_import6(load_import: LoadImport) -> None:
7171
load_import('from nlesc.examples[1] import implementation example')
7272

7373

74+
def test_load_invalid_import7(load_import: LoadImport) -> None:
75+
with pytest.raises(RuntimeError):
76+
load_import('from a.b import example')
77+
78+
79+
def test_load_invalid_import8(load_import: LoadImport) -> None:
80+
with pytest.raises(RuntimeError):
81+
load_import('from a.b import implementation')
82+
83+
7484
def test_module_path() -> None:
7585
imp = ImportStatement('nlesc.examples', 'implementation', 'test')
7686
assert imp.module_path() == Path('nlesc/examples.ymmsl')

0 commit comments

Comments
 (0)