Skip to content
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

feat: add custom __dir__ for messages and message classes #289

Merged
merged 2 commits into from
Feb 7, 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
44 changes: 44 additions & 0 deletions proto/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,27 @@ def __prepare__(mcls, name, bases, **kwargs):
def meta(cls):
return cls._meta

def __dir__(self):
names = set(dir(type))
names.update(
(
"meta",
"pb",
"wrap",
"serialize",
"deserialize",
"to_json",
"from_json",
"to_dict",
"copy_from",
)
)
desc = self.pb().DESCRIPTOR
names.update(t.name for t in desc.nested_types)
names.update(e.name for e in desc.enum_types)

return names

def pb(cls, obj=None, *, coerce: bool = False):
"""Return the underlying protobuf Message class or instance.

Expand Down Expand Up @@ -520,6 +541,29 @@ def __init__(
# Create the internal protocol buffer.
super().__setattr__("_pb", self._meta.pb(**params))

def __dir__(self):
desc = type(self).pb().DESCRIPTOR
names = {f_name for f_name in self._meta.fields.keys()}
names.update(m.name for m in desc.nested_types)
names.update(e.name for e in desc.enum_types)
names.update(dir(object()))
Copy link
Contributor

Choose a reason for hiding this comment

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

What does this do?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's including all the fields message fields, nested message and enum types, and boilerplate methods and attributes inherited from object. The important one is fields, since they weren't visible before, but since this is customizing __dir__ we need to explicitly add all the others back in too.

# Can't think of a better way of determining
# the special methods than manually listing them.
names.update(
(
"__bool__",
"__contains__",
"__dict__",
"__getattr__",
"__getstate__",
"__module__",
"__setstate__",
"__weakref__",
)
)

return names

def __bool__(self):
"""Return True if any field is truthy, False otherwise."""
return any(k in self and getattr(self, k) for k in self._meta.fields.keys())
Expand Down
69 changes: 69 additions & 0 deletions tests/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,3 +346,72 @@ class Squid(proto.Message):

with pytest.raises(TypeError):
Mollusc.Squid.copy_from(m.squid, (("mass_kg", 20)))


def test_dir():
class Mollusc(proto.Message):
class Class(proto.Enum):
UNKNOWN = 0
GASTROPOD = 1
BIVALVE = 2
CEPHALOPOD = 3

class Arm(proto.Message):
length_cm = proto.Field(proto.INT32, number=1)

mass_kg = proto.Field(proto.INT32, number=1)
class_ = proto.Field(Class, number=2)
arms = proto.RepeatedField(Arm, number=3)

expected = (
{
# Fields and nested message and enum types
"arms",
"class_",
"mass_kg",
"Arm",
"Class",
}
| {
# Other methods and attributes
"__bool__",
"__contains__",
"__dict__",
"__getattr__",
"__getstate__",
"__module__",
"__setstate__",
"__weakref__",
}
| set(dir(object))
) # Gets the long tail of dunder methods and attributes.

actual = set(dir(Mollusc()))

# Check instance names
assert actual == expected

# Check type names
expected = (
set(dir(type))
| {
# Class methods from the MessageMeta metaclass
"copy_from",
"deserialize",
"from_json",
"meta",
"pb",
"serialize",
"to_dict",
"to_json",
"wrap",
}
| {
# Nested message and enum types
"Arm",
"Class",
}
)

actual = set(dir(Mollusc))
assert actual == expected