Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proper error message for partially inferred types #170

Merged
merged 1 commit into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Loading