Fix AttributeError on unnamed random variables in numpy make_log_joint_fn - #606
Draft
rootkiller6788 wants to merge 1 commit into
Draft
Fix AttributeError on unnamed random variables in numpy make_log_joint_fn#606rootkiller6788 wants to merge 1 commit into
rootkiller6788 wants to merge 1 commit into
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
edward2.numpy.make_log_joint_fnhas a Python 2 leftover in its tracer's error path. When a random variable is called without anamekeyword argument (and there are no remaining positional arguments to consume), the tracer intends to raise a helpfulKeyError, but instead referencesrv_call.im_class.__name__.im_classwas removed in Python 3, so this raises: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:Test
Added
testMakeLogJointUnnamedRandomVariabletoedward2/numpy/program_transformations_test.py, which asserts theKeyError(with the expected message) is raised rather than anAttributeError.Verified locally with Python 3.14 / NumPy 2.4 / SciPy 1.18:
python -m pytest edward2/numpy/ -qpasses.