-
Type:
Improvement
-
Resolution: Done
-
Priority:
Major - P3
-
Affects Version/s: None
-
Component/s: None
-
None
Any undefined attribute access on MongoClient gives you a Database, and Python always considers a Database "true":
if client.pirmary: # Oops. print("I'm connected!")
Database and Collection act this way too, leading to mistakes like the one we corrected here:
https://github.com/mongodb/mongo-python-driver/commit/73edd2c134f43d58531e3dc26499b777354fb7ea
Override __nonzero__ and __bool__ for Database and Collection to raise NotImplementedError, preventing accidental use of these objects as bools in Python 2 and 3, respectively.
This is a breaking change for code like this:
db = None if some_test(): db = get_a_database() if db: print("We got a Database")
It would need to be rewritten:
if db is not None: