Skip to content

Commit 6f8b686

Browse files
committed
make new str/latex code more robust to type juggling
1 parent 475d24a commit 6f8b686

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

pymc3/printing.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,27 @@
2323

2424
class PrettyPrintingTensorVariable(TensorVariable):
2525
def _str_repr(self, formatting="plain"):
26-
print_name = self.name if self.name is not None else "<unnamed>"
27-
dist_name = self.owner.op._print_name[0]
28-
29-
if "latex" in formatting:
30-
print_name = r"\text{" + _latex_escape(print_name) + "}"
31-
dist_name = self.owner.op._print_name[1]
32-
33-
# first 3 args are always (rng, size, dtype), rest is relevant for distribution
34-
dist_args = [_str_for_input_var(x, formatting=formatting) for x in self.owner.inputs[3:]]
35-
36-
if "latex" in formatting:
37-
return r"${} \sim {}({})$".format(print_name, dist_name, ",~".join(dist_args))
38-
else:
39-
return r"{} ~ {}({})".format(print_name, dist_name, ", ".join(dist_args))
26+
try:
27+
print_name = self.name if self.name is not None else "<unnamed>"
28+
dist_name = self.owner.op._print_name[0]
29+
30+
if "latex" in formatting:
31+
print_name = r"\text{" + _latex_escape(print_name) + "}"
32+
dist_name = self.owner.op._print_name[1]
33+
34+
# first 3 args are always (rng, size, dtype), rest is relevant for distribution
35+
dist_args = [
36+
_str_for_input_var(x, formatting=formatting) for x in self.owner.inputs[3:]
37+
]
38+
39+
if "latex" in formatting:
40+
return r"${} \sim {}({})$".format(print_name, dist_name, ",~".join(dist_args))
41+
else:
42+
return r"{} ~ {}({})".format(print_name, dist_name, ", ".join(dist_args))
43+
except:
44+
# we wrap the above in a try-block because Aesara can do some type juggling
45+
# during e.g. computation of test values which makes self.owner is None
46+
return super().__str__()
4047

4148
def __latex__(self, formatting="latex", **kwargs):
4249
return self._str_repr(formatting=formatting)

0 commit comments

Comments
 (0)