Skip to content

Commit

Permalink
Proper error message for partially inferred types
Browse files Browse the repository at this point in the history
  • Loading branch information
marcoeilers committed Feb 15, 2024
1 parent 98a1d3c commit f13aa59
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/nagini_translation/analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1219,6 +1219,16 @@ def convert_type(self, mypy_type, node) -> PythonType:
return self._convert_callable_type(mypy_type, node)
elif self.types.is_type_alias_type(mypy_type):
return self.convert_type(mypy_type.alias.target, node)
elif self.types.is_partial_type(mypy_type):
try:
partial_type = self.convert_type(mypy_type.type, node)
except:
partial_type = None
if partial_type:
msg = f'Type {partial_type.python_class.name} could not be fully inferred (this usually means that a type argument is unknown)'
else:
msg = f'Type could not be fully inferred (this usually means that a type argument is unknown)'
raise InvalidProgramException(node, 'partial.type', message=msg)
else:
msg = 'Unsupported type: {}'.format(mypy_type.__class__.__name__)
raise UnsupportedException(node, desc=msg)
Expand Down
3 changes: 3 additions & 0 deletions src/nagini_translation/lib/typeinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,9 @@ def is_type_type(self, type: mypy.types.Type) -> bool:
def is_type_alias_type(self, type: mypy.types.Type) -> bool:
return isinstance(type, mypy.types.TypeAliasType)

def is_partial_type(self, type: mypy.types.Type) -> bool:
return isinstance(type, mypy.types.PartialType)

def is_any_type_from_error(self, type: mypy.types.Type) -> bool:
if isinstance(type, mypy.types.AnyType):
if type.type_of_any == mypy.types.TypeOfAny.from_error:
Expand Down
12 changes: 12 additions & 0 deletions tests/functional/translation/test_partial_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from typing import List


def get_odd_collatz(n: int) -> List[int]:
#:: ExpectedOutput(invalid.program:partial.type)
ans, x = [], n
while x != 1:
if x % 2 == 1:
ans.append(x)
x = x // 2 if x % 2 == 0 else x * 3 + 1
ans.append(1)
return sorted(ans)

0 comments on commit f13aa59

Please sign in to comment.