Skip to content

Commit d80de9c

Browse files
author
Mike Dirolf
committed
minor: remove all uses of types module
1 parent 2aa45d1 commit d80de9c

16 files changed

+66
-88
lines changed

gridfs/__init__.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
exposing a file-like interface.
1919
"""
2020

21-
import types
22-
2321
from grid_file import GridFile
2422
from pymongo.database import Database
2523

@@ -72,12 +70,12 @@ def remove(self, filename_or_spec, collection="fs"):
7270
- `collection` (optional): root collection where this file is located
7371
"""
7472
spec = filename_or_spec
75-
if isinstance(filename_or_spec, types.StringTypes):
73+
if isinstance(filename_or_spec, basestring):
7674
spec = {"filename": filename_or_spec}
7775
if not isinstance(spec, dict):
7876
raise TypeError("filename_or_spec must be an "
7977
"instance of (str, dict, SON)")
80-
if not isinstance(collection, types.StringTypes):
78+
if not isinstance(collection, basestring):
8179
raise TypeError("collection must be an instance of (str, unicode)")
8280

8381
# convert to _id's so we can uniquely create GridFile instances
@@ -100,7 +98,7 @@ def list(self, collection="fs"):
10098
:Parameters:
10199
- `collection` (optional): root collection to list files from
102100
"""
103-
if not isinstance(collection, types.StringTypes):
101+
if not isinstance(collection, basestring):
104102
raise TypeError("collection must be an instance of (str, unicode)")
105103
names = []
106104
for grid_file in self.__database[collection].files.find():

gridfs/grid_file.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
"""File-like object used for reading from and writing to GridFS"""
1616

17-
import types
1817
import datetime
1918
import math
2019
import os
@@ -95,13 +94,13 @@ class directly - instead see the `gridfs.GridFS.open` method.
9594
- `collection` (optional): the collection in which to store/retrieve
9695
this file
9796
"""
98-
if not isinstance(file_spec, types.DictType):
97+
if not isinstance(file_spec, dict):
9998
raise TypeError("file_spec must be an instance of (dict, SON)")
10099
if not isinstance(database, Database):
101100
raise TypeError("database must be an instance of database")
102-
if not isinstance(collection, types.StringTypes):
101+
if not isinstance(collection, basestring):
103102
raise TypeError("collection must be an instance of (str, unicode)")
104-
if not isinstance(mode, types.StringTypes):
103+
if not isinstance(mode, basestring):
105104
raise TypeError("mode must be an instance of (str, unicode)")
106105
if mode not in ("r", "w"):
107106
raise ValueError("mode must be one of ('r', 'w')")
@@ -311,7 +310,7 @@ def write(self, str):
311310
"""
312311
self.__assert_open("w")
313312

314-
if not isinstance(str, types.StringType):
313+
if not isinstance(str, basestring):
315314
raise TypeError("can only write strings")
316315

317316
while str:

pymongo/binary.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@
1515
"""Tools for representing binary data to be stored in MongoDB.
1616
"""
1717

18-
import types
19-
20-
2118
class Binary(str):
2219
"""Representation of binary data to be stored in or retrieved from MongoDB.
2320
@@ -37,9 +34,9 @@ class Binary(str):
3734
"""
3835

3936
def __new__(cls, data, subtype=2):
40-
if not isinstance(data, types.StringType):
37+
if not isinstance(data, str):
4138
raise TypeError("data must be an instance of str")
42-
if not isinstance(subtype, types.IntType):
39+
if not isinstance(subtype, int):
4340
raise TypeError("subtype must be an instance of int")
4441
if subtype >= 256 or subtype < 0:
4542
raise ValueError("subtype must be contained in [0, 256)")

pymongo/bson.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
1717
Generally not needed to be used by application developers."""
1818

19-
import types
2019
import struct
2120
import re
2221
import datetime
@@ -524,7 +523,7 @@ def is_valid(bson):
524523
:Parameters:
525524
- `bson`: the data to be validated
526525
"""
527-
if not isinstance(bson, types.StringType):
526+
if not isinstance(bson, str):
528527
raise TypeError("BSON data must be an instance of a subclass of str")
529528

530529
# 4 MB limit

pymongo/collection.py

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
"""Collection level utilities for Mongo."""
1616

17-
import types
1817
import warnings
1918
import struct
2019

@@ -48,10 +47,10 @@ def __init__(self, database, name, options=None):
4847
- `options`: dictionary of collection options.
4948
see `pymongo.database.Database.create_collection` for details.
5049
"""
51-
if not isinstance(name, types.StringTypes):
50+
if not isinstance(name, basestring):
5251
raise TypeError("name must be an instance of (str, unicode)")
5352

54-
if not isinstance(options, (types.DictType, types.NoneType)):
53+
if options is not None and not isinstance(options, dict):
5554
raise TypeError("options must be an instance of dict")
5655

5756
if not name or ".." in name:
@@ -168,7 +167,7 @@ def save(self, to_save, manipulate=True, safe=False):
168167
- `manipulate` (optional): manipulate the SON object before saving it
169168
- `safe` (optional): check that the save succeeded?
170169
"""
171-
if not isinstance(to_save, types.DictType):
170+
if not isinstance(to_save, dict):
172171
raise TypeError("cannot save object of type %s" % type(to_save))
173172

174173
if "_id" not in to_save:
@@ -201,7 +200,7 @@ def insert(self, doc_or_docs,
201200
Bulk insert works with any iterable
202201
"""
203202
docs = doc_or_docs
204-
if isinstance(docs, types.DictType):
203+
if isinstance(docs, dict):
205204
docs = [docs]
206205

207206
if manipulate:
@@ -263,11 +262,11 @@ def update(self, spec, document,
263262
264263
.. _update modifiers: http://www.mongodb.org/display/DOCS/Updating
265264
"""
266-
if not isinstance(spec, types.DictType):
265+
if not isinstance(spec, dict):
267266
raise TypeError("spec must be an instance of dict")
268-
if not isinstance(document, types.DictType):
267+
if not isinstance(document, dict):
269268
raise TypeError("document must be an instance of dict")
270-
if not isinstance(upsert, types.BooleanType):
269+
if not isinstance(upsert, bool):
271270
raise TypeError("upsert must be an instance of bool")
272271

273272
if upsert and manipulate:
@@ -322,7 +321,7 @@ def remove(self, spec_or_object_id=None, safe=False):
322321
if isinstance(spec, ObjectId):
323322
spec = {"_id": spec}
324323

325-
if not isinstance(spec, types.DictType):
324+
if not isinstance(spec, dict):
326325
raise TypeError("spec must be an instance of dict, not %s" %
327326
type(spec))
328327

@@ -366,7 +365,7 @@ def _fields_list_to_dict(self, fields):
366365
"""
367366
as_dict = {}
368367
for field in fields:
369-
if not isinstance(field, types.StringTypes):
368+
if not isinstance(field, basestring):
370369
raise TypeError("fields must be a list of key names as "
371370
"(string, unicode)")
372371
as_dict[field] = 1
@@ -425,19 +424,19 @@ def find(self, spec=None, fields=None, skip=0, limit=0,
425424

426425
slave_okay = self.__database.connection.slave_okay
427426

428-
if not isinstance(spec, types.DictType):
427+
if not isinstance(spec, dict):
429428
raise TypeError("spec must be an instance of dict")
430-
if not isinstance(fields, (types.ListType, types.NoneType)):
429+
if fields is not None and not isinstance(fields, list):
431430
raise TypeError("fields must be an instance of list")
432-
if not isinstance(skip, types.IntType):
431+
if not isinstance(skip, int):
433432
raise TypeError("skip must be an instance of int")
434-
if not isinstance(limit, types.IntType):
433+
if not isinstance(limit, int):
435434
raise TypeError("limit must be an instance of int")
436-
if not isinstance(timeout, types.BooleanType):
435+
if not isinstance(timeout, bool):
437436
raise TypeError("timeout must be an instance of bool")
438-
if not isinstance(snapshot, types.BooleanType):
437+
if not isinstance(snapshot, bool):
439438
raise TypeError("snapshot must be an instance of bool")
440-
if not isinstance(tailable, types.BooleanType):
439+
if not isinstance(tailable, bool):
441440
raise TypeError("tailable must be an instance of bool")
442441

443442
if fields is not None:
@@ -565,10 +564,10 @@ def drop_index(self, index_or_name):
565564
- `index_or_name`: index (or name of index) to drop
566565
"""
567566
name = index_or_name
568-
if isinstance(index_or_name, types.ListType):
567+
if isinstance(index_or_name, list):
569568
name = self._gen_index_name(index_or_name)
570569

571-
if not isinstance(name, types.StringTypes):
570+
if not isinstance(name, basestring):
572571
raise TypeError("index_or_name must be an index name or list")
573572

574573
self.__database.connection._purge_index(self.__database.name,
@@ -674,7 +673,7 @@ def rename(self, new_name):
674673
:Parameters:
675674
- `new_name`: new name for this collection
676675
"""
677-
if not isinstance(new_name, types.StringTypes):
676+
if not isinstance(new_name, basestring):
678677
raise TypeError("new_name must be an instance of (str, unicode)")
679678

680679
if not new_name or ".." in new_name:

pymongo/connection.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import sys
3737
import socket
3838
import struct
39-
import types
4039
import threading
4140
import random
4241
import errno
@@ -146,9 +145,9 @@ def __init__(self, host=None, port=None, pool_size=None,
146145
warnings.warn("The timeout parameter to Connection is deprecated",
147146
DeprecationWarning)
148147

149-
if not isinstance(host, types.StringTypes):
148+
if not isinstance(host, basestring):
150149
raise TypeError("host must be an instance of (str, unicode)")
151-
if not isinstance(port, types.IntType):
150+
if not isinstance(port, int):
152151
raise TypeError("port must be an instance of int")
153152

154153
self.__host = None
@@ -181,9 +180,9 @@ def __pair_with(self, host, port):
181180
pair with
182181
- `port`: the port number on which to connect
183182
"""
184-
if not isinstance(host, types.StringType):
183+
if not isinstance(host, str):
185184
raise TypeError("host must be an instance of str")
186-
if not isinstance(port, types.IntType):
185+
if not isinstance(port, int):
187186
raise TypeError("port must be an instance of int")
188187
self.__nodes.append((host, port))
189188

@@ -618,7 +617,7 @@ def close_cursor(self, cursor_id):
618617
.. seealso:: :meth:`set_cursor_manager` and
619618
the :mod:`~pymongo.cursor_manager` module
620619
"""
621-
if not isinstance(cursor_id, (types.IntType, types.LongType)):
620+
if not isinstance(cursor_id, (int, long)):
622621
raise TypeError("cursor_id must be an instance of (int, long)")
623622

624623
self.__cursor_manager.close(cursor_id)
@@ -632,7 +631,7 @@ def kill_cursors(self, cursor_ids):
632631
:Parameters:
633632
- `cursor_ids`: list of cursor ids to kill
634633
"""
635-
if not isinstance(cursor_ids, types.ListType):
634+
if not isinstance(cursor_ids, list):
636635
raise TypeError("cursor_ids must be a list")
637636
return self._send_message(message.kill_cursors(cursor_ids))
638637

@@ -668,7 +667,7 @@ def drop_database(self, name_or_database):
668667
if isinstance(name, Database):
669668
name = name.name
670669

671-
if not isinstance(name, types.StringTypes):
670+
if not isinstance(name, basestring):
672671
raise TypeError("name_or_database must be an instance of "
673672
"(Database, str, unicode)")
674673

pymongo/cursor.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
"""Cursor class to iterate over Mongo query results."""
1616

17-
import types
1817
import struct
1918
import warnings
2019

@@ -167,7 +166,7 @@ def limit(self, limit):
167166
:Parameters:
168167
- `limit`: the number of results to return
169168
"""
170-
if not isinstance(limit, types.IntType):
169+
if not isinstance(limit, int):
171170
raise TypeError("limit must be an int")
172171
self.__check_okay_to_chain()
173172

@@ -184,7 +183,7 @@ def skip(self, skip):
184183
:Parameters:
185184
- `skip`: the number of results to skip
186185
"""
187-
if not isinstance(skip, (types.IntType, types.LongType)):
186+
if not isinstance(skip, (int, long)):
188187
raise TypeError("skip must be an int")
189188
self.__check_okay_to_chain()
190189

@@ -221,7 +220,7 @@ def __getitem__(self, index):
221220
- `index`: An integer or slice index to be applied to this cursor
222221
"""
223222
self.__check_okay_to_chain()
224-
if isinstance(index, types.SliceType):
223+
if isinstance(index, slice):
225224
if index.step is not None:
226225
raise IndexError("Cursor instances do not support slice steps")
227226

@@ -242,7 +241,7 @@ def __getitem__(self, index):
242241
self.__limit = limit
243242
return self
244243

245-
if isinstance(index, (types.IntType, types.LongType)):
244+
if isinstance(index, (int, long)):
246245
if index < 0:
247246
raise IndexError("Cursor instances do not support negative indices")
248247
clone = self.clone()
@@ -328,7 +327,7 @@ def distinct(self, key):
328327
329328
.. versionadded:: 1.2
330329
"""
331-
if not isinstance(key, types.StringTypes):
330+
if not isinstance(key, basestring):
332331
raise TypeError("key must be an instance of (str, unicode)")
333332

334333
command = SON([("distinct", self.__collection.name), ("key", key)])
@@ -375,7 +374,7 @@ def hint(self, index):
375374
self.__hint = None
376375
return self
377376

378-
if not isinstance(index, (types.ListType)):
377+
if not isinstance(index, (list)):
379378
raise TypeError("hint takes a list specifying an index")
380379
self.__hint = helpers._index_document(index)
381380
return self
@@ -415,7 +414,7 @@ def __send_message(self, message):
415414
response = db.connection._send_message_with_response(message,
416415
**kwargs)
417416

418-
if isinstance(response, types.TupleType):
417+
if isinstance(response, tuple):
419418
(connection_id, response) = response
420419
else:
421420
connection_id = None

pymongo/cursor_manager.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
installed on a connection by calling
1919
`pymongo.connection.Connection.set_cursor_manager`."""
2020

21-
import types
22-
2321

2422
class CursorManager(object):
2523
"""The default cursor manager.
@@ -43,7 +41,7 @@ def close(self, cursor_id):
4341
:Parameters:
4442
- `cursor_id`: cursor id to close
4543
"""
46-
if not isinstance(cursor_id, (types.IntType, types.LongType)):
44+
if not isinstance(cursor_id, (int, long)):
4745
raise TypeError("cursor_id must be an instance of (int, long)")
4846

4947
self.__connection.kill_cursors([cursor_id])
@@ -78,7 +76,7 @@ def close(self, cursor_id):
7876
:Parameters:
7977
- `cursor_id`: cursor id to close
8078
"""
81-
if not isinstance(cursor_id, (types.IntType, types.LongType)):
79+
if not isinstance(cursor_id, (int, long)):
8280
raise TypeError("cursor_id must be an instance of (int, long)")
8381

8482
self.__dying_cursors.append(cursor_id)

0 commit comments

Comments
 (0)