Skip to content

Commit 1e75831

Browse files
author
Chris Rossi
authored
fix: disuse __slots__ in most places (#330)
In Python 2.7, classes which use `__slots__` can't be pickled. Users have reported problems trying to pickle NDB entities, indicating there is some perceived use case for pickling entities. Fixes #311
1 parent 810ded1 commit 1e75831

File tree

13 files changed

+3
-143
lines changed

13 files changed

+3
-143
lines changed

packages/google-cloud-ndb/google/cloud/ndb/_datastore_types.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ class BlobKey(object):
5151
:class:`bytes` instance.
5252
"""
5353

54-
__slots__ = ("_blob_key",)
55-
5654
def __init__(self, blob_key):
5755
if isinstance(blob_key, bytes):
5856
if len(blob_key) > _MAX_STRING_LENGTH:

packages/google-cloud-ndb/google/cloud/ndb/_eventloop.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -135,15 +135,6 @@ class EventLoop(object):
135135
get added to this queue and then processed by the event loop.
136136
"""
137137

138-
__slots__ = (
139-
"current",
140-
"idlers",
141-
"inactive",
142-
"queue",
143-
"rpcs",
144-
"rpc_results",
145-
)
146-
147138
def __init__(self):
148139
self.current = collections.deque()
149140
self.idlers = collections.deque()

packages/google-cloud-ndb/google/cloud/ndb/blobstore.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,6 @@ def __init__(self, *args, **kwargs):
7878

7979

8080
class BlobInfo(object):
81-
__slots__ = ()
82-
8381
def __init__(self, *args, **kwargs):
8482
raise exceptions.NoLongerImplementedError()
8583

@@ -111,8 +109,6 @@ def __init__(self, *args, **kwargs):
111109

112110

113111
class BlobReader(object):
114-
__slots__ = ()
115-
116112
def __init__(self, *args, **kwargs):
117113
raise exceptions.NoLongerImplementedError()
118114

packages/google-cloud-ndb/google/cloud/ndb/context.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -546,21 +546,15 @@ def urlfetch(self, *args, **kwargs):
546546

547547

548548
class ContextOptions(object):
549-
__slots__ = ()
550-
551549
def __init__(self, *args, **kwargs):
552550
raise exceptions.NoLongerImplementedError()
553551

554552

555553
class TransactionOptions(object):
556-
__slots__ = ()
557-
558554
def __init__(self, *args, **kwargs):
559555
raise exceptions.NoLongerImplementedError()
560556

561557

562558
class AutoBatcher(object):
563-
__slots__ = ()
564-
565559
def __init__(self, *args, **kwargs):
566560
raise exceptions.NoLongerImplementedError()

packages/google-cloud-ndb/google/cloud/ndb/django_middleware.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,5 @@
1919

2020

2121
class NdbDjangoMiddleware(object):
22-
__slots__ = ()
23-
2422
def __init__(self, *args, **kwargs):
2523
raise NotImplementedError

packages/google-cloud-ndb/google/cloud/ndb/key.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,6 @@ class Key(object):
272272
arguments were given with the path.
273273
"""
274274

275-
__slots__ = ("_key", "_reference")
276-
277275
def __new__(cls, *path_args, **kwargs):
278276
# Avoid circular import in Python 2.7
279277
from google.cloud.ndb import context as context_module

packages/google-cloud-ndb/google/cloud/ndb/metadata.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@
5959
class _BaseMetadata(model.Model):
6060
"""Base class for all metadata models."""
6161

62-
__slots__ = ()
63-
6462
_use_cache = False
6563
_use_global_cache = False
6664

@@ -81,8 +79,6 @@ def _get_kind(cls):
8179
class Namespace(_BaseMetadata):
8280
"""Model for __namespace__ metadata query results."""
8381

84-
__slots__ = ()
85-
8682
KIND_NAME = "__namespace__"
8783
EMPTY_NAMESPACE_ID = 1
8884

@@ -127,8 +123,6 @@ def key_to_namespace(cls, key):
127123
class Kind(_BaseMetadata):
128124
"""Model for __kind__ metadata query results."""
129125

130-
__slots__ = ()
131-
132126
KIND_NAME = "__kind__"
133127

134128
@property
@@ -168,8 +162,6 @@ def key_to_kind(cls, key):
168162
class Property(_BaseMetadata):
169163
"""Model for __property__ metadata query results."""
170164

171-
__slots__ = ()
172-
173165
KIND_NAME = "__property__"
174166

175167
@property

packages/google-cloud-ndb/google/cloud/ndb/model.py

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -388,8 +388,6 @@ def __ne__(self, other):
388388
class IndexProperty(_NotEqualMixin):
389389
"""Immutable object representing a single property in an index."""
390390

391-
__slots__ = ("_name", "_direction")
392-
393391
@utils.positional(1)
394392
def __new__(cls, name, direction):
395393
instance = super(IndexProperty, cls).__new__(cls)
@@ -426,8 +424,6 @@ def __hash__(self):
426424
class Index(_NotEqualMixin):
427425
"""Immutable object representing an index."""
428426

429-
__slots__ = ("_kind", "_properties", "_ancestor")
430-
431427
@utils.positional(1)
432428
def __new__(cls, kind, properties, ancestor):
433429
instance = super(Index, cls).__new__(cls)
@@ -475,8 +471,6 @@ def __hash__(self):
475471
class IndexState(_NotEqualMixin):
476472
"""Immutable object representing an index and its state."""
477473

478-
__slots__ = ("_definition", "_state", "_id")
479-
480474
@utils.positional(1)
481475
def __new__(cls, definition, state, id):
482476
instance = super(IndexState, cls).__new__(cls)
@@ -526,8 +520,6 @@ def __hash__(self):
526520

527521

528522
class ModelAdapter(object):
529-
__slots__ = ()
530-
531523
def __new__(self, *args, **kwargs):
532524
raise exceptions.NoLongerImplementedError()
533525

@@ -796,8 +788,6 @@ def make_connection(*args, **kwargs):
796788
class ModelAttribute(object):
797789
"""Base for classes that implement a ``_fix_up()`` method."""
798790

799-
__slots__ = ()
800-
801791
def _fix_up(self, cls, code_name):
802792
"""Fix-up property name. To be implemented by subclasses.
803793
@@ -824,8 +814,6 @@ class _BaseValue(_NotEqualMixin):
824814
TypeError: If ``b_val`` is a list.
825815
"""
826816

827-
__slots__ = ("b_val",)
828-
829817
def __init__(self, b_val):
830818
if b_val is None:
831819
raise TypeError("Cannot wrap None")
@@ -2169,8 +2157,6 @@ class ModelKey(Property):
21692157
.. automethod:: _validate
21702158
"""
21712159

2172-
__slots__ = ()
2173-
21742160
def __init__(self):
21752161
super(ModelKey, self).__init__()
21762162
self._name = "__key__"
@@ -2253,8 +2239,6 @@ class BooleanProperty(Property):
22532239
.. automethod:: _validate
22542240
"""
22552241

2256-
__slots__ = ()
2257-
22582242
def _validate(self, value):
22592243
"""Validate a ``value`` before setting it.
22602244
@@ -2285,8 +2269,6 @@ class IntegerProperty(Property):
22852269
.. automethod:: _validate
22862270
"""
22872271

2288-
__slots__ = ()
2289-
22902272
def _validate(self, value):
22912273
"""Validate a ``value`` before setting it.
22922274
@@ -2318,8 +2300,6 @@ class FloatProperty(Property):
23182300
.. automethod:: _validate
23192301
"""
23202302

2321-
__slots__ = ()
2322-
23232303
def _validate(self, value):
23242304
"""Validate a ``value`` before setting it.
23252305
@@ -2602,8 +2582,6 @@ class Item(ndb.Model):
26022582
NotImplementedError: If ``indexed=True`` is provided.
26032583
"""
26042584

2605-
__slots__ = ()
2606-
26072585
def __init__(self, *args, **kwargs):
26082586
indexed = kwargs.pop("indexed", False)
26092587
if indexed:
@@ -2732,8 +2710,6 @@ class StringProperty(TextProperty):
27322710
NotImplementedError: If ``indexed=False`` is provided.
27332711
"""
27342712

2735-
__slots__ = ()
2736-
27372713
def __init__(self, *args, **kwargs):
27382714
indexed = kwargs.pop("indexed", True)
27392715
if not indexed:
@@ -2757,8 +2733,6 @@ class GeoPtProperty(Property):
27572733
.. automethod:: _validate
27582734
"""
27592735

2760-
__slots__ = ()
2761-
27622736
def _validate(self, value):
27632737
"""Validate a ``value`` before setting it.
27642738
@@ -2967,8 +2941,6 @@ class User(object):
29672941
UserNotFoundError: If ``email`` is empty.
29682942
"""
29692943

2970-
__slots__ = ("_auth_domain", "_email", "_user_id")
2971-
29722944
def __init__(self, email=None, _auth_domain=None, _user_id=None):
29732945
if _auth_domain is None:
29742946
raise ValueError("_auth_domain is required")
@@ -3465,8 +3437,6 @@ class BlobKeyProperty(Property):
34653437
.. automethod:: _validate
34663438
"""
34673439

3468-
__slots__ = ()
3469-
34703440
def _validate(self, value):
34713441
"""Validate a ``value`` before setting it.
34723442
@@ -3685,8 +3655,6 @@ class DateProperty(DateTimeProperty):
36853655
.. automethod:: _validate
36863656
"""
36873657

3688-
__slots__ = ()
3689-
36903658
def _validate(self, value):
36913659
"""Validate a ``value`` before setting it.
36923660
@@ -3747,8 +3715,6 @@ class TimeProperty(DateTimeProperty):
37473715
.. automethod:: _validate
37483716
"""
37493717

3750-
__slots__ = ()
3751-
37523718
def _validate(self, value):
37533719
"""Validate a ``value`` before setting it.
37543720

packages/google-cloud-ndb/google/cloud/ndb/msgprop.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,10 @@
1919

2020

2121
class EnumProperty(object):
22-
__slots__ = ()
23-
2422
def __init__(self, *args, **kwargs):
2523
raise NotImplementedError
2624

2725

2826
class MessageProperty(object):
29-
__slots__ = ()
30-
3127
def __init__(self, *args, **kwargs):
3228
raise NotImplementedError

packages/google-cloud-ndb/google/cloud/ndb/query.py

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,6 @@ class PropertyOrder(object):
183183
or not (ascending). Default is False.
184184
"""
185185

186-
__slots__ = ["name", "reverse"]
187-
188186
def __init__(self, name, reverse=False):
189187
self.name = name
190188
self.reverse = reverse
@@ -227,8 +225,6 @@ class RepeatedStructuredPropertyPredicate(object):
227225
contain a value for each key in ``match_keys``.
228226
"""
229227

230-
__slots__ = ["name", "match_keys", "match_values"]
231-
232228
def __init__(self, name, match_keys, entity_pb):
233229
self.name = name
234230
self.match_keys = match_keys
@@ -305,8 +301,6 @@ class Parameter(ParameterizedThing):
305301
TypeError: If the ``key`` is not a string or integer.
306302
"""
307303

308-
__slots__ = ("_key",)
309-
310304
def __init__(self, key):
311305
if not isinstance(key, six.integer_types + six.string_types):
312306
raise TypeError(
@@ -411,8 +405,6 @@ class Node(object):
411405

412406
_multiquery = False
413407

414-
__slots__ = ()
415-
416408
def __new__(cls):
417409
if cls is Node:
418410
raise TypeError("Cannot instantiate Node, only a subclass.")
@@ -479,8 +471,6 @@ def resolve(self, bindings, used):
479471
class FalseNode(Node):
480472
"""Tree node for an always-failing filter."""
481473

482-
__slots__ = ()
483-
484474
def __eq__(self, other):
485475
"""Equality check.
486476
@@ -524,8 +514,6 @@ class ParameterNode(Node):
524514
:class:`.ParameterizedFunction`.
525515
"""
526516

527-
__slots__ = ("_prop", "_op", "_param")
528-
529517
def __new__(cls, prop, op, param):
530518
# Avoid circular import in Python 2.7
531519
from google.cloud.ndb import model
@@ -643,7 +631,9 @@ class FilterNode(Node):
643631
:class:`frozenset`)
644632
"""
645633

646-
__slots__ = ("_name", "_opsymbol", "_value")
634+
_name = None
635+
_opsymbol = None
636+
_value = None
647637

648638
def __new__(cls, name, opsymbol, value):
649639
# Avoid circular import in Python 2.7
@@ -754,8 +744,6 @@ class PostFilterNode(Node):
754744
the given filter.
755745
"""
756746

757-
__slots__ = ("predicate",)
758-
759747
def __new__(cls, predicate):
760748
instance = super(PostFilterNode, cls).__new__(cls)
761749
instance.predicate = predicate
@@ -826,8 +814,6 @@ class _BooleanClauses(object):
826814
with the current boolean expression via ``AND`` or ``OR``.
827815
"""
828816

829-
__slots__ = ("name", "combine_or", "or_parts")
830-
831817
def __init__(self, name, combine_or):
832818
self.name = name
833819
self.combine_or = combine_or
@@ -919,8 +905,6 @@ class ConjunctionNode(Node):
919905
expression.
920906
"""
921907

922-
__slots__ = ("_nodes",)
923-
924908
def __new__(cls, *nodes):
925909
if not nodes:
926910
raise TypeError("ConjunctionNode() requires at least one node.")
@@ -1075,7 +1059,6 @@ class DisjunctionNode(Node):
10751059
"""
10761060

10771061
_multiquery = True
1078-
__slots__ = ("_nodes",)
10791062

10801063
def __new__(cls, *nodes):
10811064
if not nodes:

0 commit comments

Comments
 (0)