Skip to content

Commit 4db3503

Browse files
refactor: code cleanup and improvements (#63)
* test: use bash from PATH for portability * refactor: rename ContextVar from var_ prefix to ctx_ prefix 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> * refactor: use dict mapping for comparison operators in transformer 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> * refactor: improve CLI error handling with proper exception chaining - 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> * refactor: simplify Slice.find default value assignments - 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> * refactor: reduce Compare subclass boilerplate with _symbol and _operator - 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> * refactor: replace assert with proper TypeError exceptions - 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> * refactor: final cleanup of assert statements and if-elif chains - 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> * refactor: minor cleanup for naming consistency - 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> * ci: add Python 3.14 support and update actions/cache to v4 * ci: migrate from PDM to Nix-based workflow and add Python 3.14 support - Replace PDM setup with custom Nix GitHub Action for CI - Add new Nix build check job to verify flake builds - Update GitHub Actions: checkout@v6, actions/cache@v5, codecov-action@v5 - Add Nix store and Cargo dependency caching - Switch from pre-commit to prek for linting - Update flake inputs to use nixpkgs-unstable directly - Add Python 3.14 to test matrix (3.10-3.14) - Update dependency versions (babel, charset-normalizer, coverage, etc.) - Add make init step before running tests * ci: update actions/cache to v4 --------- Co-authored-by: Amp <amp@ampcode.com>
1 parent c2cbdac commit 4db3503

File tree

16 files changed

+1361
-997
lines changed

16 files changed

+1361
-997
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: "Setup Nix Environment"
2+
description: ""
3+
runs:
4+
using: composite
5+
steps:
6+
- uses: nixbuild/nix-quick-install-action@v33
7+
with:
8+
nix_conf: |
9+
keep-env-derivations = true
10+
keep-outputs = true
11+
- name: Restore and save Nix store
12+
uses: nix-community/cache-nix-action@v6
13+
with:
14+
primary-key: nix-${{ runner.os }}-${{ hashFiles('**/*.nix', '**/flake.lock') }}
15+
restore-prefixes-first-match: nix-${{ runner.os }}-
16+
gc-max-store-size-linux: 15G
17+
18+
- name: Cache Cargo dependencies
19+
uses: actions/cache@v4
20+
with:
21+
path: |
22+
~/.cargo/registry
23+
~/.cargo/git
24+
target
25+
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
26+
restore-keys: |
27+
${{ runner.os }}-cargo-
28+
29+
- name: Setup nix develop env
30+
shell: bash
31+
run: |
32+
echo 'use flake' >.envrc
33+
- uses: HatsuneMiku3939/direnv-action@v1
34+
# and avoid develop env being gc

.github/workflows/test.yml

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,39 +13,44 @@ jobs:
1313
lint:
1414
runs-on: ubuntu-latest
1515
steps:
16-
- uses: actions/checkout@v1
17-
- name: Set up PDM
18-
uses: pdm-project/setup-pdm@v3
16+
- uses: actions/checkout@v6
17+
- uses: ./.github/actions/setup-nix
18+
- name: Cache pre-commit hooks
19+
uses: actions/cache@v5
1920
with:
20-
python-version: 3.13
21-
version: 2.19.2
22-
- name: Cache Nox Virtualenvs
23-
uses: actions/cache@v1
24-
with:
25-
path: .nox
26-
key: ${{ runner.os }}-nox-${{ hashFiles('**/pdm.lock') }}
27-
restore-keys: ${{ runner.os }}-nox
28-
- name: Install nox
29-
run: |
30-
pip install nox
31-
pdm config python.use_venv true
32-
- uses: pre-commit/action@v2.0.0
33-
env:
34-
SKIP: export_requirements_txt
21+
path: ~/.cache/prek
22+
key: prek-v1|${{ runner.os }}|${{ runner.arch }}|${{ hashFiles('**/.pre-commit-config.yaml') }}
23+
24+
- name: Run pre commit hooks on all files
25+
run: prek run --all-files
26+
27+
nix-build-check:
28+
needs:
29+
- lint
30+
runs-on: ubuntu-latest
31+
steps:
32+
- uses: actions/checkout@v6
33+
- uses: ./.github/actions/setup-nix
34+
35+
- name: Build nix package
36+
run: nix build --profile default
37+
# use --profile default to avoid dependencies being gc
38+
39+
- name: Run nix flake check
40+
run: nix flake check --all-systems
41+
3542
test:
3643
needs: lint
3744
runs-on: ubuntu-latest
3845
strategy:
3946
matrix:
40-
python-version: ["3.10", "3.11", "3.12", "3.13"]
47+
python-version: ["3.10", "3.11", "3.12", "3.13", "3.14"]
4148
steps:
42-
- uses: actions/checkout@v1
43-
- name: Set up PDM
44-
uses: pdm-project/setup-pdm@v3
45-
with:
46-
python-version: ${{ matrix.python-version }}
49+
- uses: actions/checkout@v6
50+
- uses: ./.github/actions/setup-nix
51+
4752
- name: Cache Nox Virtualenvs
48-
uses: actions/cache@v1
53+
uses: actions/cache@v4
4954
with:
5055
path: .nox
5156
key: ${{ runner.os }}-${{ matrix.python-version }}-nox-${{ hashFiles('**/pdm.lock') }}
@@ -56,9 +61,10 @@ jobs:
5661
pdm config python.use_venv true
5762
- name: Test with coverage
5863
run: |
64+
make PYTHON=${{ matrix.python-version }} init
5965
make PYTHON=${{ matrix.python-version }} cov
6066
- name: Upload coverage to Codecov
61-
uses: codecov/codecov-action@v2
67+
uses: codecov/codecov-action@v5
6268
with:
6369
token: ${{ secrets.CODECOV_TOKEN }}
6470
flags: main,unittest,${{ matrix.python-version }}

flake.lock

Lines changed: 34 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
inputs = {
33
dream2nix.url = "github:nix-community/dream2nix";
4-
nixpkgs.follows = "dream2nix/nixpkgs";
4+
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
55
};
66

77
outputs = {
@@ -40,13 +40,14 @@
4040
inputsFrom = [self.packages.${system}.default.devShell];
4141

4242
packages = with pkgs; [
43-
pre-commit
43+
prek
4444
python3Packages.nox
4545

4646
python310
4747
python311
4848
python312
4949
python313
50+
python314
5051
];
5152
};
5253
});

jsonpath/cli.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,22 @@ def cli(args: argparse.Namespace) -> None:
1414
try:
1515
jp = parse(args.expression)
1616
except JSONPathError as exc:
17-
sys.exit(str(exc))
17+
raise SystemExit(str(exc)) from exc
1818

1919
if args.file:
2020
file_path = Path(args.file)
21-
with file_path.open() as f:
22-
data = json.load(f)
21+
try:
22+
with file_path.open() as f:
23+
data = json.load(f)
24+
except (OSError, json.JSONDecodeError) as exc:
25+
raise SystemExit(f"Error reading {file_path}: {exc}") from exc
2326
elif not sys.stdin.isatty():
24-
data = json.load(sys.stdin)
27+
try:
28+
data = json.load(sys.stdin)
29+
except json.JSONDecodeError as exc:
30+
raise SystemExit(f"Invalid JSON from stdin: {exc}") from exc
2531
else:
26-
sys.exit("JSON file is needed.")
32+
raise SystemExit("JSON file is needed.")
2733

2834
json.dump(jp.find(data), sys.stdout, indent=2, ensure_ascii=args.ensure_ascii)
2935
sys.stdout.write("\n")

0 commit comments

Comments
 (0)