Skip to content

Commit e17129a

Browse files
authored
fix: Remove uses of six. #913 (#958)
1 parent 70ebac1 commit e17129a

File tree

6 files changed

+39
-47
lines changed

6 files changed

+39
-47
lines changed

google/cloud/ndb/_gql.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import datetime
22
import re
3-
import six
43
import time
54

65
from google.cloud.ndb import context as context_module
@@ -656,7 +655,7 @@ def _args_to_val(self, func, args):
656655
"""
657656
vals = []
658657
for arg in args:
659-
if isinstance(arg, six.string_types + six.integer_types):
658+
if isinstance(arg, (str, int)):
660659
val = query_module.Parameter(arg)
661660
else:
662661
val = arg.Get()
@@ -782,7 +781,7 @@ def _raise_cast_error(message):
782781
def _time_function(values):
783782
if len(values) == 1:
784783
value = values[0]
785-
if isinstance(value, six.string_types):
784+
if isinstance(value, str):
786785
try:
787786
time_tuple = time.strptime(value, "%H:%M:%S")
788787
except ValueError as error:
@@ -791,7 +790,7 @@ def _time_function(values):
791790
)
792791
time_tuple = time_tuple[3:]
793792
time_tuple = time_tuple[0:3]
794-
elif isinstance(value, six.integer_types):
793+
elif isinstance(value, int):
795794
time_tuple = (value,)
796795
else:
797796
_raise_cast_error("Invalid argument for time(), {}".format(value))
@@ -808,7 +807,7 @@ def _time_function(values):
808807
def _date_function(values):
809808
if len(values) == 1:
810809
value = values[0]
811-
if isinstance(value, six.string_types):
810+
if isinstance(value, str):
812811
try:
813812
time_tuple = time.strptime(value, "%Y-%m-%d")[0:6]
814813
except ValueError as error:
@@ -830,7 +829,7 @@ def _date_function(values):
830829
def _datetime_function(values):
831830
if len(values) == 1:
832831
value = values[0]
833-
if isinstance(value, six.string_types):
832+
if isinstance(value, str):
834833
try:
835834
time_tuple = time.strptime(value, "%Y-%m-%d %H:%M:%S")[0:6]
836835
except ValueError as error:

google/cloud/ndb/context.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import contextvars
2121
import itertools
2222
import os
23-
import six
2423
import threading
2524
import uuid
2625

@@ -550,7 +549,7 @@ def set_global_cache_timeout_policy(self, policy):
550549
if policy is None:
551550
policy = _default_global_cache_timeout_policy
552551

553-
elif isinstance(policy, six.integer_types):
552+
elif isinstance(policy, int):
554553
timeout = policy
555554

556555
def policy(key):

google/cloud/ndb/key.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@
9090

9191
import base64
9292
import functools
93-
import six
9493

9594
from google.cloud.datastore import _app_engine_key_pb2
9695
from google.cloud.datastore import key as _key_module
@@ -1245,7 +1244,7 @@ def _from_urlsafe(urlsafe, app, namespace, database):
12451244
Tuple[google.cloud.datastore.key.Key, .Reference]: The key
12461245
corresponding to ``urlsafe`` and the Reference protobuf.
12471246
"""
1248-
if isinstance(urlsafe, six.string_types): # pragma: NO BRANCH
1247+
if isinstance(urlsafe, str): # pragma: NO BRANCH
12491248
urlsafe = urlsafe.encode("ascii")
12501249
padding = b"=" * (-len(urlsafe) % 4)
12511250
urlsafe += padding
@@ -1526,7 +1525,7 @@ def _clean_flat_path(flat):
15261525
if isinstance(kind, type):
15271526
kind = kind._get_kind()
15281527
flat[i] = kind
1529-
if not isinstance(kind, six.string_types):
1528+
if not isinstance(kind, str):
15301529
raise TypeError(
15311530
"Key kind must be a string or Model class; "
15321531
"received {!r}".format(kind)
@@ -1537,7 +1536,7 @@ def _clean_flat_path(flat):
15371536
if id_ is None:
15381537
if i + 2 < len(flat):
15391538
raise exceptions.BadArgumentError("Incomplete Key entry must be last")
1540-
elif not isinstance(id_, six.string_types + six.integer_types):
1539+
elif not isinstance(id_, (str, int)):
15411540
raise TypeError(_INVALID_ID_TYPE.format(id_))
15421541

15431542
# Remove trailing ``None`` for a partial key.

google/cloud/ndb/model.py

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,6 @@ class Person(Model):
257257
import inspect
258258
import json
259259
import pickle
260-
import six
261260
import zlib
262261

263262
import pytz
@@ -1069,7 +1068,7 @@ def _verify_name(name):
10691068
TypeError: If the ``name`` is not a string.
10701069
ValueError: If the name contains a ``.``.
10711070
"""
1072-
if not isinstance(name, six.string_types):
1071+
if not isinstance(name, str):
10731072
raise TypeError("Name {!r} is not a string".format(name))
10741073

10751074
if "." in name:
@@ -2102,7 +2101,7 @@ def _legacy_db_get_value(v, p):
21022101
# If this passes, don't return unicode.
21032102
except UnicodeDecodeError:
21042103
try:
2105-
sval = six.text_type(sval.decode("utf-8"))
2104+
sval = str(sval.decode("utf-8"))
21062105
except UnicodeDecodeError:
21072106
pass
21082107
return sval
@@ -2435,7 +2434,7 @@ def _validate(self, value):
24352434
.BadValueError: If ``value`` is not an :class:`int` or convertible
24362435
to one.
24372436
"""
2438-
if not isinstance(value, six.integer_types):
2437+
if not isinstance(value, int):
24392438
raise exceptions.BadValueError(
24402439
"In field {}, expected integer, got {!r}".format(self._name, value)
24412440
)
@@ -2467,14 +2466,14 @@ def _validate(self, value):
24672466
.BadValueError: If ``value`` is not a :class:`float` or convertible
24682467
to one.
24692468
"""
2470-
if not isinstance(value, six.integer_types + (float,)):
2469+
if not isinstance(value, (float, int)):
24712470
raise exceptions.BadValueError(
24722471
"In field {}, expected float, got {!r}".format(self._name, value)
24732472
)
24742473
return float(value)
24752474

24762475

2477-
class _CompressedValue(six.binary_type):
2476+
class _CompressedValue(bytes):
24782477
"""A marker object wrapping compressed values.
24792478
24802479
Args:
@@ -2784,7 +2783,7 @@ def _validate(self, value):
27842783
.BadValueError: If the current property is indexed but the UTF-8
27852784
encoded value exceeds the maximum length (1500 bytes).
27862785
"""
2787-
if not isinstance(value, six.text_type):
2786+
if not isinstance(value, str):
27882787
# In Python 2.7, bytes is a synonym for str
27892788
if isinstance(value, bytes):
27902789
try:
@@ -2811,7 +2810,7 @@ def _to_base_type(self, value):
28112810
:class:`str`, this will return the UTF-8 encoded bytes for it.
28122811
Otherwise, it will return :data:`None`.
28132812
"""
2814-
if isinstance(value, six.text_type):
2813+
if isinstance(value, str):
28152814
return value.encode("utf-8")
28162815

28172816
def _from_base_type(self, value):
@@ -2946,7 +2945,7 @@ def _validate(self, value):
29462945
.BadValueError: If the current property is indexed but the UTF-8
29472946
encoded value exceeds the maximum length (1500 bytes).
29482947
"""
2949-
if isinstance(value, six.binary_type):
2948+
if isinstance(value, bytes):
29502949
try:
29512950
encoded_length = len(value)
29522951
value = value.decode("utf-8")
@@ -2956,7 +2955,7 @@ def _validate(self, value):
29562955
self._name, value
29572956
)
29582957
)
2959-
elif isinstance(value, six.string_types):
2958+
elif isinstance(value, str):
29602959
encoded_length = len(value.encode("utf-8"))
29612960
else:
29622961
raise exceptions.BadValueError("Expected string, got {!r}".format(value))
@@ -2978,7 +2977,7 @@ def _to_base_type(self, value):
29782977
:class:`bytes`, this will return the UTF-8 decoded ``str`` for it.
29792978
Otherwise, it will return :data:`None`.
29802979
"""
2981-
if isinstance(value, six.binary_type):
2980+
if isinstance(value, bytes):
29822981
return value.decode("utf-8")
29832982

29842983
def _from_base_type(self, value):
@@ -3001,7 +3000,7 @@ def _from_base_type(self, value):
30013000
:class:`str` corresponding to it. Otherwise, it will return
30023001
:data:`None`.
30033002
"""
3004-
if isinstance(value, six.binary_type):
3003+
if isinstance(value, bytes):
30053004
try:
30063005
return value.decode("utf-8")
30073006
except UnicodeError:
@@ -3209,7 +3208,7 @@ def _from_base_type(self, value):
32093208
"""
32103209
# We write and retrieve `bytes` normally, but for some reason get back
32113210
# `str` from a projection query.
3212-
if not isinstance(value, six.text_type):
3211+
if not isinstance(value, str):
32133212
value = value.decode("ascii")
32143213
return json.loads(value)
32153214

@@ -3510,14 +3509,14 @@ def _to_base_type(self, value):
35103509
user_entity = ds_entity_module.Entity()
35113510

35123511
# Set required fields.
3513-
user_entity["email"] = six.ensure_text(value.email())
3512+
user_entity["email"] = str(value.email())
35143513
user_entity.exclude_from_indexes.add("email")
3515-
user_entity["auth_domain"] = six.ensure_text(value.auth_domain())
3514+
user_entity["auth_domain"] = str(value.auth_domain())
35163515
user_entity.exclude_from_indexes.add("auth_domain")
35173516
# Set optional field.
35183517
user_id = value.user_id()
35193518
if user_id:
3520-
user_entity["user_id"] = six.ensure_text(user_id)
3519+
user_entity["user_id"] = str(user_id)
35213520
user_entity.exclude_from_indexes.add("user_id")
35223521

35233522
return user_entity
@@ -3612,7 +3611,7 @@ def _handle_positional(wrapped):
36123611
@functools.wraps(wrapped)
36133612
def wrapper(self, *args, **kwargs):
36143613
for arg in args:
3615-
if isinstance(arg, six.string_types):
3614+
if isinstance(arg, str):
36163615
if "name" in kwargs:
36173616
raise TypeError("You can only specify name once")
36183617

@@ -3651,7 +3650,7 @@ def __init__(
36513650
kind = kind._get_kind()
36523651

36533652
else:
3654-
if kind is not None and not isinstance(kind, six.string_types):
3653+
if kind is not None and not isinstance(kind, str):
36553654
raise TypeError("Kind must be a Model class or a string")
36563655

36573656
super(KeyProperty, self).__init__(
@@ -3933,7 +3932,7 @@ def _from_base_type(self, value):
39333932
returns the value without ``tzinfo`` or ``None`` if value did
39343933
not have ``tzinfo`` set.
39353934
"""
3936-
if isinstance(value, six.integer_types):
3935+
if isinstance(value, int):
39373936
# Projection query, value is integer nanoseconds
39383937
seconds = value / 1e6
39393938
value = datetime.datetime.fromtimestamp(seconds, pytz.utc)
@@ -4698,8 +4697,7 @@ def __repr__(cls):
46984697
return "{}<{}>".format(cls.__name__, ", ".join(props))
46994698

47004699

4701-
@six.add_metaclass(MetaModel)
4702-
class Model(_NotEqualMixin):
4700+
class Model(_NotEqualMixin, metaclass=MetaModel):
47034701
"""A class describing Cloud Datastore entities.
47044702
47054703
Model instances are usually called entities. All model classes
@@ -4965,7 +4963,7 @@ def __init__(_self, **kwargs):
49654963

49664964
def _get_property_for(self, p, indexed=True, depth=0):
49674965
"""Internal helper to get the Property for a protobuf-level property."""
4968-
if isinstance(p.name(), six.text_type):
4966+
if isinstance(p.name(), str):
49694967
p.set_name(bytes(p.name(), encoding="utf-8"))
49704968
parts = p.name().decode().split(".")
49714969
if len(parts) <= depth:
@@ -5023,9 +5021,9 @@ def _from_pb(cls, pb, set_key=True, ent=None, key=None):
50235021
# A key passed in overrides a key in the pb.
50245022
if key is None and pb.key().path.element_size():
50255023
# modern NDB expects strings.
5026-
if not isinstance(pb.key_.app_, six.text_type): # pragma: NO BRANCH
5024+
if not isinstance(pb.key_.app_, str): # pragma: NO BRANCH
50275025
pb.key_.app_ = pb.key_.app_.decode()
5028-
if not isinstance(pb.key_.name_space_, six.text_type): # pragma: NO BRANCH
5026+
if not isinstance(pb.key_.name_space_, str): # pragma: NO BRANCH
50295027
pb.key_.name_space_ = pb.key_.name_space_.decode()
50305028

50315029
key = Key(reference=pb.key())
@@ -5331,7 +5329,7 @@ def _fix_up_properties(cls):
53315329
an underscore.
53325330
"""
53335331
kind = cls._get_kind()
5334-
if not isinstance(kind, six.string_types):
5332+
if not isinstance(kind, str):
53355333
raise KindError(
53365334
"Class {} defines a ``_get_kind()`` method that returns "
53375335
"a non-string ({!r})".format(cls.__name__, kind)
@@ -6061,7 +6059,7 @@ def _get_or_insert_async(_cls, _name, *args, **kwargs):
60616059
project = _cls._get_arg(kwargs, "project")
60626060
options = kwargs.pop("_options")
60636061

6064-
if not isinstance(name, six.string_types):
6062+
if not isinstance(name, str):
60656063
raise TypeError("'name' must be a string; received {!r}".format(name))
60666064

60676065
elif not name:
@@ -6666,10 +6664,10 @@ def get_indexes(**options):
66666664
def _unpack_user(v):
66676665
"""Internal helper to unpack a User value from a protocol buffer."""
66686666
uv = v.uservalue()
6669-
email = six.text_type(uv.email().decode("utf-8"))
6670-
auth_domain = six.text_type(uv.auth_domain().decode("utf-8"))
6667+
email = str(uv.email().decode("utf-8"))
6668+
auth_domain = str(uv.auth_domain().decode("utf-8"))
66716669
obfuscated_gaiaid = uv.obfuscated_gaiaid().decode("utf-8")
6672-
obfuscated_gaiaid = six.text_type(obfuscated_gaiaid)
6670+
obfuscated_gaiaid = str(obfuscated_gaiaid)
66736671

66746672
value = User(
66756673
email=email,

google/cloud/ndb/query.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,6 @@ def ranked(cls, rank):
139139

140140
import functools
141141
import logging
142-
import six
143142

144143
from google.cloud.ndb import context as context_module
145144
from google.cloud.ndb import exceptions
@@ -306,7 +305,7 @@ class Parameter(ParameterizedThing):
306305
"""
307306

308307
def __init__(self, key):
309-
if not isinstance(key, six.integer_types + six.string_types):
308+
if not isinstance(key, (int, str)):
310309
raise TypeError(
311310
"Parameter key must be an integer or string, not {}".format(key)
312311
)
@@ -1680,7 +1679,7 @@ def _to_property_orders(self, order_by):
16801679
elif isinstance(order, model.Property):
16811680
# use the sign to turn it into a PropertyOrder
16821681
orders.append(+order)
1683-
elif isinstance(order, six.string_types):
1682+
elif isinstance(order, str):
16841683
name = order
16851684
reverse = False
16861685
if order.startswith("-"):
@@ -2349,7 +2348,7 @@ def _to_property_names(properties):
23492348

23502349
fixed = []
23512350
for prop in properties:
2352-
if isinstance(prop, six.string_types):
2351+
if isinstance(prop, str):
23532352
fixed.append(prop)
23542353
elif isinstance(prop, model.Property):
23552354
fixed.append(prop._name)

setup.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ def main():
4646
"pymemcache >= 2.1.0, < 5.0.0dev",
4747
"pytz >= 2018.3",
4848
"redis >= 3.0.0, < 6.0.0dev",
49-
# TODO(https://github.com/googleapis/python-ndb/issues/913) remove this dependency once six is no longer used in the codebase
50-
"six >= 1.12.0, < 2.0.0dev"
5149
]
5250

5351
setuptools.setup(

0 commit comments

Comments
 (0)