From a3a44ce3b902b7ebac182cc46bda3891ea043a00 Mon Sep 17 00:00:00 2001 From: CodeSteak Date: Sun, 31 Oct 2021 15:33:46 +0100 Subject: [PATCH 1/2] adding special case for missing return statement. --- python/deps/untypy/untypy/util/__init__.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/python/deps/untypy/untypy/util/__init__.py b/python/deps/untypy/untypy/util/__init__.py index dc46b1ac..d6e8f4de 100644 --- a/python/deps/untypy/untypy/util/__init__.py +++ b/python/deps/untypy/untypy/util/__init__.py @@ -2,9 +2,9 @@ import types from typing import Optional, Union, List -from untypy.util.display import IndicatorStr from untypy.error import UntypyTypeError, Frame, Location from untypy.interfaces import ExecutionContext, TypeChecker, WrappedFunction +from untypy.util.display import IndicatorStr from untypy.util.return_traces import get_last_return @@ -126,8 +126,15 @@ def wrap(self, err: UntypyTypeError) -> UntypyTypeError: declared = WrappedFunction.find_location(self.fn) responsable = declared + if responsable is not None: - responsable = responsable.narrow_in_span(self.reti_loc) + if err.expected is not None and err.given is None: + # Missing Return-Value? + err = err.with_note("Did you miss an return statement?") + last_line = responsable.line_no + responsable.line_span - 1 + responsable = responsable.narrow_in_span((responsable.file, last_line)) + else: + responsable = responsable.narrow_in_span(self.reti_loc) return err.with_frame(Frame( return_id.ty, From 339a5e08987b28eeab1cf45bc4d1d6cc55a3828d Mon Sep 17 00:00:00 2001 From: CodeSteak Date: Sun, 31 Oct 2021 15:34:34 +0100 Subject: [PATCH 2/2] test special case for missing return. --- python/fileTests | 1 + python/test-data/testMissingReturn.err | 14 ++++++++++++++ python/test-data/testMissingReturn.out | 0 python/test-data/testMissingReturn.py | 6 ++++++ 4 files changed, 21 insertions(+) create mode 100644 python/test-data/testMissingReturn.err create mode 100644 python/test-data/testMissingReturn.out create mode 100644 python/test-data/testMissingReturn.py diff --git a/python/fileTests b/python/fileTests index 4015ed16..fe0ff5ef 100755 --- a/python/fileTests +++ b/python/fileTests @@ -142,6 +142,7 @@ checkWithOutputAux yes 1 test-data/testForwardRef2.py checkWithOutputAux yes 0 test-data/testForwardRef3.py checkWithOutputAux yes 1 test-data/testForwardRef4.py checkWithOutputAux yes 1 test-data/testTypesReturn.py +checkWithOutputAux yes 1 test-data/testMissingReturn.py checkWithOutputAux yes 1 test-data/testTypesSequence1.py checkWithOutputAux yes 1 test-data/testTypesSequence2.py checkWithOutputAux yes 1 test-data/testTypesTuple1.py diff --git a/python/test-data/testMissingReturn.err b/python/test-data/testMissingReturn.err new file mode 100644 index 00000000..a1f39d58 --- /dev/null +++ b/python/test-data/testMissingReturn.err @@ -0,0 +1,14 @@ +Traceback (most recent call last): + File "test-data/testMissingReturn.py", line 6, in + print(billigStrom(500)) +WyppTypeError: got value of wrong type +Did you miss an return statement? + +given: None +expected: value of type float + +context: billigStrom(kwh: float) -> float + ^^^^^ +declared at: test-data/testMissingReturn.py:3 +caused by: test-data/testMissingReturn.py:4 + | pass diff --git a/python/test-data/testMissingReturn.out b/python/test-data/testMissingReturn.out new file mode 100644 index 00000000..e69de29b diff --git a/python/test-data/testMissingReturn.py b/python/test-data/testMissingReturn.py new file mode 100644 index 00000000..a6214c07 --- /dev/null +++ b/python/test-data/testMissingReturn.py @@ -0,0 +1,6 @@ +# See: https://github.com/skogsbaer/write-your-python-program/issues/15 + +def billigStrom(kwh: float) -> float: + pass + +print(billigStrom(500)) \ No newline at end of file