Skip to content

STYLE: Remove Python 2 leftovers from the query processor and shell - #139

Open
demianw wants to merge 1 commit into
masterfrom
remove-python2-leftovers
Open

demianw wants to merge 1 commit into
masterfrom
remove-python2-leftovers

Conversation

@demianw

@demianw demianw commented Nov 4, 2026

Copy link
Copy Markdown
Owner

What was wrong

These are the Python 2 leftovers listed in #115:

  • query_processor.py visit_Call checks the starargs / kwargs attributes of ast.Call. They were removed in Python 3.5. f(*x) now shows up as an ast.Starred in args, and f(**x) as a keyword in keywords, which the same condition already checks (len(node.keywords) == 0, and a Starred argument is rejected by generic_visit).
  • query_processor.py EvaluateQueries.__metaclass__ = DocStringInheritor: Python 3 ignores the attribute, so it has no effect.
  • tract_math/decorator.py: hasattr(writer, 'writeheader') is always true on Python 3.
  • shell.py: except SyntaxError as e: print(e.value) (in do_save and default) would raise AttributeError, because SyntaxError has no value. safe_method then reports it as "Uncaught exception, please contact the development team". In practice queries_preprocess converts syntax errors to TractQuerierSyntaxError, so this handler is a fallback, but it should not crash.
  • shell.py names() (used by the shell's dir command and tab completion): this is not a Python 2 issue but a leftover of the old query naming. Before 692ffd0, side queries were stored as x_left / x_right, and names() showed them as x.left / x.right. Now they are stored as x.left / x.right, so this branch never matches them. It only mangled user queries whose names end in _left: c_left = 1; c_right = 2 was listed as c.left, c.right, which do not exist, and c_right was hidden.

Change

  • query_processor.py: drop the two starargs/kwargs lines, the __metaclass__ line, the from .code_util import DocStringInheritor import, and the commented-out # @add_metaclass(DocStringInheritor).
  • tract_math/decorator.py: call writer.writeheader() unconditionally.
  • shell.py: print(e) for SyntaxError. names() returns the evaluated query names unchanged.

No behaviour change apart from the two shell fixes: no crash on a SyntaxError, and dir / tab completion list the real names of queries ending in _left / _right. For dotted side queries (x.left, everything in the packaged .qry files) names() returns exactly what it returned before.

Testing

  • New tract_querier/tests/test_shell.py:

    • names() on a shell with a.left, a.right, b, c_left, c_right;
    • a SyntaxError from queries_preprocess (monkeypatched) is printed by default and do_save without "Uncaught exception".

    Both tests fail with master's shell.py.

  • Called process_output with a dict of lists and with a dict of scalars: the CSV/TSV header and rows are printed as before.

  • python -m pytest -q -p no:cacheprovider tract_querier: 44 passed, 1 skipped.

Merge order:

Closes #115

🤖 Generated with Claude Code

- query_processor.py: drop the `starargs`/`kwargs` checks on `ast.Call`
  (removed in Python 3.5; `*args`/`**kwargs` show up in `args` and
  `keywords`, which are already checked), and the `__metaclass__`
  attribute (ignored by Python 3) with its `code_util` import and the
  commented-out `add_metaclass` decorator.
- tract_math/decorator.py: `csv.DictWriter.writeheader` always exists.
- shell.py: `except SyntaxError as e: print(e.value)` raised
  AttributeError, because SyntaxError has no `value`; print the exception.
  `names()` translated `x_left` keys to `x.left`/`x.right` and hid `x_right`,
  from when side queries were stored as `x_left`. They are now stored as
  `x.left`, so the translation only mangled user queries whose names end in
  `_left`/`_right`. Return the query names unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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.

ENH: remove Python 2 leftovers

1 participant