Skip to content

Commit 24580b7

Browse files
authored
fix: type error location (#1169)
Signed-off-by: Louis Mandel <[email protected]>
1 parent 1bacaa5 commit 24580b7

File tree

5 files changed

+42
-22
lines changed

5 files changed

+42
-22
lines changed

src/pdl/pdl_interpreter.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -373,14 +373,6 @@ def process_block(
373373
return result, background, scope, trace
374374

375375

376-
def context_in_contribute(block: AdvancedBlockType) -> bool:
377-
if ContributeTarget.CONTEXT.value in block.contribute:
378-
return True
379-
if get_contribute_context_value(block.contribute) is not None:
380-
return True
381-
return False
382-
383-
384376
# A start-end time wrapper around `process_advanced_block`
385377
def process_advanced_block_timed(
386378
state: InterpreterState,
@@ -520,7 +512,7 @@ def process_advanced_block( # noqa:C901
520512
result_with_type_checking,
521513
spec=block.spec,
522514
msg="Type errors during spec checking:",
523-
loc=loc,
515+
loc=append(loc, "spec"),
524516
trace=trace,
525517
)
526518
result = lazy_apply(checker, result)
@@ -589,6 +581,14 @@ def process_advanced_block( # noqa:C901
589581
return result, background, new_scope, trace
590582

591583

584+
def context_in_contribute(block: AdvancedBlockType) -> bool:
585+
if ContributeTarget.CONTEXT.value in block.contribute:
586+
return True
587+
if get_contribute_context_value(block.contribute) is not None:
588+
return True
589+
return False
590+
591+
592592
ResultWithTypeCheckingT = TypeVar("ResultWithTypeCheckingT")
593593

594594

tests/data/line/hello32.pdl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
defs:
2+
x:
3+
data: 1
4+
spec: string
5+
text: ${x}

tests/test_examples_run.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,4 @@ expected_runtime_error:
118118
- tests/data/line/hello28.pdl
119119
- tests/data/line/hello29.pdl
120120
- tests/data/line/hello30.pdl
121+
- tests/data/line/hello32.pdl

tests/test_line_table.py

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ def test_line1(capsys: CaptureFixture[str]):
4444
"file": "tests/data/line/hello3.pdl",
4545
"errors": [
4646
"",
47-
"tests/data/line/hello3.pdl:6 - Type errors during spec checking:",
48-
"tests/data/line/hello3.pdl:6 - World! should be of type <class 'int'>",
47+
"tests/data/line/hello3.pdl:7 - Type errors during spec checking:",
48+
"tests/data/line/hello3.pdl:7 - World! should be of type <class 'int'>",
4949
],
5050
}
5151

@@ -100,8 +100,8 @@ def test_line8(capsys: CaptureFixture[str]):
100100
"file": "tests/data/line/hello9.pdl",
101101
"errors": [
102102
"",
103-
"tests/data/line/hello9.pdl:3 - Type errors during spec checking:",
104-
"tests/data/line/hello9.pdl:3 - hello should be of type <class 'int'>",
103+
"tests/data/line/hello9.pdl:4 - Type errors during spec checking:",
104+
"tests/data/line/hello9.pdl:4 - hello should be of type <class 'int'>",
105105
],
106106
}
107107

@@ -140,8 +140,8 @@ def test_line11(capsys: CaptureFixture[str]):
140140
"file": "tests/data/line/hello12.pdl",
141141
"errors": [
142142
"",
143-
"tests/data/line/hello12.pdl:9 - Type errors during spec checking:",
144-
"tests/data/line/hello12.pdl:9 - How are you? should be of type <class 'bool'>",
143+
"tests/data/line/hello12.pdl:11 - Type errors during spec checking:",
144+
"tests/data/line/hello12.pdl:11 - How are you? should be of type <class 'bool'>",
145145
],
146146
}
147147

@@ -154,8 +154,8 @@ def test_line12(capsys: CaptureFixture[str]):
154154
"file": "tests/data/line/hello13.pdl",
155155
"errors": [
156156
"",
157-
"tests/data/line/hello13.pdl:9 - Type errors during spec checking:",
158-
"tests/data/line/hello13.pdl:9 - 1 should be of type <class 'str'>",
157+
"tests/data/line/hello13.pdl:12 - Type errors during spec checking:",
158+
"tests/data/line/hello13.pdl:12 - 1 should be of type <class 'str'>",
159159
],
160160
}
161161

@@ -195,8 +195,8 @@ def test_line15(capsys: CaptureFixture[str]):
195195
"file": "tests/data/line/hello16.pdl",
196196
"errors": [
197197
"",
198-
"tests/data/line/hello16.pdl:8 - Type errors during spec checking:",
199-
"tests/data/line/hello16.pdl:8 - 30 should be of type <class 'str'>",
198+
"tests/data/line/hello16.pdl:10 - Type errors during spec checking:",
199+
"tests/data/line/hello16.pdl:10 - 30 should be of type <class 'str'>",
200200
],
201201
}
202202

@@ -209,8 +209,8 @@ def test_line16(capsys: CaptureFixture[str]):
209209
"file": "tests/data/line/hello17.pdl",
210210
"errors": [
211211
"",
212-
"tests/data/line/hello17.pdl:3 - Type errors during spec checking:",
213-
"tests/data/line/hello17.pdl:3 - hello should be of type <class 'int'>",
212+
"tests/data/line/hello17.pdl:4 - Type errors during spec checking:",
213+
"tests/data/line/hello17.pdl:4 - hello should be of type <class 'int'>",
214214
],
215215
}
216216

@@ -405,3 +405,17 @@ def test_line30(capsys: CaptureFixture[str]):
405405

406406
def test_line31(capsys: CaptureFixture[str]):
407407
do_test(line31, capsys)
408+
409+
410+
line32 = {
411+
"file": "tests/data/line/hello32.pdl",
412+
"errors": [
413+
"",
414+
"tests/data/line/hello32.pdl:4 - Type errors during spec checking:",
415+
"tests/data/line/hello32.pdl:4 - 1 should be of type <class 'str'>",
416+
],
417+
}
418+
419+
420+
def test_line32(capsys: CaptureFixture[str]):
421+
do_test(line32, capsys)

tests/test_runtime_errors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def test_type_result():
7979
exec_str(prog_str)
8080
assert (
8181
str(exc.value.message)
82-
== "Type errors during spec checking:\nline 0 - Hello should be of type <class 'int'>"
82+
== "Type errors during spec checking:\nline 3 - Hello should be of type <class 'int'>"
8383
)
8484

8585

0 commit comments

Comments
 (0)