Skip to content

Fix AttributeError on unnamed random variables in numpy make_log_joint_fn - #606

Draft
rootkiller6788 wants to merge 1 commit into
google:mainfrom
rootkiller6788:fix-numpy-make-log-joint-im-class
Draft

Fix AttributeError on unnamed random variables in numpy make_log_joint_fn#606
rootkiller6788 wants to merge 1 commit into
google:mainfrom
rootkiller6788:fix-numpy-make-log-joint-im-class

Conversation

@rootkiller6788

Copy link
Copy Markdown

Summary

edward2.numpy.make_log_joint_fn has a Python 2 leftover in its tracer's error path. When a random variable is called without a name keyword argument (and there are no remaining positional arguments to consume), the tracer intends to raise a helpful KeyError, but instead references rv_call.im_class.__name__. im_class was removed in Python 3, so this raises:

AttributeError: 'function' object has no attribute 'im_class'

which masks the real problem (an unnamed random variable).

Change

Use rv_call.__self__.__class__.__name__ instead, which is the same expression the tracer already uses a few lines below to obtain the distribution class. The error message is now:

KeyError: 'Random variable call norm_gen has no name in its arguments.'

Test

Added testMakeLogJointUnnamedRandomVariable to edward2/numpy/program_transformations_test.py, which asserts the KeyError (with the expected message) is raised rather than an AttributeError.

Verified locally with Python 3.14 / NumPy 2.4 / SciPy 1.18: python -m pytest edward2/numpy/ -q passes.

…t_fn

The tracer in edward2.numpy.make_log_joint_fn referenced the Python 2
im_class attribute on a bound method when raising a KeyError for a
random variable call missing a name. On Python 3 this raises an
unhelpful AttributeError instead of the intended KeyError. Use
__self__.__class__ (the same pattern already used later in the tracer)
to report the distribution class name, and add a regression test.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant