Skip to content
This repository was archived by the owner on Mar 6, 2026. It is now read-only.

Commit 133acf8

Browse files
authored
fix: use six string_types and integer_types for all isinstance() checks (#323)
* fix: use six string_types and integer_types for all isinstance() checks.
1 parent d1a4800 commit 133acf8

6 files changed

Lines changed: 26 additions & 10 deletions

File tree

google/cloud/ndb/_gql.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import re
2+
import six
23

34
from google.cloud.ndb import exceptions
45
from google.cloud.ndb import query as query_module
@@ -659,7 +660,7 @@ def _args_to_val(self, func, args):
659660
"""
660661
vals = []
661662
for arg in args:
662-
if isinstance(arg, (int, str)):
663+
if isinstance(arg, six.string_types + six.integer_types):
663664
val = query_module.Parameter(arg)
664665
else:
665666
val = arg.Get()

google/cloud/ndb/context.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import collections
1919
import contextlib
20+
import six
2021
import threading
2122

2223
from google.cloud.ndb import _eventloop
@@ -459,7 +460,7 @@ def set_global_cache_timeout_policy(self, policy):
459460
if policy is None:
460461
policy = _default_global_cache_timeout_policy
461462

462-
elif isinstance(policy, int):
463+
elif isinstance(policy, six.integer_types):
463464
timeout = policy
464465

465466
def policy(key):

google/cloud/ndb/key.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1421,7 +1421,7 @@ def _clean_flat_path(flat):
14211421
if isinstance(kind, type):
14221422
kind = kind._get_kind()
14231423
flat[i] = kind
1424-
if not isinstance(kind, str):
1424+
if not isinstance(kind, six.string_types):
14251425
raise TypeError(
14261426
"Key kind must be a string or Model class; "
14271427
"received {!r}".format(kind)

google/cloud/ndb/model.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,7 +1061,7 @@ def _verify_name(name):
10611061
TypeError: If the ``name`` is not a string.
10621062
ValueError: If the name contains a ``.``.
10631063
"""
1064-
if not isinstance(name, str):
1064+
if not isinstance(name, six.string_types):
10651065
raise TypeError("Name {!r} is not a string".format(name))
10661066

10671067
if "." in name:
@@ -2334,7 +2334,7 @@ def _validate(self, value):
23342334
.BadValueError: If ``value`` is not a :class:`float` or convertible
23352335
to one.
23362336
"""
2337-
if not isinstance(value, (float, int)):
2337+
if not isinstance(value, six.integer_types + (float,)):
23382338
raise exceptions.BadValueError(
23392339
"Expected float, got {!r}".format(value)
23402340
)
@@ -4871,7 +4871,7 @@ def _fix_up_properties(cls):
48714871
an underscore.
48724872
"""
48734873
kind = cls._get_kind()
4874-
if not isinstance(kind, str):
4874+
if not isinstance(kind, six.string_types):
48754875
raise KindError(
48764876
"Class {} defines a ``_get_kind()`` method that returns "
48774877
"a non-string ({!r})".format(cls.__name__, kind)
@@ -5653,7 +5653,7 @@ def _get_or_insert_async(
56535653
tasklets.Future: Model: The entity that was either just retrieved
56545654
or created.
56555655
"""
5656-
if not isinstance(name, str):
5656+
if not isinstance(name, six.string_types):
56575657
raise TypeError(
56585658
"'name' must be a string; received {!r}".format(name)
56595659
)

google/cloud/ndb/query.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ class Parameter(ParameterizedThing):
308308
__slots__ = ("_key",)
309309

310310
def __init__(self, key):
311-
if not isinstance(key, (int, str)):
311+
if not isinstance(key, six.integer_types + six.string_types):
312312
raise TypeError(
313313
"Parameter key must be an integer or string, not {}".format(
314314
key
@@ -1681,7 +1681,7 @@ def _to_property_names(self, properties):
16811681

16821682
fixed = []
16831683
for prop in properties:
1684-
if isinstance(prop, str):
1684+
if isinstance(prop, six.string_types):
16851685
fixed.append(prop)
16861686
elif isinstance(prop, model.Property):
16871687
fixed.append(prop._name)
@@ -1704,7 +1704,7 @@ def _to_property_orders(self, order_by):
17041704
elif isinstance(order, model.Property):
17051705
# use the sign to turn it into a PropertyOrder
17061706
orders.append(+order)
1707-
elif isinstance(order, str):
1707+
elif isinstance(order, six.string_types):
17081708
name = order
17091709
reverse = False
17101710
if order.startswith("-"):

tests/unit/test_key.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,20 @@ def test_constructor_default():
4949
)
5050
assert key._reference is None
5151

52+
@staticmethod
53+
@pytest.mark.usefixtures("in_context")
54+
def test_constructor_with_unicode():
55+
"""Regression test for #322.
56+
57+
https://github.com/googleapis/python-ndb/issues/322
58+
"""
59+
key = key_module.Key(u"Kind", 42)
60+
61+
assert key._key == google.cloud.datastore.Key(
62+
u"Kind", 42, project="testing"
63+
)
64+
assert key._reference is None
65+
5266
@staticmethod
5367
@pytest.mark.usefixtures("in_context")
5468
def test_constructor_with_different_namespace(context):

0 commit comments

Comments
 (0)