Skip to content

Commit

Permalink
Merge pull request #20 from mutpy/feature/python3.8-support
Browse files Browse the repository at this point in the history
Add support for Python 3.8
  • Loading branch information
phihos authored Oct 29, 2019
2 parents 0677320 + 1311c45 commit 16b1817
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 7 deletions.
5 changes: 1 addition & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@ language: python

matrix:
include:
- python: 2.6
- python: 2.7
- python: 3.3
- python: 3.4
- python: 3.5
- python: 3.6
- python: 3.7
dist: xenial
sudo: true
- python: 3.8
install:
# python 2.6 and 3.3 compatibility
- if [[ $TRAVIS_PYTHON_VERSION == 2.6 ]]; then pip install pycparser==2.14 cryptography==2.1.4; fi
Expand Down
17 changes: 16 additions & 1 deletion astmonkey/tests/test_visitors.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# -*- coding: utf-8 -*-
import sys

import pytest

try:
Expand Down Expand Up @@ -34,7 +36,10 @@ def test_node_label(self, visitor):
visitor.visit(node)

dot_node = visitor.graph.get_node(str(node.body[0].value))[0]
assert dot_node.get_label() == 'ast.Num(n=1)'
if sys.version_info >= (3, 8):
assert dot_node.get_label() == 'ast.Constant(value=1, kind=None)'
else:
assert dot_node.get_label() == 'ast.Num(n=1)'

def test_edge_label(self, visitor):
node = transformers.ParentChildNodeTransformer().visit(ast.parse('x = 1'))
Expand Down Expand Up @@ -350,6 +355,16 @@ class TestSourceGeneratorNodeVisitor(object):
"f'{x!a}'",
]

if utils.check_version(from_inclusive=(3, 8)):
roundtrip_testdata += [
# assignment expressions
'if n := len(a) > 10:' + EOL + INDENT + PASS,
# positional-only parameters
'def f(a, /, b, *, c):' + EOL + INDENT + PASS,
# positional-only parameters with defaults
'def f(a=1, /, b=2, *, c=3):' + EOL + INDENT + PASS,
]

# add additional tests for semantic testing
semantic_testdata = list(roundtrip_testdata)

Expand Down
48 changes: 47 additions & 1 deletion astmonkey/visitors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,51 @@ def visit_FormattedValue(self, node):
self.write('!%c' % (node.conversion,))


class SourceGeneratorNodeVisitorPython38(SourceGeneratorNodeVisitorPython36):
__python_version__ = (3, 8)

def visit_Constant(self, node):
if type(node.value) == str:
self.write(repr(node.s))
elif node.value == Ellipsis:
self.write('...')
else:
self.write(str(node.value))

def visit_NamedExpr(self, node):
self.visit(node.target)
self.write(' := ')
self.visit(node.value)

def signature(self, node, add_space=False):
write_comma = CommaWriter(self.write, add_space_at_beginning=add_space)


defaults = list(node.defaults)

if node.posonlyargs:
padding = [None] * (len(node.posonlyargs) - len(node.defaults))
for arg, default in zip(node.posonlyargs, padding + defaults[:len(node.posonlyargs)]):
self.signature_arg(arg, default, write_comma)
self.write(', /')
defaults = defaults[len(node.posonlyargs):]

padding = [None] * (len(node.args) - len(node.defaults))
for arg, default in zip(node.args, padding + defaults):
self.signature_arg(arg, default, write_comma)

self.signature_spec_arg(node, 'vararg', write_comma, prefix='*')
self.signature_kwonlyargs(node, write_comma)
self.signature_spec_arg(node, 'kwarg', write_comma, prefix='**')

@classmethod
def _get_actual_lineno(cls, node):
if isinstance(node, ast.FunctionDef) and node.decorator_list:
return node.decorator_list[0].lineno
else:
return SourceGeneratorNodeVisitorPython36._get_actual_lineno(node)


SourceGeneratorNodeVisitor = utils.get_by_python_version([
SourceGeneratorNodeVisitorPython26,
SourceGeneratorNodeVisitorPython27,
Expand All @@ -1022,5 +1067,6 @@ def visit_FormattedValue(self, node):
SourceGeneratorNodeVisitorPython33,
SourceGeneratorNodeVisitorPython34,
SourceGeneratorNodeVisitorPython35,
SourceGeneratorNodeVisitorPython36
SourceGeneratorNodeVisitorPython36,
SourceGeneratorNodeVisitorPython38
])
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'License :: OSI Approved :: Apache Software License'
]
)
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tox]
envlist =
coverage-erase
test-py{26,27,33,34,35,36,37}
test-py{27,34,35,36,37,38}
coverage-report
[testenv]
deps=
Expand Down

0 comments on commit 16b1817

Please sign in to comment.