Skip to content

Commit f5805b5

Browse files
committed
Add Python 2 string compatibility (treat unicode as a string type)
1 parent 0128246 commit f5805b5

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

_plotly_utils/basevalidators.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import base64
22
import numbers
3-
import six
43
import textwrap
54
import uuid
65
from importlib import import_module
@@ -14,6 +13,8 @@
1413
# ----------------
1514
import sys
1615

16+
from _plotly_utils.compat import str_types
17+
1718
np = None
1819
pd = None
1920

@@ -349,7 +350,7 @@ def __init__(self,
349350
# ----------------------------
350351
# Look for regular expressions
351352
for v in self.values:
352-
if v and isinstance(v, str) and v[0] == '/' and v[-1] == '/':
353+
if v and isinstance(v, str_types) and v[0] == '/' and v[-1] == '/':
353354
# String is a regex with leading and trailing '/' character
354355
regex_str = v[1:-1]
355356
self.val_regexs.append(re.compile(regex_str))
@@ -387,7 +388,7 @@ def perform_replacemenet(self, v):
387388
"""
388389
Return v with any applicable regex replacements applied
389390
"""
390-
if isinstance(v, str):
391+
if isinstance(v, str_types):
391392
for repl_args in self.regex_replacements:
392393
if repl_args:
393394
v = re.sub(repl_args[0], repl_args[1], v)
@@ -442,7 +443,7 @@ def in_values(self, e):
442443
"""
443444
Return whether a value matches one of the enumeration options
444445
"""
445-
is_str = isinstance(e, str)
446+
is_str = isinstance(e, str_types)
446447
for v, regex in zip(self.values, self.val_regexs):
447448
if is_str and regex:
448449
in_values = fullmatch(regex, e) is not None
@@ -821,7 +822,7 @@ def validate_coerce(self, v):
821822

822823
# If strict, make sure all elements are strings.
823824
if self.strict:
824-
invalid_els = [e for e in v if not isinstance(e, str)]
825+
invalid_els = [e for e in v if not isinstance(e, str_types)]
825826
if invalid_els:
826827
self.raise_invalid_elements(invalid_els)
827828

@@ -862,10 +863,10 @@ def validate_coerce(self, v):
862863

863864
else:
864865
if self.strict:
865-
if not isinstance(v, str):
866+
if not isinstance(v, str_types):
866867
self.raise_invalid_val(v)
867868
else:
868-
if not isinstance(v, (str, int, float)):
869+
if not isinstance(v, str_types + (int, float)):
869870
self.raise_invalid_val(v)
870871

871872
# Convert value to a string
@@ -1081,7 +1082,7 @@ def perform_validate_coerce(v, allow_number=None):
10811082
if isinstance(v, numbers.Number) and allow_number:
10821083
# If allow_numbers then any number is ok
10831084
return v
1084-
elif not isinstance(v, str):
1085+
elif not isinstance(v, str_types):
10851086
# If not allow_numbers then value must be a string
10861087
return None
10871088
else:
@@ -1203,7 +1204,7 @@ def validate_coerce(self, v):
12031204
pass
12041205
if v is None:
12051206
v_valid = True
1206-
elif isinstance(v, str):
1207+
elif isinstance(v, str_types):
12071208
v_match = [
12081209
el for el in ColorscaleValidator.named_colorscales
12091210
if el.lower() == v.lower()
@@ -1218,7 +1219,7 @@ def validate_coerce(self, v):
12181219
len(e) != 2 or
12191220
not isinstance(e[0], numbers.Number) or
12201221
not (0 <= e[0] <= 1) or
1221-
not isinstance(e[1], str) or
1222+
not isinstance(e[1], str_types) or
12221223
ColorValidator.perform_validate_coerce(e[1]) is None)]
12231224

12241225
if len(invalid_els) == 0:
@@ -1337,7 +1338,7 @@ def description(self):
13371338
def validate_coerce(self, v):
13381339
if v is None:
13391340
pass
1340-
elif not isinstance(v, str):
1341+
elif not isinstance(v, str_types):
13411342
self.raise_invalid_val(v)
13421343
else:
13431344
# match = re.fullmatch(self.regex, v)
@@ -1419,7 +1420,7 @@ def description(self):
14191420
return desc
14201421

14211422
def vc_scalar(self, v):
1422-
if not isinstance(v, str):
1423+
if not isinstance(v, str_types):
14231424
return None
14241425

14251426
# To be generous we accept flags separated on plus ('+'),
@@ -1660,7 +1661,7 @@ def description(self):
16601661
def validate_coerce(self, v):
16611662
if v is None:
16621663
pass
1663-
elif isinstance(v, str):
1664+
elif isinstance(v, str_types):
16641665
# Future possibilities:
16651666
# - Detect filesystem system paths and convert to URI
16661667
# - Validate either url or data uri

_plotly_utils/compat.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import six
2+
3+
str_types = six.string_types + (six.text_type,)

0 commit comments

Comments
 (0)