ezmsg-org/ezmsg

Trying to print Stream object when not in ExecutionContext raises assertion

Closed this issue · 1 comments

ezmsg.core.stream.Stream overrides its __repr__ which includes its self.address. self.address is a property inherited from the parent Addressable class. address is a simple construct usingself.location. If self.location is None then an AssertionError is raised upon its access.

location is None unless it has had its _set_location called, which afaict only happens during ExecutionContext.setup.

Reproduce with:

import typing

import ezmsg.core as ez
from ezmsg.util.debuglog import DebugLog

try:
    print(DebugLog.INPUT)
    # Same if print(DebugLog().INPUT)
except AssertionError as e:
    print("Cannot print Stream object")

stream_obj = ez.stream.Stream(typing.Any)
try:
    print(stream_obj)
except AssertionError as e:
    print("Cannot print Stream object")

I'm guessing that raising the AssertionError when self.location is None is intentional (it's explicitly tested in the unit tests).

So maybe this should only be fixed at the Stream.__repr__ level.

Instead of

    def __repr__(self) -> str:
        return f"Stream:{self.address}[{self.msg_type}]"

We can use

    def __repr__(self) -> str:
        _addr = self.address if self._location is not None else "unlocated"
        return f"Stream:{_addr}[{self.msg_type}]"

The above test then prints

InputStream:unlocated[typing.Any]()
Stream:unlocated[typing.Any]