Skip to content

Commit

Permalink
add six, make unicode tests conditional on py version
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanLukes committed Jan 25, 2020
1 parent e6628bc commit 4c20209
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 15 deletions.
4 changes: 3 additions & 1 deletion baron/render.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import sys
import json

import six


def render(node, strict=False):
"""Recipe to render a given FST node.
Expand Down Expand Up @@ -69,7 +71,7 @@ def render_node(node, strict=False):
if key_type == "key":
assert isinstance(node[render_key], (dict, type(None))), "Key '%s' is expected to have type of 'key' (dict/None) but has type of '%s' instead" % (render_key, type(node[render_key]))
elif key_type == "string":
assert isinstance(node[render_key], str), "Key '%s' is expected to have type of 'string' but has type of '%s' instead" % (render_key, type(node[render_key]))
assert isinstance(node[render_key], six.string_types), "Key '%s' is expected to have type of 'string' but has type of '%s' instead" % (render_key, type(node[render_key]))
elif key_type in ("list", "formatting"):
assert isinstance(node[render_key], list), "Key '%s' is expected to have type of 'list' but has type of '%s' instead" % (render_key, type(node[render_key]))
elif key_type == "constant":
Expand Down
3 changes: 2 additions & 1 deletion baron/utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import sys

import regex as re
import six

python_version = sys.version_info[0]
python_subversion = sys.version_info[1]
string_instance = str if python_version == 3 else basestring
string_instance = six.string_types # alias, for isinstance usage in redbaron


class BaronError(Exception):
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
rply
regex
regex
six
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
long_description=read_md("README.md") + "\n\n" + open("CHANGELOG", "r").read(),
author_email='[email protected]',
url='https://github.com/PyCQA/baron',
install_requires=['rply', 'regex'],
install_requires=['rply', 'regex', 'six'],
packages=['baron'],
license='lgplv3+',
scripts=[],
Expand Down
7 changes: 4 additions & 3 deletions tests/test_path.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import six

from baron.baron import parse
from baron.path import PathWalker, Position, BoundingBox
from baron.path import position_to_path, path_to_node, position_to_node
from baron.path import path_to_bounding_box, node_to_bounding_box
from baron.utils import string_instance
from baron.path import position_to_path, path_to_node, position_to_node


def test_position():
Expand Down Expand Up @@ -145,7 +146,7 @@ def check_path(code, positions, target_path):
return

node = path_to_node(tree, path)
assert isinstance(node, string_instance)
assert isinstance(node, six.string_types)

assert position_to_node(tree, position) is node

Expand Down
9 changes: 5 additions & 4 deletions tests/test_spliter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/python
# -*- coding:Utf-8 -*-

import six

from baron.spliter import split, UntreatedError
from baron.utils import python_version
Expand Down Expand Up @@ -91,8 +91,9 @@ def test_assign():
assert split("a = b") == ["a", " ", "=", " ", "b"]


def test_assign_unicode():
assert split("α = β") == ["α", " ", "=", " ", "β"]
if six.PY3:
def test_assign_unicode():
assert split("α = β") == ["α", " ", "=", " ", "β"]


def test_call():
Expand Down Expand Up @@ -377,6 +378,6 @@ def test_regression():

# TODO: make this test pass in python3 also
# requires to remove dependency on ast.py
if python_version == 2:
if six.PY2:
def test_remove_crap():
assert split("\x0c\xef\xbb\xbf") == []
9 changes: 5 additions & 4 deletions tests/test_tokenizer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/python
# -*- coding:Utf-8 -*-

import six

from baron.tokenizer import tokenize, KEYWORDS

Expand All @@ -22,9 +22,10 @@ def test_name__():
match('_a', 'NAME')


def test_name_unicode():
match('β', 'NAME')
match('가사', 'NAME')
if six.PY3:
def test_name_unicode():
match('β', 'NAME')
match('가사', 'NAME')


def test_name_number():
Expand Down

0 comments on commit 4c20209

Please sign in to comment.