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

Cleaner ecolab.inspect for proto #662

Merged
merged 1 commit into from
Oct 29, 2024
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ Changelog follow https://keepachangelog.com/ format.

## [Unreleased]

* `ecolab`:
* `ecolab.inspect`: Proto are better displayed (hide attributes
`DESCRIPTOR`, `Extensions` in sub-section)

## [1.10.0] - 2024-10-17

* `epy`:
Expand Down
38 changes: 38 additions & 0 deletions etils/ecolab/inspects/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from etils import enp
from etils.ecolab.inspects import attrs
from etils.ecolab.inspects import html_helper as H
from google.protobuf import message

# Import both C++ and Python API
from google.protobuf.internal import containers
Expand Down Expand Up @@ -76,6 +77,7 @@ def from_obj(cls, obj: object, *, name: str = '') -> ObjectNode:
SetNode,
ClsNode,
ArrayNode,
ProtoNode,
ExceptionNode,
ObjectNode,
]:
Expand Down Expand Up @@ -371,6 +373,8 @@ def children(self) -> list[Node]:
else:
mro = self.obj.mro()
# Add `[[mro]]` subsection
# TODO(epot): Also add `[[mro]]` section to all objects (but hidden inside)
# private.
return super().children + [
SubsectionNode(
children=[Node.from_obj(cls) for cls in mro],
Expand All @@ -379,6 +383,40 @@ def children(self) -> list[Node]:
]


_PROTO_FIELD_NAMES = frozenset({
'DESCRIPTOR',
'Extensions',
})


class ProtoNode(ObjectNode[message.Message]):
"""Proto."""

MATCH_TYPES = message.Message

@property
def children(self) -> list[Node]:
# Filter the proto-specific attributes to hide them in a separate section
val_attrs = []
proto_attrs = []
other_attrs = []
for c in super().children:
if isinstance(c, SubsectionNode):
other_attrs.append(c)
elif not isinstance(c, ObjectNode):
raise ValueError(f'Unexpected child {c!r}')
elif c.name in _PROTO_FIELD_NAMES:
proto_attrs.append(c)
else:
val_attrs.append(c)

return (
val_attrs
+ other_attrs
+ [SubsectionNode(children=proto_attrs, name='Proto')]
)


@dataclasses.dataclass
class ExceptionNode(ObjectNode[attrs.ExceptionWrapper]):
"""Exception."""
Expand Down
2 changes: 0 additions & 2 deletions etils/ecolab/inspects/nodes_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

"""Tests."""

from __future__ import annotations

import datetime
Expand Down
Loading