Skip to content

Commit a3fb246

Browse files
committed
PYTHON-801 - Remove BaseObject.uuid_subtype
1 parent 7e4ebde commit a3fb246

File tree

9 files changed

+64
-74
lines changed

9 files changed

+64
-74
lines changed

bson/binary.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,16 +174,15 @@ def __repr__(self):
174174

175175

176176
class UUIDLegacy(Binary):
177-
"""UUID wrapper to support working with UUIDs stored as legacy
178-
BSON binary subtype 3.
177+
"""UUID wrapper to support working with UUIDs stored as PYTHON_LEGACY.
179178
180179
.. doctest::
181180
182181
>>> import uuid
183-
>>> from bson.binary import Binary, UUIDLegacy, UUID_SUBTYPE
182+
>>> from bson.binary import Binary, UUIDLegacy, STANDARD
184183
>>> my_uuid = uuid.uuid4()
185-
>>> coll = db.test
186-
>>> coll.uuid_subtype = UUID_SUBTYPE
184+
>>> coll = db.get_collection('test',
185+
... CodecOptions(uuid_representation=STANDARD))
187186
>>> coll.insert({'uuid': Binary(my_uuid.bytes, 3)})
188187
ObjectId('...')
189188
>>> coll.find({'uuid': my_uuid}).count()

doc/api/pymongo/collection.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
.. autoattribute:: codec_options
2828
.. autoattribute:: read_preference
2929
.. autoattribute:: write_concern
30-
.. autoattribute:: uuid_subtype
3130
.. automethod:: with_options
3231
.. automethod:: insert(doc_or_docs[, manipulate=True[, check_keys=True[, continue_on_error=False[, **kwargs]]]])
3332
.. automethod:: save(to_save[, manipulate=True[, check_keys=True[, **kwargs]]])

doc/api/pymongo/database.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
.. autoattribute:: codec_options
2727
.. autoattribute:: read_preference
2828
.. autoattribute:: write_concern
29-
.. autoattribute:: uuid_subtype
3029

3130

3231
.. autoclass:: pymongo.database.SystemJS

doc/api/pymongo/mongo_client.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
.. autoattribute:: read_preference
3333
.. autoattribute:: secondary_acceptable_latency_ms
3434
.. autoattribute:: write_concern
35-
.. autoattribute:: uuid_subtype
3635
.. autoattribute:: is_locked
3736
.. automethod:: database_names
3837
.. automethod:: drop_database

doc/api/pymongo/mongo_replica_set_client.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
.. autoattribute:: read_preference
3131
.. autoattribute:: secondary_acceptable_latency_ms
3232
.. autoattribute:: write_concern
33-
.. autoattribute:: uuid_subtype
3433
.. automethod:: database_names
3534
.. automethod:: drop_database
3635
.. automethod:: get_default_database

pymongo/collection.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ def __init__(self, database, name, create=False, codec_options=None,
9090
9191
.. versionchanged:: 3.0
9292
Added the codec_options, read_preference, and write_concern options.
93+
Removed the uuid_subtype attribute.
9394
:class:`~pymongo.collection.Collection` no longer returns an
9495
instance of :class:`~pymongo.collection.Collection` for attribute
9596
names with leading underscores. You must use dict-style lookups

pymongo/common.py

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@
2727
from pymongo.ssl_support import validate_cert_reqs
2828
from pymongo.write_concern import WriteConcern
2929
from bson.binary import (STANDARD, PYTHON_LEGACY,
30-
JAVA_LEGACY, CSHARP_LEGACY,
31-
ALL_UUID_REPRESENTATIONS)
30+
JAVA_LEGACY, CSHARP_LEGACY)
3231
from bson.py3compat import string_type, integer_types
3332

3433
# Defaults until we connect to a server and get updated limits.
@@ -255,14 +254,6 @@ def validate_uuid_representation(dummy, value):
255254
"%s" % (value, tuple(_UUID_REPRESENTATIONS)))
256255

257256

258-
def validate_uuid_subtype(dummy, value):
259-
"""Validate the uuid subtype option, a numerical value whose acceptable
260-
values are defined in bson.binary."""
261-
if value not in ALL_UUID_REPRESENTATIONS:
262-
raise ConfigurationError("Not a valid setting for uuid_subtype.")
263-
return value
264-
265-
266257
def validate_read_preference_tags(name, value):
267258
"""Parse readPreferenceTags if passed as a client kwarg.
268259
"""
@@ -472,24 +463,6 @@ def __set_tags(self, tag_sets):
472463

473464
tag_sets = property(__get_tags, __set_tags)
474465

475-
def __get_uuid_subtype(self):
476-
"""This attribute specifies which BSON Binary subtype is used when
477-
storing UUIDs. Historically UUIDs have been stored as BSON Binary
478-
subtype 3. This attribute is used to switch to the newer BSON Binary
479-
subtype 4. It can also be used to force legacy byte order and subtype
480-
compatibility with the Java and C# drivers. See the :mod:`bson.binary`
481-
module for all options."""
482-
return self.__codec_options.uuid_representation
483-
484-
def __set_uuid_subtype(self, value):
485-
"""Sets the BSON Binary subtype to be used when storing UUIDs."""
486-
uuid_representation = validate_uuid_subtype("uuid_subtype", value)
487-
self.__codec_options = CodecOptions(self.__codec_options.as_class,
488-
self.__codec_options.tz_aware,
489-
uuid_representation)
490-
491-
uuid_subtype = property(__get_uuid_subtype, __set_uuid_subtype)
492-
493466
def _get_wc_override(self):
494467
"""Get write concern override.
495468

test/test_binary.py

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from bson.py3compat import u
2929
from bson.son import SON
3030
from test import client_context, unittest
31+
from pymongo.codec_options import CodecOptions
3132
from pymongo.mongo_client import MongoClient
3233

3334

@@ -131,11 +132,11 @@ def test_repr(self):
131132
def test_legacy_java_uuid(self):
132133
# Test decoding
133134
data = self.java_data
134-
docs = bson.decode_all(data, SON, False, OLD_UUID_SUBTYPE)
135+
docs = bson.decode_all(data, SON, False, PYTHON_LEGACY)
135136
for d in docs:
136137
self.assertNotEqual(d['newguid'], uuid.UUID(d['newguidstring']))
137138

138-
docs = bson.decode_all(data, SON, False, UUID_SUBTYPE)
139+
docs = bson.decode_all(data, SON, False, STANDARD)
139140
for d in docs:
140141
self.assertNotEqual(d['newguid'], uuid.UUID(d['newguidstring']))
141142

@@ -149,11 +150,11 @@ def test_legacy_java_uuid(self):
149150

150151
# Test encoding
151152
encoded = b''.join([bson.BSON.encode(doc,
152-
uuid_subtype=OLD_UUID_SUBTYPE)
153+
uuid_subtype=PYTHON_LEGACY)
153154
for doc in docs])
154155
self.assertNotEqual(data, encoded)
155156

156-
encoded = b''.join([bson.BSON.encode(doc, uuid_subtype=UUID_SUBTYPE)
157+
encoded = b''.join([bson.BSON.encode(doc, uuid_subtype=STANDARD)
157158
for doc in docs])
158159
self.assertNotEqual(data, encoded)
159160

@@ -171,15 +172,17 @@ def test_legacy_java_uuid_roundtrip(self):
171172
docs = bson.decode_all(data, SON, False, JAVA_LEGACY)
172173

173174
client_context.client.pymongo_test.drop_collection('java_uuid')
174-
coll = client_context.client.pymongo_test.java_uuid
175-
coll.uuid_subtype = JAVA_LEGACY
175+
db = client_context.client.pymongo_test
176+
coll = db.get_collection(
177+
'java_uuid', CodecOptions(uuid_representation=JAVA_LEGACY))
176178

177179
coll.insert(docs)
178180
self.assertEqual(5, coll.count())
179181
for d in coll.find():
180182
self.assertEqual(d['newguid'], uuid.UUID(d['newguidstring']))
181183

182-
coll.uuid_subtype = OLD_UUID_SUBTYPE
184+
coll = db.get_collection(
185+
'java_uuid', CodecOptions(uuid_representation=PYTHON_LEGACY))
183186
for d in coll.find():
184187
self.assertNotEqual(d['newguid'], d['newguidstring'])
185188
client_context.client.pymongo_test.drop_collection('java_uuid')
@@ -188,11 +191,11 @@ def test_legacy_csharp_uuid(self):
188191
data = self.csharp_data
189192

190193
# Test decoding
191-
docs = bson.decode_all(data, SON, False, OLD_UUID_SUBTYPE)
194+
docs = bson.decode_all(data, SON, False, PYTHON_LEGACY)
192195
for d in docs:
193196
self.assertNotEqual(d['newguid'], uuid.UUID(d['newguidstring']))
194197

195-
docs = bson.decode_all(data, SON, False, UUID_SUBTYPE)
198+
docs = bson.decode_all(data, SON, False, STANDARD)
196199
for d in docs:
197200
self.assertNotEqual(d['newguid'], uuid.UUID(d['newguidstring']))
198201

@@ -206,11 +209,11 @@ def test_legacy_csharp_uuid(self):
206209

207210
# Test encoding
208211
encoded = b''.join([bson.BSON.encode(doc,
209-
uuid_subtype=OLD_UUID_SUBTYPE)
212+
uuid_subtype=PYTHON_LEGACY)
210213
for doc in docs])
211214
self.assertNotEqual(data, encoded)
212215

213-
encoded = b''.join([bson.BSON.encode(doc, uuid_subtype=UUID_SUBTYPE)
216+
encoded = b''.join([bson.BSON.encode(doc, uuid_subtype=STANDARD)
214217
for doc in docs])
215218
self.assertNotEqual(data, encoded)
216219

@@ -228,15 +231,17 @@ def test_legacy_csharp_uuid_roundtrip(self):
228231
docs = bson.decode_all(data, SON, False, CSHARP_LEGACY)
229232

230233
client_context.client.pymongo_test.drop_collection('csharp_uuid')
231-
coll = client_context.client.pymongo_test.csharp_uuid
232-
coll.uuid_subtype = CSHARP_LEGACY
234+
db = client_context.client.pymongo_test
235+
coll = db.get_collection(
236+
'csharp_uuid', CodecOptions(uuid_representation=CSHARP_LEGACY))
233237

234238
coll.insert(docs)
235239
self.assertEqual(5, coll.count())
236240
for d in coll.find():
237241
self.assertEqual(d['newguid'], uuid.UUID(d['newguidstring']))
238242

239-
coll.uuid_subtype = OLD_UUID_SUBTYPE
243+
coll = db.get_collection(
244+
'csharp_uuid', CodecOptions(uuid_representation=PYTHON_LEGACY))
240245
for d in coll.find():
241246
self.assertNotEqual(d['newguid'], d['newguidstring'])
242247
client_context.client.pymongo_test.drop_collection('csharp_uuid')
@@ -245,20 +250,24 @@ def test_uri_to_uuid(self):
245250

246251
uri = "mongodb://foo/?uuidrepresentation=csharpLegacy"
247252
client = MongoClient(uri, connect=False)
248-
self.assertEqual(client.pymongo_test.test.uuid_subtype, CSHARP_LEGACY)
253+
self.assertEqual(
254+
client.pymongo_test.test.codec_options.uuid_representation,
255+
CSHARP_LEGACY)
249256

250257
@client_context.require_connection
251258
def test_uuid_queries(self):
252259

253-
coll = client_context.client.pymongo_test.test
260+
db = client_context.client.pymongo_test
261+
coll = db.test
254262
coll.drop()
255263

256264
uu = uuid.uuid4()
257265
coll.insert({'uuid': Binary(uu.bytes, 3)})
258266
self.assertEqual(1, coll.count())
259267

260268
# Test UUIDLegacy queries.
261-
coll.uuid_subtype = 4
269+
coll = db.get_collection("test",
270+
CodecOptions(uuid_representation=STANDARD))
262271
self.assertEqual(0, coll.find({'uuid': uu}).count())
263272
cur = coll.find({'uuid': UUIDLegacy(uu)})
264273
self.assertEqual(1, cur.count())

test/test_common.py

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@
1919

2020
sys.path[0:0] = [""]
2121

22-
from bson.binary import UUIDLegacy, OLD_UUID_SUBTYPE, UUID_SUBTYPE
22+
from bson.binary import UUIDLegacy, PYTHON_LEGACY, STANDARD
2323
from bson.code import Code
2424
from bson.objectid import ObjectId
2525
from bson.son import SON
26+
from pymongo.codec_options import CodecOptions
2627
from pymongo.mongo_client import MongoClient
2728
from pymongo.errors import ConfigurationError, OperationFailure
2829
from pymongo.write_concern import WriteConcern
@@ -37,38 +38,41 @@ def setUpModule():
3738

3839
class TestCommon(IntegrationTest):
3940

40-
def test_uuid_subtype(self):
41+
def test_uuid_representation(self):
4142
coll = self.db.uuid
4243
coll.drop()
4344

44-
def change_subtype(collection, subtype):
45-
collection.uuid_subtype = subtype
45+
self.assertRaises(ValueError, CodecOptions, uuid_representation=7)
46+
self.assertRaises(ValueError, CodecOptions, uuid_representation=2)
4647

4748
# Test property
48-
self.assertEqual(OLD_UUID_SUBTYPE, coll.uuid_subtype)
49-
self.assertRaises(ConfigurationError, change_subtype, coll, 7)
50-
self.assertRaises(ConfigurationError, change_subtype, coll, 2)
49+
self.assertEqual(PYTHON_LEGACY,
50+
coll.codec_options.uuid_representation)
5151

5252
# Test basic query
5353
uu = uuid.uuid4()
5454
# Insert as binary subtype 3
5555
coll.insert({'uu': uu})
5656
self.assertEqual(uu, coll.find_one({'uu': uu})['uu'])
57-
coll.uuid_subtype = UUID_SUBTYPE
58-
self.assertEqual(UUID_SUBTYPE, coll.uuid_subtype)
57+
coll = self.db.get_collection(
58+
"uuid", CodecOptions(uuid_representation=STANDARD))
59+
self.assertEqual(STANDARD, coll.codec_options.uuid_representation)
5960
self.assertEqual(None, coll.find_one({'uu': uu}))
6061
self.assertEqual(uu, coll.find_one({'uu': UUIDLegacy(uu)})['uu'])
6162

6263
# Test Cursor.count
6364
self.assertEqual(0, coll.find({'uu': uu}).count())
64-
coll.uuid_subtype = OLD_UUID_SUBTYPE
65+
coll = self.db.get_collection(
66+
"uuid", CodecOptions(uuid_representation=PYTHON_LEGACY))
6567
self.assertEqual(1, coll.find({'uu': uu}).count())
6668

6769
# Test remove
68-
coll.uuid_subtype = UUID_SUBTYPE
70+
coll = self.db.get_collection(
71+
"uuid", CodecOptions(uuid_representation=STANDARD))
6972
coll.remove({'uu': uu})
7073
self.assertEqual(1, coll.count())
71-
coll.uuid_subtype = OLD_UUID_SUBTYPE
74+
coll = self.db.get_collection(
75+
"uuid", CodecOptions(uuid_representation=PYTHON_LEGACY))
7276
coll.remove({'uu': uu})
7377
self.assertEqual(0, coll.count())
7478

@@ -83,22 +87,26 @@ def change_subtype(collection, subtype):
8387
self.assertEqual(1, coll.find_one({'_id': uu})['i'])
8488

8589
# Test update
86-
coll.uuid_subtype = UUID_SUBTYPE
90+
coll = self.db.get_collection(
91+
"uuid", CodecOptions(uuid_representation=STANDARD))
8792
coll.update({'_id': uu}, {'$set': {'i': 2}})
88-
coll.uuid_subtype = OLD_UUID_SUBTYPE
93+
coll = self.db.get_collection(
94+
"uuid", CodecOptions(uuid_representation=PYTHON_LEGACY))
8995
self.assertEqual(1, coll.find_one({'_id': uu})['i'])
9096
coll.update({'_id': uu}, {'$set': {'i': 2}})
9197
self.assertEqual(2, coll.find_one({'_id': uu})['i'])
9298

9399
# Test Cursor.distinct
94100
self.assertEqual([2], coll.find({'_id': uu}).distinct('i'))
95-
coll.uuid_subtype = UUID_SUBTYPE
101+
coll = self.db.get_collection(
102+
"uuid", CodecOptions(uuid_representation=STANDARD))
96103
self.assertEqual([], coll.find({'_id': uu}).distinct('i'))
97104

98105
# Test find_and_modify
99106
self.assertEqual(None, coll.find_and_modify({'_id': uu},
100107
{'$set': {'i': 5}}))
101-
coll.uuid_subtype = OLD_UUID_SUBTYPE
108+
coll = self.db.get_collection(
109+
"uuid", CodecOptions(uuid_representation=PYTHON_LEGACY))
102110
self.assertEqual(2, coll.find_and_modify({'_id': uu},
103111
{'$set': {'i': 5}})['i'])
104112
self.assertEqual(5, coll.find_one({'_id': uu})['i'])
@@ -132,7 +140,8 @@ def change_subtype(collection, subtype):
132140
" return total;"
133141
"}")
134142

135-
coll.uuid_subtype = UUID_SUBTYPE
143+
coll = self.db.get_collection(
144+
"uuid", CodecOptions(uuid_representation=STANDARD))
136145
q = {"_id": uu}
137146
if client_context.version.at_least(1, 7, 4):
138147
result = coll.inline_map_reduce(map, reduce, query=q)
@@ -141,7 +150,8 @@ def change_subtype(collection, subtype):
141150
result = coll.map_reduce(map, reduce, "results", query=q)
142151
self.assertEqual(0, self.db.results.count())
143152

144-
coll.uuid_subtype = OLD_UUID_SUBTYPE
153+
coll = self.db.get_collection(
154+
"uuid", CodecOptions(uuid_representation=PYTHON_LEGACY))
145155
q = {"_id": uu}
146156
if client_context.version.at_least(1, 7, 4):
147157
result = coll.inline_map_reduce(map, reduce, query=q)
@@ -158,11 +168,13 @@ def change_subtype(collection, subtype):
158168
coll.insert({"_id": uuid.uuid4(), "a": 1})
159169

160170
reduce = "function (obj, prev) { prev.count++; }"
161-
coll.uuid_subtype = UUID_SUBTYPE
171+
coll = self.db.get_collection(
172+
"uuid", CodecOptions(uuid_representation=STANDARD))
162173
self.assertEqual([],
163174
coll.group([], {"_id": uu},
164175
{"count": 0}, reduce))
165-
coll.uuid_subtype = OLD_UUID_SUBTYPE
176+
coll = self.db.get_collection(
177+
"uuid", CodecOptions(uuid_representation=PYTHON_LEGACY))
166178
self.assertEqual([{"count": 1}],
167179
coll.group([], {"_id": uu},
168180
{"count": 0}, reduce))

0 commit comments

Comments
 (0)