Properly handle missing attributes in query/eval strings by alexmojaki · Pull Request #32408 · pandas-dev/pandas (original) (raw)

Consider this script:

import pandas as pd

pd.eval("pd.thing")

Currently it raises an error like this:

  File "/home/alex/work/pandas/pandas/core/computation/expr.py", line 640, in visit_Attribute
    raise ValueError(f"Invalid Attribute context {ctx.__name__}")
AttributeError: 'Load' object has no attribute '__name__'

Adding __class__ to that line changes the error to the more sensible:

ValueError: Invalid Attribute context Load

Re-raising the original error gives what's really needed:

  File "/home/alex/work/pandas/pandas/core/computation/expr.py", line 631, in visit_Attribute
    v = getattr(resolved, attr)
  File "/home/alex/work/pandas/pandas/__init__.py", line 260, in __getattr__
    raise AttributeError(f"module 'pandas' has no attribute '{name}'")
AttributeError: module 'pandas' has no attribute 'thing'