Skip to content
This repository has been archived by the owner on Oct 6, 2020. It is now read-only.

Commit

Permalink
Improve exception messages when receiving unexpected ast type
Browse files Browse the repository at this point in the history
  • Loading branch information
mschwager committed Aug 16, 2019
1 parent c1bfd1c commit 3101c06
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
2 changes: 1 addition & 1 deletion dlint/namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def from_module_node(cls, module_node):
# hole of looking at things like function and class-scope imports and
# conditional imports in 'if' or 'try' statements
if not isinstance(module_node, ast.Module):
raise TypeError("only module-level imports are supported")
raise TypeError('expected type ast.Module, received {}'.format(type(module_node)))

imports = []
from_imports = []
Expand Down
4 changes: 2 additions & 2 deletions dlint/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def decorator_name(decorator):
# E.g. @decorator(argument)
return decorator.func.id
else:
raise TypeError(type(decorator))
raise TypeError('expected type ast.Attribute, ast.Name, or ast.Call, received {}'.format(type(decorator)))


def call_name(call):
Expand All @@ -36,7 +36,7 @@ def call_name(call):
# E.g. func_call(argument)
return call.func.id
else:
raise TypeError(type(call.func))
raise TypeError('expected type ast.Attribute or ast.Name, received {}'.format(type(call.func)))


def function_has_inlinecallbacks_decorator(function):
Expand Down
8 changes: 8 additions & 0 deletions tests/test_namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

import unittest

import pytest

import dlint


Expand Down Expand Up @@ -101,6 +103,12 @@ def test_as_from_import_mismatch(self):

assert result == expected

def test_from_module_node_unknown_type(self):
unknown_type = None

with pytest.raises(TypeError):
dlint.namespace.Namespace.from_module_node(unknown_type)


if __name__ == "__main__":
unittest.main()

0 comments on commit 3101c06

Please sign in to comment.