-
Notifications
You must be signed in to change notification settings - Fork 15.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Override protobuf Message.__dir__ method
Add a new implementation of Message.__dir__ method that filters out attributes which are not accessible. Such an attribute is currently only `Extensions`, which is only accessible if any extension exists. Add a unit test that verifies all remaining attributes can be accessed. PiperOrigin-RevId: 700778715
- Loading branch information
1 parent
602b62a
commit 9668016
Showing
2 changed files
with
38 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,9 @@ | |
|
||
__author__ = '[email protected] (Will Robinson)' | ||
|
||
_INCONSISTENT_MESSAGE_ATTRIBUTES = ('Extensions',) | ||
|
||
|
||
class Error(Exception): | ||
"""Base error type for this module.""" | ||
pass | ||
|
@@ -56,6 +59,20 @@ def __deepcopy__(self, memo=None): | |
clone.MergeFrom(self) | ||
return clone | ||
|
||
def __dir__(self): | ||
"""Filters out attributes that would raise AttributeError if accessed.""" | ||
missing_attributes = set() | ||
for attribute in _INCONSISTENT_MESSAGE_ATTRIBUTES: | ||
try: | ||
getattr(self, attribute) | ||
except AttributeError: | ||
missing_attributes.add(attribute) | ||
return [ | ||
attribute | ||
for attribute in super().__dir__() | ||
if attribute not in missing_attributes | ||
] | ||
|
||
def __eq__(self, other_msg): | ||
"""Recursively compares two messages by value and structure.""" | ||
raise NotImplementedError | ||
|