Conversation
| sys.argv[1:], "h", ["help", "help-frameworks"]) | ||
| except getopt.GetoptError as err: | ||
| print(str(err)) | ||
| print(err) |
There was a problem hiding this comment.
Function main refactored with the following changes:
- Remove unnecessary call to
str()withinprint()(remove-str-from-print)
| try: | ||
| readme_content = f.read() | ||
| except: | ||
| readme_content = "" | ||
| readme_content = f.read() | ||
| except: | ||
| readme_content = "" |
There was a problem hiding this comment.
Lines 43-46 refactored with the following changes:
- Merge nested try-statement into a single try (
flatten-nested-try)
| '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"] |
There was a problem hiding this comment.
Lines 288-292 refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp) - Merge dictionary assignment with declaration (
merge-dict-assign)
This removes the following comments ( why? ):
# GSSAPI extras
| for name in _LIST_NAMES: | ||
| yield name | ||
|
|
||
| yield from _LIST_NAMES |
There was a problem hiding this comment.
Function gen_list_name refactored with the following changes:
- Replace yield inside for loop with yield from (
yield-from)
| 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") |
There was a problem hiding this comment.
Function _make_c_string_check refactored with the following changes:
- Lift code into else after jump in control flow (
reintroduce-else) - Merge else clause's nested if statement into elif (
merge-else-if-into-elif)
| 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 |
There was a problem hiding this comment.
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] |
There was a problem hiding this comment.
Function _json_convert refactored with the following changes:
- Replace list(), dict() or set() with comprehension (
collection-builtin-to-comprehension)
| 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 |
There was a problem hiding this comment.
Function _parse_canonical_datetime refactored with the following changes:
- Swap if/else branches (
swap-if-else-branches) - Remove unnecessary else after guard condition (
remove-unnecessary-else)
| 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 |
There was a problem hiding this comment.
Function _parse_canonical_dbpointer refactored with the following changes:
- Swap if/else branches (
swap-if-else-branches) - Remove unnecessary else after guard condition (
remove-unnecessary-else)
| 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) |
There was a problem hiding this comment.
Function default refactored with the following changes:
- Swap if/else branches (
swap-if-else-branches) - Remove unnecessary else after guard condition (
remove-unnecessary-else)
| second. | ||
| """ | ||
| timestamp = struct.unpack(">I", self.__id[0:4])[0] | ||
| timestamp = struct.unpack(">I", self.__id[:4])[0] |
There was a problem hiding this comment.
Function ObjectId.generation_time refactored with the following changes:
- Replace [0:x] with [:x] (
remove-redundant-slice-index)
| 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 |
There was a problem hiding this comment.
Function ObjectId.__setstate__ refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp)
| result = [] | ||
| for key in self.__keys: | ||
| result.append("(%r, %r)" % (key, self[key])) | ||
| result = ["(%r, %r)" % (key, self[key]) for key in self.__keys] |
There was a problem hiding this comment.
Function SON.__repr__ refactored with the following changes:
- Convert for loop into list comprehension (
list-comprehension)
| for k in self.__keys: | ||
| yield k | ||
| yield from self.__keys |
There was a problem hiding this comment.
Function SON.__iter__ refactored with the following changes:
- Replace yield inside for loop with yield from (
yield-from)
| 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)) |
There was a problem hiding this comment.
Function GridOut._ensure_file refactored with the following changes:
- Hoist conditional out of nested conditional (
hoist-if-from-if)
| 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") | ||
| ): |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Function CommandCursor.__init__ refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp)
| self.__batch_size = batch_size == 1 and 2 or batch_size | ||
| self.__batch_size = 2 if batch_size == 1 else batch_size |
There was a problem hiding this comment.
Function CommandCursor.batch_size refactored with the following changes:
- Replace boolean ternary with inline if expression (
ternary-to-if-expression)
| 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) |
There was a problem hiding this comment.
Function CommandCursor.__send_message refactored with the following changes:
- Merge nested if conditions (
merge-nested-ifs)
| 0 or a positive float. | ||
| """ | ||
| if value == 0 or value == "0": | ||
| if value in [0, "0"]: |
There was a problem hiding this comment.
Function validate_positive_float_or_zero refactored with the following changes:
- Replace multiple comparisons of same variable with
inoperator (merge-comparisons)
| if value is None: | ||
| raise ConfigurationError("%s cannot be None" % (option, )) | ||
| if value == 0 or value == "0": | ||
| if value in [0, "0"]: |
There was a problem hiding this comment.
Function validate_timeout_or_zero refactored with the following changes:
- Replace multiple comparisons of same variable with
inoperator (merge-comparisons)
| 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"]: |
There was a problem hiding this comment.
Function validate_max_staleness refactored with the following changes:
- Replace multiple comparisons of same variable with
inoperator (merge-comparisons)
|
|
||
| def __iter__(self): | ||
| return (key for key in self.__casedkeys) | ||
| return iter(self.__casedkeys) |
There was a problem hiding this comment.
Function _CaseInsensitiveDictionary.__iter__ refactored with the following changes:
- Simplify generator expression (
simplify-generator)
| return False | ||
|
|
||
| return True | ||
| return all(self[key] == other[key] for key in other) |
There was a problem hiding this comment.
Function _CaseInsensitiveDictionary.__eq__ refactored with the following changes:
- Use any() instead of for loop (
use-any) - Invert any/all to simplify comparisons (
invert-any-all)
| 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 |
There was a problem hiding this comment.
Function _CaseInsensitiveDictionary.setdefault refactored with the following changes:
- Remove unnecessary else after guard condition (
remove-unnecessary-else)
| 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 |
There was a problem hiding this comment.
Function _Query.get_message refactored with the following changes:
- Swap positions of nested conditionals (
swap-nested-ifs) - Hoist repeated code outside conditional statement (
hoist-statement-from-if) - Replace boolean ternary with inline if expression (
ternary-to-if-expression) - Replace if statement with if expression (
assign-if-exp)
This removes the following comments ( why? ):
# Set the secondaryOk bit.
| 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 | ||
|
|
There was a problem hiding this comment.
Function _GetMore.use_command refactored with the following changes:
- Merge duplicate blocks in conditional (
merge-duplicate-blocks) - Remove redundant conditional (
remove-redundant-if)
This removes the following comments ( why? ):
# OP_MSG supports exhaust on MongoDB 4.2+
| if self.sock_mgr: | ||
| flags = _OpMsg.EXHAUST_ALLOWED | ||
| else: | ||
| flags = 0 | ||
| flags = _OpMsg.EXHAUST_ALLOWED if self.sock_mgr else 0 |
There was a problem hiding this comment.
Function _GetMore.get_message refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp)
| 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 |
There was a problem hiding this comment.
Function _RawBatchQuery.use_command refactored with the following changes:
- Merge duplicate blocks in conditional (
merge-duplicate-blocks) - Remove redundant conditional (
remove-redundant-if) - Simplify conditional into return statement (
return-identity)
This removes the following comments ( why? ):
# MongoDB 4.2+ supports exhaust over OP_MSG
| 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 |
There was a problem hiding this comment.
Function _RawBatchGetMore.use_command refactored with the following changes:
- Merge duplicate blocks in conditional (
merge-duplicate-blocks) - Remove redundant conditional (
remove-redundant-if) - Simplify conditional into return statement (
return-identity)
This removes the following comments ( why? ):
# MongoDB 4.2+ supports exhaust over OP_MSG
Sourcery Code Quality Report✅ Merging this PR will increase code quality in the affected files by 0.20%.
Here are some functions in these files that still need a tune-up:
Legend and ExplanationThe emojis denote the absolute quality of the code:
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! |
Branch
masterrefactored 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
masterbranch, then run:Help us improve this pull request!