Skip to content

refactor: code cleanup and improvements#63

Open
linw1995 wants to merge 9 commits intomasterfrom
refactor/follow-piglet-skill
Open

refactor: code cleanup and improvements#63
linw1995 wants to merge 9 commits intomasterfrom
refactor/follow-piglet-skill

Conversation

@linw1995
Copy link
Owner

@linw1995 linw1995 commented Feb 5, 2026

Summary

This PR includes several refactoring improvements to enhance code quality and maintainability:

Main Changes

  • Exception handling: Replace assert statements with proper TypeError/RuntimeError exceptions
  • Naming consistency: Rename ContextVar prefixes from var_ to ctx_ for better clarity
  • CLI improvements: Better error handling with proper exception chaining in CLI module
  • Transformer simplification: Use dict mappings for comparison operators and functions instead of if-elif chains
  • Slice.find: Simplify default value assignments (start=0, step=1, end=len)
  • Compare class: Reduce boilerplate by leveraging class attributes and operator module

Files Changed

  • jsonpath/cli.py: Enhanced error handling with exception chaining
  • jsonpath/core.py: Major refactoring of exception handling and ContextVar naming
  • jsonpath/transformer.py: Simplified operator/function lookup with dict mappings
  • tests/: Updated tests for bash portability and related changes

All changes are backward compatible and improve code readability and error messages.

linw1995 and others added 9 commits February 5, 2026 23:12
Improve naming clarity for module-level ContextVar variables:
- var_root -> ctx_root
- var_parent -> ctx_parent
- var_self -> ctx_self
- var_finding -> ctx_finding

Amp-Thread-ID: https://ampcode.com/threads/T-019c2e4e-7deb-7076-8168-36fbd8141afa
Co-authored-by: Amp <amp@ampcode.com>
Replace if-elif chain with COMPARISON_OPERATORS dict for better
readability and extensibility.

Amp-Thread-ID: https://ampcode.com/threads/T-019c2e4e-7deb-7076-8168-36fbd8141afa
Co-authored-by: Amp <amp@ampcode.com>
- Use 'raise SystemExit() from exc' to preserve exception chain
- Add try-except for file reading and JSON parsing errors
- Provide more descriptive error messages

Amp-Thread-ID: https://ampcode.com/threads/T-019c2e4e-7deb-7076-8168-36fbd8141afa
Co-authored-by: Amp <amp@ampcode.com>
- Use 'or' operator to reduce redundant if statements
- Replace assert with proper JSONPathFindError exception

Amp-Thread-ID: https://ampcode.com/threads/T-019c2e4e-7deb-7076-8168-36fbd8141afa
Co-authored-by: Amp <amp@ampcode.com>
- Add _symbol and _operator class attributes to Compare base class
- Move common _get_partial_expression and find to base class
- Subclasses now only define _symbol and _operator attributes
- And/Or retain custom find for short-circuit evaluation semantics

Amp-Thread-ID: https://ampcode.com/threads/T-019c2e4e-7deb-7076-8168-36fbd8141afa
Co-authored-by: Amp <amp@ampcode.com>
- Array.__init__: use TypeError for invalid idx parameter
- Predicate.__init__: use TypeError for invalid expr parameter
- Brace.__init__: use TypeError for invalid expr parameter
- Search.__init__: use TypeError for invalid expr parameter
- Key.__init__: use TypeError for unexpected arguments
- Contains.__init__: use TypeError for invalid parameters
- Not.__init__: use TypeError for invalid parameters

This ensures type checks work even with Python -O optimization.

Amp-Thread-ID: https://ampcode.com/threads/T-019c2e4e-7deb-7076-8168-36fbd8141afa
Co-authored-by: Amp <amp@ampcode.com>
- get_begin: replace assert with RuntimeError
- Array.find: simplify control flow, remove unreachable AssertionError
- predicate: merge isinstance checks to remove assert
- func_call: use FUNCTIONS dict mapping instead of if-elif chain

Amp-Thread-ID: https://ampcode.com/threads/T-019c2e4e-7deb-7076-8168-36fbd8141afa
Co-authored-by: Amp <amp@ampcode.com>
- Slice: rename self.end to self.stop to match parameter name
- comparison_expr: use ValueError instead of AssertionError
- doctest: update var_boo to ctx_boo for naming consistency

Amp-Thread-ID: https://ampcode.com/threads/T-019c2e4e-7deb-7076-8168-36fbd8141afa
Co-authored-by: Amp <amp@ampcode.com>
@linw1995 linw1995 added the enhancement New feature or request label Feb 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant