Skip to content
New issue

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

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

Already on GitHub? # to your account

Add rudimentary support for python files that use type hints #56

Merged
merged 1 commit into from
Jan 12, 2021
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
6 changes: 6 additions & 0 deletions pscript/commonast.py
Original file line number Diff line number Diff line change
Expand Up @@ -1034,6 +1034,12 @@ def _convert_Assign(self, n):
def _convert_AugAssign(self, n):
op = n.op.__class__.__name__
return AugAssign(self._convert(n.target), op, self._convert(n.value))

def _convert_AnnAssign(self, n):
if n.value is None:
raise RuntimeError("Cannot convert AnnAssign nodes with no assignment!")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed this to RuntimeError because that's what all other errors in this file were.

c = self._convert
return Assign([c(n.target)], c(n.value))

def _convert_Print(self, n): # pragma: no cover - Python 2.x compat
c = self._convert
Expand Down
3 changes: 3 additions & 0 deletions pscript/parser1.py
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,9 @@ def parse_Import(self, node):
return [] # stuff to help the parser
if node.root == 'time':
return [] # PScript natively supports time() and perf_counter()
if node.root == 'typing':
# User is probably importing type annotations. Ignore this import.
return []
raise JSError('PScript does not support imports.')

def parse_Module(self, node):
Expand Down
18 changes: 18 additions & 0 deletions pscript/tests/test_commonast.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,4 +379,22 @@ def test_python_33_plus():
assert isinstance(node, commonast.YieldFrom)


@skipif(sys.version_info < (3,5), reason='Need Python 3.5+')
def test_annotated_assignments():
# Verify that we treat annotated assignments as regular assingments
code = "foo: int = 3"
node = commonast.parse(code).body_nodes[0]

assert isinstance(node, commonast.Assign)
assert len(node.target_nodes) == 1
assert isinstance(node.target_nodes[0], commonast.Name)
assert node.target_nodes[0].name == "foo"
assert isinstance(node.value_node, commonast.Num)
assert node.value_node.value == 3

# Verify that we do not support annotated assignments with no value
with raises(RuntimeError):
commonast.parse("foo: int")


run_tests_if_main()