Skip to content

Sourcery refactored master branch#1

Open
sourcery-ai[bot] wants to merge 1 commit intomasterfrom
sourcery/master
Open

Sourcery refactored master branch#1
sourcery-ai[bot] wants to merge 1 commit intomasterfrom
sourcery/master

Conversation

@sourcery-ai
Copy link
Copy Markdown

@sourcery-ai sourcery-ai bot commented Dec 24, 2021

Branch master refactored by Sourcery.

If you're happy with these changes, merge this Pull Request using the Squash and merge strategy.

See our documentation here.

Run Sourcery locally

Reduce the feedback loop during development by using the Sourcery editor plugin:

Review changes via command line

To manually merge these changes, make sure you're on the master branch, then run:

git fetch origin sourcery/master
git merge --ff-only FETCH_HEAD
git reset HEAD^

Help us improve this pull request!

Copy link
Copy Markdown
Author

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sourcery timed out performing refactorings.

Due to GitHub API limits, only the first 60 comments can be shown.

sys.argv[1:], "h", ["help", "help-frameworks"])
except getopt.GetoptError as err:
print(str(err))
print(err)
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function main refactored with the following changes:

Comment on lines -43 to +45
try:
readme_content = f.read()
except:
readme_content = ""
readme_content = f.read()
except:
readme_content = ""
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 43-46 refactored with the following changes:

Comment on lines +285 to -292
'gssapi': ["winkerberos>=0.5.0"]
if sys.platform == 'win32'
else ["pykerberos"],
}

# GSSAPI extras
if sys.platform == 'win32':
extras_require['gssapi'] = ["winkerberos>=0.5.0"]
else:
extras_require['gssapi'] = ["pykerberos"]
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 288-292 refactored with the following changes:

This removes the following comments ( why? ):

# GSSAPI extras

for name in _LIST_NAMES:
yield name

yield from _LIST_NAMES
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function gen_list_name refactored with the following changes:

  • Replace yield inside for loop with yield from (yield-from)

Comment on lines +482 to -487
elif "\x00" in string:
raise InvalidDocument("BSON keys / regex patterns must not "
"contain a NUL character")
else:
if "\x00" in string:
raise InvalidDocument("BSON keys / regex patterns must not "
"contain a NUL character")
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function _make_c_string_check refactored with the following changes:

Comment on lines -255 to +260
mask = mask << 8
mask <<= 8

mask = 0x00000000000000ff
for i in range(6, 0, -1):
arr[i] = (high & mask) >> ((6 - i) << 3)
mask = mask << 8
mask <<= 8
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Decimal128.to_decimal refactored with the following changes:

  • Replace assignment with augmented assignment (aug-assign)

for k, v in obj.items()))
elif hasattr(obj, '__iter__') and not isinstance(obj, (str, bytes)):
return list((_json_convert(v, json_options) for v in obj))
return [_json_convert(v, json_options) for v in obj]
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function _json_convert refactored with the following changes:

Comment on lines -630 to +634
if json_options.tz_aware:
if json_options.tzinfo:
aware = aware.astimezone(json_options.tzinfo)
return aware
else:
if not json_options.tz_aware:
return aware.replace(tzinfo=None)
if json_options.tzinfo:
aware = aware.astimezone(json_options.tzinfo)
return aware
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function _parse_canonical_datetime refactored with the following changes:

Comment on lines -688 to +700
if isinstance(dbref, DBRef):
dbref_doc = dbref.as_doc()
# DBPointer must not contain $db in its value.
if dbref.database is not None:
raise TypeError(
'Bad $dbPointer, extra field $db: %s' % (dbref_doc,))
if not isinstance(dbref.id, ObjectId):
raise TypeError(
'Bad $dbPointer, $id must be an ObjectId: %s' % (dbref_doc,))
if len(dbref_doc) != 2:
raise TypeError(
'Bad $dbPointer, extra field(s) in DBRef: %s' % (dbref_doc,))
return dbref
else:
if not isinstance(dbref, DBRef):
raise TypeError('Bad $dbPointer, expected a DBRef: %s' % (doc,))
dbref_doc = dbref.as_doc()
# DBPointer must not contain $db in its value.
if dbref.database is not None:
raise TypeError(
'Bad $dbPointer, extra field $db: %s' % (dbref_doc,))
if not isinstance(dbref.id, ObjectId):
raise TypeError(
'Bad $dbPointer, $id must be an ObjectId: %s' % (dbref_doc,))
if len(dbref_doc) != 2:
raise TypeError(
'Bad $dbPointer, extra field(s) in DBRef: %s' % (dbref_doc,))
return dbref
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function _parse_canonical_dbpointer refactored with the following changes:

