ezmsg-org / ezmsg

Pure-Python DAG-based high-performance SHM-backed pub-sub and multi-processing pattern
https://ezmsg.readthedocs.io/en/latest/
MIT License
15 stars 6 forks source link

Trying to print Stream object when not in ExecutionContext raises assertion #93

Closed cboulay closed 9 months ago

cboulay commented 9 months ago

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")
cboulay commented 9 months ago

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]