Skip to content

Improve string representation of Assert Ops #739

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

Closed
Closed
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
14 changes: 12 additions & 2 deletions pytensor/raise_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,13 @@ def __init__(self, exc_type, msg=""):
self.msg = msg

def __str__(self):
return f"CheckAndRaise{{{self.exc_type}({self.msg})}}"
name = self.__class__.__name__
Copy link
Member

Choose a reason for hiding this comment

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

Will the name variable not always be instance of CheckAndRaise in itself here? If so what is the benefit of having a separate variable for it?

Copy link
Member Author

Choose a reason for hiding this comment

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

If there is a subclass it will use the subclass name instead

exc_name = self.exc_type.__name__
if len(self.msg) > 30:
msg = self.msg[:27] + "..."
else:
msg = self.msg
Comment on lines +53 to +56
Copy link
Member

Choose a reason for hiding this comment

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

I am a bit conflicted here. Why do we want to contain the error message? I understand that it makes the code more readable but it would also hide some important information regarding the message. Although the length of 27 again seems sufficient when displaying the error message.

Copy link
Member Author

Choose a reason for hiding this comment

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

This is just for the text representation (like when doing pytensor.dprint). If the assert fails during execution, the whole message will be print out.

Right now showing the whole message makes graph printing pretty ugly. That's the whole point of this PR

Copy link
Member

Choose a reason for hiding this comment

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

Ohh I see. LGTM 👍

return f"{name}{{raises={exc_name}, msg='{msg}'}}"

def __eq__(self, other):
if type(self) != type(other):
Expand Down Expand Up @@ -195,7 +201,11 @@ def __init__(self, msg="PyTensor Assert failed!"):
super().__init__(AssertionError, msg)

def __str__(self):
return f"Assert{{msg={self.msg}}}"
if len(self.msg) > 30:
msg = self.msg[:27] + "..."
else:
msg = self.msg
return f"Assert{{msg='{msg}'}}"


assert_op = Assert()
Expand Down