Skip to content
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

[pyobj-] catch AttributeError viewing columns list #2631

Merged
merged 3 commits into from
Jan 12, 2025

Conversation

midichef
Copy link
Contributor

@midichef midichef commented Dec 7, 2024

Viewing a sheet's columns with ^X sheet.columns gives an error trying to read formatted_help that causes all the attributes to fail to be displayed.

Traceback (most recent call last):
  File "/home/midichef/.venv/lib/python3.12/site-packages/visidata/pyobj.py", line 56, in PyobjColumns
    return [ColumnAttr(k, type=deduceType(getattr(obj, k))) for k in getPublicAttrs(obj)]
  File "/home/midichef/.venv/lib/python3.12/site-packages/visidata/pyobj.py", line 52, in getPublicAttrs
    return [k for k in dir(obj) if not k.startswith('_') and not callable(getattr(obj, k))]
  File "/home/midichef/.venv/lib/python3.12/site-packages/visidata/column.py", line 189, in formatted_help
    return MissingAttrFormatter().format(self.help, sheet=self.sheet, col=self, vd=vd)
AttributeError: 'ItemColumn' object has no attribute 'help'

A simpler way to solve this problem would be to replace self.help with getattr(self, 'help', ''), but that would break the guide formatting behavior. formatted_help was designed to be accessed when handling a format string for a guide: "{sheet.cursorCol.formatted_help}". In that context, MissingAttrFormatter() looks for AttributeError to know when to fall back to displaying the format string unaltered:

visidata/visidata/utils.py

Lines 195 to 199 in 37f0a0c

def get_field(self, field_name, args, kwargs):
try:
return super().get_field(field_name, args, kwargs)
except (KeyError, AttributeError, IndexError, ValueError) as e:
return ('{' + field_name + '}', field_name)

So this PR changes the way PyobjColumns accesses attributes, to be compatible with the MissingAttrFormatter design.

@midichef midichef changed the title [column-] catch AttributeError viewing columns list as pyobj [pyobj-] catch AttributeError viewing columns list as pyobj Dec 7, 2024
@midichef midichef changed the title [pyobj-] catch AttributeError viewing columns list as pyobj [pyobj-] catch AttributeError viewing columns list Dec 7, 2024
@saulpw
Copy link
Owner

saulpw commented Jan 12, 2025

Thanks for noticing this, it's definitely broken as it is currently. Interestingly, in the case that the attribute throws an exception, I would want to have that attribute in the list, so I can see the exception on the pyobj sheet. I'll make the change here.

@anjakefala anjakefala merged commit 3d6b820 into saulpw:develop Jan 12, 2025
14 checks passed
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.

3 participants