Comment on lines -840 to +842
if json_options.strict_uuid:
binval = Binary.from_uuid(
obj, uuid_representation=json_options.uuid_representation)
return _encode_binary(binval, binval.subtype, json_options)
else:
if not json_options.strict_uuid:
return {"$uuid": obj.hex}
binval = Binary.from_uuid(
obj, uuid_representation=json_options.uuid_representation)
return _encode_binary(binval, binval.subtype, json_options)
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function default refactored with the following changes:

second.
"""
timestamp = struct.unpack(">I", self.__id[0:4])[0]
timestamp = struct.unpack(">I", self.__id[:4])[0]
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function ObjectId.generation_time refactored with the following changes:

Comment on lines -241 to +245
if isinstance(value, dict):
oid = value["_ObjectId__id"]
else:
oid = value
oid = value["_ObjectId__id"] if isinstance(value, dict) else value
# ObjectIds pickled in python 2.x used `str` for __id.
# In python 3.x this has to be converted to `bytes`
# by encoding latin-1.
if isinstance(oid, str):
self.__id = oid.encode('latin-1')
else:
self.__id = oid
self.__id = oid.encode('latin-1') if isinstance(oid, str) else oid
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function ObjectId.__setstate__ refactored with the following changes:

result = []
for key in self.__keys:
result.append("(%r, %r)" % (key, self[key]))
result = ["(%r, %r)" % (key, self[key]) for key in self.__keys]
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function SON.__repr__ refactored with the following changes:

Comment on lines -74 to +72
for k in self.__keys:
yield k
yield from self.__keys
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function SON.__iter__ refactored with the following changes:

  • Replace yield inside for loop with yield from (yield-from)

Comment on lines -490 to +492
if not self._file:
raise NoFile("no file in gridfs collection %r with _id %r" %
(self.__files, self.__file_id))
if not self._file:
raise NoFile("no file in gridfs collection %r with _id %r" %
(self.__files, self.__file_id))
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function GridOut._ensure_file refactored with the following changes:

Comment on lines -160 to +164
if "$" in name and not (name.startswith("oplog.$main") or
name.startswith("$cmd")):
if (
"$" in name
and not name.startswith("oplog.$main")
and not name.startswith("$cmd")
):
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Collection.__init__ refactored with the following changes:

  • Simplify logical expression using De Morgan identities (de-morgan)

else:
self.__ns = collection.full_name

self.__ns = cursor_info["ns"] if "ns" in cursor_info else collection.full_name
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function CommandCursor.__init__ refactored with the following changes:

Comment on lines -121 to +117
self.__batch_size = batch_size == 1 and 2 or batch_size
self.__batch_size = 2 if batch_size == 1 else batch_size
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function CommandCursor.batch_size refactored with the following changes:

Comment on lines -173 to +171
if isinstance(response, PinnedResponse):
if not self.__sock_mgr:
self.__sock_mgr = _SocketManager(response.socket_info,
response.more_to_come)
if isinstance(response, PinnedResponse) and not self.__sock_mgr:
self.__sock_mgr = _SocketManager(response.socket_info,
response.more_to_come)
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function CommandCursor.__send_message refactored with the following changes:

0 or a positive float.
"""
if value == 0 or value == "0":
if value in [0, "0"]:
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function validate_positive_float_or_zero refactored with the following changes:

if value is None:
raise ConfigurationError("%s cannot be None" % (option, ))
if value == 0 or value == "0":
if value in [0, "0"]:
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function validate_timeout_or_zero refactored with the following changes:

def validate_max_staleness(option, value):
"""Validates maxStalenessSeconds according to the Max Staleness Spec."""
if value == -1 or value == "-1":
if value in [-1, "-1"]:
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function validate_max_staleness refactored with the following changes:


def __iter__(self):
return (key for key in self.__casedkeys)
return iter(self.__casedkeys)
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function _CaseInsensitiveDictionary.__iter__ refactored with the following changes:

return False

return True
return all(self[key] == other[key] for key in other)
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function _CaseInsensitiveDictionary.__eq__ refactored with the following changes:

Comment on lines -936 to +934
else:
self.__casedkeys[lc_key] = key
self.__data[lc_key] = default
return default
self.__casedkeys[lc_key] = key
self.__data[lc_key] = default
return default
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function _CaseInsensitiveDictionary.setdefault refactored with the following changes:

Comment on lines -319 to +316
if read_preference.mode:
# Set the secondaryOk bit.
flags = self.flags | 4
else:
flags = self.flags

flags = self.flags | 4 if read_preference.mode else self.flags
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function _Query.get_message refactored with the following changes:

This removes the following comments ( why? ):

# Set the secondaryOk bit.

Comment on lines -383 to -388
if not self.exhaust:
use_cmd = True
elif sock_info.max_wire_version >= 8:
# OP_MSG supports exhaust on MongoDB 4.2+
if not self.exhaust or sock_info.max_wire_version >= 8:
use_cmd = True

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function _GetMore.use_command refactored with the following changes:

This removes the following comments ( why? ):

# OP_MSG supports exhaust on MongoDB 4.2+

Comment on lines -421 to +405
if self.sock_mgr:
flags = _OpMsg.EXHAUST_ALLOWED
else:
flags = 0
flags = _OpMsg.EXHAUST_ALLOWED if self.sock_mgr else 0
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function _GetMore.get_message refactored with the following changes:

Comment on lines -437 to +418
if sock_info.max_wire_version >= 8:
# MongoDB 4.2+ supports exhaust over OP_MSG
return True
elif not self.exhaust:
return True
return False
return sock_info.max_wire_version >= 8 or not self.exhaust
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function _RawBatchQuery.use_command refactored with the following changes:

This removes the following comments ( why? ):

# MongoDB 4.2+ supports exhaust over OP_MSG

Comment on lines -449 to +425
if sock_info.max_wire_version >= 8:
# MongoDB 4.2+ supports exhaust over OP_MSG
return True
elif not self.exhaust:
return True
return False
return sock_info.max_wire_version >= 8 or not self.exhaust
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function _RawBatchGetMore.use_command refactored with the following changes:

This removes the following comments ( why? ):

# MongoDB 4.2+ supports exhaust over OP_MSG

@sourcery-ai
Copy link
Copy Markdown
Author

sourcery-ai bot commented Dec 24, 2021

Sourcery Code Quality Report

✅  Merging this PR will increase code quality in the affected files by 0.20%.

Quality metrics Before After Change
Complexity 7.41 ⭐ 7.17 ⭐ -0.24 👍
Method Length 53.08 ⭐ 52.95 ⭐ -0.13 👍
Working memory 7.35 🙂 7.34 🙂 -0.01 👍
Quality 69.86% 🙂 70.06% 🙂 0.20% 👍
Other metrics Before After Change
Lines 47302 47035 -267
Changed files Quality Before Quality After Quality Change
green_framework_test.py 76.41% ⭐ 76.56% ⭐ 0.15% 👍
setup.py 63.05% 🙂 63.98% 🙂 0.93% 👍
bson/init.py 73.40% 🙂 73.47% 🙂 0.07% 👍
bson/_helpers.py 90.03% ⭐ 89.94% ⭐ -0.09% 👎
bson/binary.py 69.04% 🙂 70.09% 🙂 1.05% 👍
bson/codec_options.py 76.88% ⭐ 77.27% ⭐ 0.39% 👍
bson/decimal128.py 67.69% 🙂 67.81% 🙂 0.12% 👍
bson/json_util.py 53.83% 🙂 54.28% 🙂 0.45% 👍
bson/objectid.py 89.58% ⭐ 89.83% ⭐ 0.25% 👍
bson/son.py 88.06% ⭐ 87.86% ⭐ -0.20% 👎
gridfs/grid_file.py 79.42% ⭐ 79.51% ⭐ 0.09% 👍
pymongo/auth.py 56.44% 🙂 58.20% 🙂 1.76% 👍
pymongo/bulk.py 64.54% 🙂 64.79% 🙂 0.25% 👍
pymongo/client_options.py 73.75% 🙂 74.47% 🙂 0.72% 👍
pymongo/client_session.py 82.06% ⭐ 82.38% ⭐ 0.32% 👍
pymongo/collection.py 64.44% 🙂 64.47% 🙂 0.03% 👍
pymongo/command_cursor.py 78.25% ⭐ 79.07% ⭐ 0.82% 👍
pymongo/common.py 86.46% ⭐ 86.62% ⭐ 0.16% 👍
pymongo/cursor.py 61.98% 🙂 62.11% 🙂 0.13% 👍
pymongo/encryption.py 80.26% ⭐ 80.32% ⭐ 0.06% 👍
pymongo/encryption_options.py 62.40% 🙂 62.51% 🙂 0.11% 👍
pymongo/errors.py 94.14% ⭐ 94.60% ⭐ 0.46% 👍
pymongo/helpers.py 68.56% 🙂 70.51% 🙂 1.95% 👍
pymongo/message.py 68.17% 🙂 68.29% 🙂 0.12% 👍
pymongo/mongo_client.py 63.84% 🙂 64.00% 🙂 0.16% 👍
pymongo/monitoring.py 89.62% ⭐ 89.65% ⭐ 0.03% 👍
pymongo/network.py 31.40% 😞 31.56% 😞 0.16% 👍
pymongo/ocsp_support.py 53.63% 🙂 53.14% 🙂 -0.49% 👎
pymongo/operations.py 84.05% ⭐ 84.86% ⭐ 0.81% 👍
pymongo/pool.py 61.18% 🙂 61.22% 🙂 0.04% 👍
pymongo/pyopenssl_context.py 83.05% ⭐ 83.22% ⭐ 0.17% 👍
pymongo/results.py 97.09% ⭐ 97.12% ⭐ 0.03% 👍
pymongo/server.py 42.87% 😞 44.52% 😞 1.65% 👍
pymongo/server_description.py 85.54% ⭐ 86.17% ⭐ 0.63% 👍
pymongo/server_selectors.py 92.40% ⭐ 93.53% ⭐ 1.13% 👍
pymongo/socket_checker.py 66.12% 🙂 64.02% 🙂 -2.10% 👎
pymongo/topology.py 66.82% 🙂 67.10% 🙂 0.28% 👍
pymongo/topology_description.py 64.98% 🙂 65.02% 🙂 0.04% 👍
pymongo/uri_parser.py 49.39% 😞 49.42% 😞 0.03% 👍
test/init.py 71.97% 🙂 72.26% 🙂 0.29% 👍
test/pymongo_mocks.py 71.88% 🙂 72.79% 🙂 0.91% 👍
test/qcheck.py 78.47% ⭐ 78.34% ⭐ -0.13% 👎
test/test_auth.py 66.81% 🙂 66.83% 🙂 0.02% 👍
test/test_auth_spec.py 38.86% 😞 40.68% 😞 1.82% 👍
test/test_binary.py 81.69% ⭐ 81.67% ⭐ -0.02% 👎
test/test_bson.py 79.90% ⭐ 79.85% ⭐ -0.05% 👎
test/test_change_stream.py 77.20% ⭐ 77.19% ⭐ -0.01% 👎
test/test_client.py 78.39% ⭐ 78.40% ⭐ 0.01% 👍
test/test_collection.py 70.68% 🙂 70.77% 🙂 0.09% 👍
test/test_command_monitoring_legacy.py 18.98% ⛔ 19.19% ⛔ 0.21% 👍
test/test_crud_v1.py 58.88% 🙂 60.82% 🙂 1.94% 👍
test/test_cursor.py 64.34% 🙂 64.91% 🙂 0.57% 👍
test/test_custom_types.py 82.35% ⭐ 82.36% ⭐ 0.01% 👍
test/test_database.py 72.76% 🙂 72.82% 🙂 0.06% 👍
test/test_dns.py 25.92% 😞 27.09% 😞 1.17% 👍
test/test_encryption.py 74.31% 🙂 74.32% 🙂 0.01% 👍
test/test_examples.py 57.21% 🙂 57.44% 🙂 0.23% 👍
test/test_gridfs.py 81.14% ⭐ 81.08% ⭐ -0.06% 👎
test/test_gridfs_bucket.py 83.19% ⭐ 83.14% ⭐ -0.05% 👎
test/test_gridfs_spec.py 51.03% 🙂 51.36% 🙂 0.33% 👍
test/test_mongos_load_balancing.py 83.31% ⭐ 83.32% ⭐ 0.01% 👍
test/test_monitoring.py 60.28% 🙂 60.28% 🙂 0.00%
test/test_pooling.py 81.15% ⭐ 81.24% ⭐ 0.09% 👍
test/test_read_preferences.py 75.16% ⭐ 75.16% ⭐ 0.00%
test/test_sdam_monitoring_spec.py 57.97% 🙂 58.02% 🙂 0.05% 👍
test/test_server_selection_in_window.py 73.26% 🙂 73.35% 🙂 0.09% 👍
test/test_session.py 72.35% 🙂 72.36% 🙂 0.01% 👍
test/test_srv_polling.py 89.47% ⭐ 89.44% ⭐ -0.03% 👎
test/test_threads.py 88.52% ⭐ 88.35% ⭐ -0.17% 👎
test/test_uri_parser.py 66.46% 🙂 66.46% 🙂 0.00%
test/test_uri_spec.py 40.64% 😞 40.64% 😞 0.00%
test/unified_format.py 62.59% 🙂 62.54% 🙂 -0.05% 👎
test/utils.py 79.48% ⭐ 79.53% ⭐ 0.05% 👍
test/utils_selection_tests.py 48.18% 😞 49.34% 😞 1.16% 👍
test/utils_spec_runner.py 57.76% 🙂 58.48% 🙂 0.72% 👍
test/version.py 78.37% ⭐ 78.65% ⭐ 0.28% 👍
test/mockupdb/test_handshake.py 51.62% 🙂 52.42% 🙂 0.80% 👍
test/mockupdb/test_op_msg_read_preference.py 68.67% 🙂 69.33% 🙂 0.66% 👍
test/mockupdb/test_slave_okay_rs.py 86.87% ⭐ 86.95% ⭐ 0.08% 👍
test/mod_wsgi_test/test_client.py 60.44% 🙂 60.44% 🙂 0.00%
test/ocsp/test_ocsp.py 91.88% ⭐ 93.06% ⭐ 1.18% 👍

