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

Fix some Python 3.8 issues #29

Closed
wants to merge 1 commit into from
Closed
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
19 changes: 12 additions & 7 deletions tests/test_mark_tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import textwrap
import unittest
from . import tools
from .tools import Constant


class TestMarkTokens(unittest.TestCase):
Expand Down Expand Up @@ -181,11 +182,14 @@ def test_deep_recursion(self):
m = self.create_mark_checker(source)
self.assertEqual(len(m.all_nodes), 2104)
self.assertEqual(m.view_node(m.all_nodes[-1]),
"Str:'F1akOFFiRIgPHTZksKBAgMCLGTdGNIAAQgKfDAcgZbj0odOnUA8GBAA7'")
"%s:'F1akOFFiRIgPHTZksKBAgMCLGTdGNIAAQgKfDAcgZbj0odOnUA8GBAA7'"
% Constant('Str'))
self.assertEqual(m.view_node(m.all_nodes[-2]),
"Str:'Ii0uLDAxLzI0Mh44U0gxMDI5JkM0JjU3NDY6Kjc5Njo7OUE8Ozw+Oz89QTxA'")
"%s:'Ii0uLDAxLzI0Mh44U0gxMDI5JkM0JjU3NDY6Kjc5Njo7OUE8Ozw+Oz89QTxA'"
% Constant('Str'))
self.assertEqual(m.view_node(m.all_nodes[1053]),
"Str:'R0lGODlhigJnAef/AAABAAEEAAkCAAMGAg0GBAYJBQoMCBMODQ4QDRITEBkS'")
"%s:'R0lGODlhigJnAef/AAABAAEEAAkCAAMGAg0GBAYJBQoMCBMODQ4QDRITEBkS'"
% Constant('Str'))
self.assertEqual(m.view_node(m.all_nodes[1052]),
"BinOp:'R0lGODlhigJnAef/AAABAAEEAAkCAAMGAg0GBAYJBQoMCBMODQ4QDRITEBkS'\r\n" +
" +'CxsSEhkWDhYYFQ0aJhkaGBweGyccGh8hHiIkIiMmGTEiHhQoPSYoJSkqKDcp'")
Expand Down Expand Up @@ -225,7 +229,7 @@ def test_adjacent_strings(self):
)
"""
m = self.create_mark_checker(source)
node_name = 'Const' if self.is_astroid_test else 'Str'
node_name = 'Const' if self.is_astroid_test else Constant('Str')
self.assertEqual(m.view_nodes_at(2, 6), {
node_name + ":'x y z' \\\n'''a b c''' \"u v w\""
})
Expand Down Expand Up @@ -336,7 +340,8 @@ def test_conditional_expr(self):
name_a = 'AssignName:a' if self.is_astroid_test else 'Name:a'
const_true = ('Const:True' if self.is_astroid_test else
'Name:True' if six.PY2 else
'NameConstant:True')
'%s:True'
% Constant('NameConstant'))
self.assertEqual(m.view_nodes_at(1, 0),
{name_a, "Assign:a = True if True else False", "Module:" + source})
self.assertEqual(m.view_nodes_at(1, 4),
Expand Down Expand Up @@ -388,7 +393,7 @@ def test_del_dict(self):
if self.is_astroid_test:
self.assertEqual(m.view_nodes_at(1, 5), {'Const:4'})
else:
self.assertEqual(m.view_nodes_at(1, 5), {'Num:4'})
self.assertEqual(m.view_nodes_at(1, 5), {'%s:4' % Constant('Num')})
self.assertEqual(m.view_nodes_at(2, 0), {'Delete:del x[4]'})
self.assertEqual(m.view_nodes_at(2, 4), {'Name:x', 'Subscript:x[4]'})

Expand Down Expand Up @@ -428,7 +433,7 @@ def test_keyword_arg_only(self):
self.assertEqual(m.view_nodes_at(2, 8), {'Keyword:b=[y]'})
else:
self.assertEqual(m.view_nodes_at(1, 2), {'keyword:x=1'})
self.assertEqual(m.view_nodes_at(1, 4), {'Num:1'})
self.assertEqual(m.view_nodes_at(1, 4), {'%s:1' % Constant('Num')})
self.assertEqual(m.view_nodes_at(2, 2), {'keyword:a=(x)'})
self.assertEqual(m.view_nodes_at(2, 8), {'keyword:b=[y]'})

Expand Down
12 changes: 7 additions & 5 deletions tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
from __future__ import unicode_literals, print_function
import ast
import astroid
import sys
import unittest
from .context import asttokens
from .tools import Constant

class TestUtil(unittest.TestCase):

Expand Down Expand Up @@ -48,13 +50,13 @@ def view(node):
'Call:bar(1 + 2)',
'Name:bar',
'BinOp:1 + 2',
'Num:1',
'Num:2',
'%s:1' % Constant('Num'),
'%s:2' % Constant('Num'),
"BinOp:'hello' + ', ' + 'world'",
"BinOp:'hello' + ', '",
"Str:'hello'",
"Str:', '",
"Str:'world'"
"%s:'hello'" % Constant('Str'),
"%s:', '" % Constant('Str'),
"%s:'world'" % Constant('Str'),
])

def test_walk_astroid(self):
Expand Down
11 changes: 11 additions & 0 deletions tests/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,14 @@ def verify_all_nodes(self, test_case):
tested_nodes += 1

return tested_nodes


def Constant(name):
Copy link
Member

Choose a reason for hiding this comment

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

What do you think of changing MarkChecker.view_node to replace node.__class__.__name__ with get_node_name(node) which is something like this:

def get_node_name(node):
   name = node.__class__.__name__
   return "Constant" if name in ("Num", "Str", "NameConstant") else name

Then we can change code throughout to expect things like Constant:17 or Constant:'hello', which seems a bit simpler.

"""Helper to write tests working on all Python versions.

Return 'Constant' on Python 3.8 and newer, or name on older Python.
"""
if sys.version_info >= (3, 8):
return 'Constant'
else:
return name
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ deps =
coverage
py{27}: astroid
py{35,36}: astroid<2
py{37}: astroid>=2.dev
py{37,38}: astroid>=2.dev