Skip to content

PYTHON-1834 [v4.0] Use a code formatter #854

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
263 changes: 128 additions & 135 deletions bson/__init__.py

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions bson/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def _setstate_slots(self, state):

def _mangle_name(name, prefix):
if name.startswith("__"):
prefix = "_"+prefix
prefix = "_" + prefix
else:
prefix = ""
return prefix + name
Expand All @@ -36,5 +36,5 @@ def _getstate_slots(self):
for name in self.__slots__:
mangled_name = _mangle_name(name, prefix)
if hasattr(self, mangled_name):
ret[mangled_name] = getattr(self, mangled_name)
ret[mangled_name] = getattr(self, mangled_name)
return ret
53 changes: 29 additions & 24 deletions bson/binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,17 +153,20 @@ class UuidRepresentation:
"""

ALL_UUID_SUBTYPES = (OLD_UUID_SUBTYPE, UUID_SUBTYPE)
ALL_UUID_REPRESENTATIONS = (UuidRepresentation.UNSPECIFIED,
UuidRepresentation.STANDARD,
UuidRepresentation.PYTHON_LEGACY,
UuidRepresentation.JAVA_LEGACY,
UuidRepresentation.CSHARP_LEGACY)
ALL_UUID_REPRESENTATIONS = (
UuidRepresentation.UNSPECIFIED,
UuidRepresentation.STANDARD,
UuidRepresentation.PYTHON_LEGACY,
UuidRepresentation.JAVA_LEGACY,
UuidRepresentation.CSHARP_LEGACY,
)
UUID_REPRESENTATION_NAMES = {
UuidRepresentation.UNSPECIFIED: 'UuidRepresentation.UNSPECIFIED',
UuidRepresentation.STANDARD: 'UuidRepresentation.STANDARD',
UuidRepresentation.PYTHON_LEGACY: 'UuidRepresentation.PYTHON_LEGACY',
UuidRepresentation.JAVA_LEGACY: 'UuidRepresentation.JAVA_LEGACY',
UuidRepresentation.CSHARP_LEGACY: 'UuidRepresentation.CSHARP_LEGACY'}
UuidRepresentation.UNSPECIFIED: "UuidRepresentation.UNSPECIFIED",
UuidRepresentation.STANDARD: "UuidRepresentation.STANDARD",
UuidRepresentation.PYTHON_LEGACY: "UuidRepresentation.PYTHON_LEGACY",
UuidRepresentation.JAVA_LEGACY: "UuidRepresentation.JAVA_LEGACY",
UuidRepresentation.CSHARP_LEGACY: "UuidRepresentation.CSHARP_LEGACY",
}

MD5_SUBTYPE = 5
"""BSON binary subtype for an MD5 hash.
Expand Down Expand Up @@ -244,8 +247,9 @@ def from_uuid(cls, uuid, uuid_representation=UuidRepresentation.STANDARD):
raise TypeError("uuid must be an instance of uuid.UUID")

if uuid_representation not in ALL_UUID_REPRESENTATIONS:
raise ValueError("uuid_representation must be a value "
"from bson.binary.UuidRepresentation")
raise ValueError(
"uuid_representation must be a value " "from bson.binary.UuidRepresentation"
)

if uuid_representation == UuidRepresentation.UNSPECIFIED:
raise ValueError(
Expand All @@ -254,7 +258,8 @@ def from_uuid(cls, uuid, uuid_representation=UuidRepresentation.STANDARD):
"converted to bson.Binary instances using "
"bson.Binary.from_uuid() or a different UuidRepresentation "
"can be configured. See the documentation for "
"UuidRepresentation for more information.")
"UuidRepresentation for more information."
)

subtype = OLD_UUID_SUBTYPE
if uuid_representation == UuidRepresentation.PYTHON_LEGACY:
Expand Down Expand Up @@ -289,12 +294,12 @@ def as_uuid(self, uuid_representation=UuidRepresentation.STANDARD):
.. versionadded:: 3.11
"""
if self.subtype not in ALL_UUID_SUBTYPES:
raise ValueError("cannot decode subtype %s as a uuid" % (
self.subtype,))
raise ValueError("cannot decode subtype %s as a uuid" % (self.subtype,))

if uuid_representation not in ALL_UUID_REPRESENTATIONS:
raise ValueError("uuid_representation must be a value from "
"bson.binary.UuidRepresentation")
raise ValueError(
"uuid_representation must be a value from " "bson.binary.UuidRepresentation"
)

if uuid_representation == UuidRepresentation.UNSPECIFIED:
raise ValueError("uuid_representation cannot be UNSPECIFIED")
Expand All @@ -312,26 +317,26 @@ def as_uuid(self, uuid_representation=UuidRepresentation.STANDARD):
if self.subtype == UUID_SUBTYPE:
return UUID(bytes=self)

raise ValueError("cannot decode subtype %s to %s" % (
self.subtype, UUID_REPRESENTATION_NAMES[uuid_representation]))
raise ValueError(
"cannot decode subtype %s to %s"
% (self.subtype, UUID_REPRESENTATION_NAMES[uuid_representation])
)

@property
def subtype(self):
"""Subtype of this binary data.
"""
"""Subtype of this binary data."""
return self.__subtype

def __getnewargs__(self):
# Work around http://bugs.python.org/issue7382
data = super(Binary, self).__getnewargs__()[0]
if not isinstance(data, bytes):
data = data.encode('latin-1')
data = data.encode("latin-1")
return data, self.__subtype

def __eq__(self, other):
if isinstance(other, Binary):
return ((self.__subtype, bytes(self)) ==
(other.subtype, bytes(other)))
return (self.__subtype, bytes(self)) == (other.subtype, bytes(other))
# We don't return NotImplemented here because if we did then
# Binary("foo") == "foo" would return True, since Binary is a
# subclass of str...
Expand Down
3 changes: 1 addition & 2 deletions bson/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ def __new__(cls, code, scope=None, **kwargs):

@property
def scope(self):
"""Scope dictionary for this instance or ``None``.
"""
"""Scope dictionary for this instance or ``None``."""
return self.__scope

def __repr__(self):
Expand Down
Loading