Here are some functions in these files that still need a tune-up:

File Function Complexity Length Working Memory Quality Recommendation
pymongo/network.py command 45 ⛔ 427 ⛔ 30 ⛔ 6.09% ⛔ Refactor to reduce nesting. Try splitting into smaller methods. Extract out complex expressions
pymongo/uri_parser.py parse_uri 63 ⛔ 439 ⛔ 20 ⛔ 6.51% ⛔ Refactor to reduce nesting. Try splitting into smaller methods. Extract out complex expressions
test/test_command_monitoring_legacy.py create_test 137 ⛔ 609 ⛔ 16 ⛔ 7.59% ⛔ Refactor to reduce nesting. Try splitting into smaller methods. Extract out complex expressions
test/test_command_monitoring_legacy.py create_test.run_scenario 104 ⛔ 603 ⛔ 16 ⛔ 7.71% ⛔ Refactor to reduce nesting. Try splitting into smaller methods. Extract out complex expressions
test/utils.py prepare_spec_arguments 41 ⛔ 343 ⛔ 26 ⛔ 9.19% ⛔ Refactor to reduce nesting. Try splitting into smaller methods. Extract out complex expressions

Legend and Explanation

The emojis denote the absolute quality of the code:

  • ⭐ excellent
  • 🙂 good
  • 😞 poor
  • ⛔ very poor

The 👍 and 👎 indicate whether the quality has improved or gotten worse with this pull request.


Please see our documentation here for details on how these metrics are calculated.

We are actively working on this report - lots more documentation and extra metrics to come!

Help us improve this quality report!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants