Skip to content

Commit

Permalink
fix: tweak to_python (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
software-dov committed Aug 3, 2020
1 parent 621a1a1 commit 5459ede
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 30 deletions.
21 changes: 5 additions & 16 deletions proto/_file_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,28 +42,17 @@ def _get_manifest(self, new_class):
def _get_remaining_manifest(self, new_class):
return self._get_manifest(new_class) - {new_class.__name__}

def _has_manifest(self, new_class):
return len(self._get_manifest(new_class)) > 0

def _is_in_manifest(self, new_class):
return new_class.__name__ in self._get_manifest(new_class)

def _calculate_salt(self, new_class, fallback):
if self._has_manifest(new_class=new_class) and not self._is_in_manifest(
new_class=new_class
):
manifest = self._get_manifest(new_class)
if manifest and new_class.__name__ not in manifest:
log.warning(
"proto-plus module {module} has a declared manifest but {classname} is not in it".format(
"proto-plus module {module} has a declared manifest but {class_name} is not in it".format(
module=inspect.getmodule(new_class).__name__,
classname=new_class.__name__,
class_name=new_class.__name__,
)
)

return (
""
if self._is_in_manifest(new_class=new_class)
else (fallback or "").lower()
)
return "" if new_class.__name__ in manifest else (fallback or "").lower()

def generate_file_pb(self, new_class, fallback_salt=""):
"""Generate the descriptors for all protos in the file.
Expand Down
7 changes: 4 additions & 3 deletions proto/marshal/marshal.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,14 @@ def reset(self):
def to_python(self, proto_type, value, *, absent: bool = None):
# Internal protobuf has its own special type for lists of values.
# Return a view around it that implements MutableSequence.
if isinstance(value, compat.repeated_composite_types):
value_type = type(value) # Minor performance boost over isinstance
if value_type in compat.repeated_composite_types:
return RepeatedComposite(value, marshal=self)
if isinstance(value, compat.repeated_scalar_types):
if value_type in compat.repeated_scalar_types:
return Repeated(value, marshal=self)

# Same thing for maps of messages.
if isinstance(value, compat.map_composite_types):
if value_type in compat.map_composite_types:
return MapComposite(value, marshal=self)

# Convert ordinary values.
Expand Down
22 changes: 11 additions & 11 deletions proto/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,20 +370,9 @@ def __init__(self, mapping=None, **kwargs):
# * An instance of the underlying protobuf descriptor class.
# * A dict
# * Nothing (keyword arguments only).
#
# Sanity check: Did we get something not on that list? Error if so.
if mapping and not isinstance(
mapping, (collections.abc.Mapping, type(self), self._meta.pb)
):
raise TypeError(
"Invalid constructor input for %s: %r"
% (self.__class__.__name__, mapping,)
)

# Handle the first two cases: they both involve keeping
# a copy of the underlying protobuf descriptor instance.
if isinstance(mapping, type(self)):
mapping = mapping._pb
if isinstance(mapping, self._meta.pb):
# Make a copy of the mapping.
# This is a constructor for a new object, so users will assume
Expand All @@ -400,12 +389,23 @@ def __init__(self, mapping=None, **kwargs):
if kwargs:
self._pb.MergeFrom(self._meta.pb(**kwargs))
return
if isinstance(mapping, type(self)):
# Performance hack to streamline construction from vanilla protos.
self.__init__(mapping=mapping._pb, **kwargs)
return

# Handle the remaining case by converging the mapping and kwargs
# dictionaries (with kwargs winning), and saving a descriptor
# based on that.

if mapping is None:
mapping = {}
elif not isinstance(mapping, collections.abc.Mapping):
# Sanity check: Did we get something not a map? Error if so.
raise TypeError(
"Invalid constructor input for %s: %r"
% (self.__class__.__name__, mapping,)
)
mapping.update(kwargs)

# Avoid copying the mapping unnecessarily
Expand Down

0 comments on commit 5459ede

Please sign in to comment.