Skip to content

core: add AST-based linter for undeclared state reads in function-bas…#656

Merged
skrawcz merged 3 commits intoapache:mainfrom
Smitaambiger:feature/action-reads-linter
Mar 1, 2026
Merged

core: add AST-based linter for undeclared state reads in function-bas…#656
skrawcz merged 3 commits intoapache:mainfrom
Smitaambiger:feature/action-reads-linter

Conversation

@Smitaambiger
Copy link
Contributor

Closes #407

This PR adds AST-based validation for undeclared state reads in function-based actions.

What this does

  • Parses the originating function using inspect.getsource
  • Walks the AST to detect state["<literal>"] accesses
  • Compares detected keys with declared reads=[...]
  • Raises ValueError if undeclared keys are accessed
  • Skips validation when source is unavailable

Why

Prevents silent bugs caused by accessing state keys that were not declared in reads, enforcing stricter action correctness.

Tests

  • Added unit tests covering:
    • Valid declared reads
    • Undeclared read detection

@Smitaambiger
Copy link
Contributor Author

@elijahbenizzy

Initial implementation for AST-based undeclared state read validation.

Currently implemented as a hard ValueError at action construction time.

Happy to adjust:

  • validation timing (build-time vs decorator-time)
  • severity (warning vs error)

Looking forward to feedback.

Copy link
Contributor

@skrawcz skrawcz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good start, a few things to add.

@Smitaambiger
Copy link
Contributor Author

@skrawcz ,Thanks for the detailed feedback!

Addressed the requested changes:

  • Moved imports to top level
  • Consolidated tests into test_action.py
  • Added test for multiple undeclared keys interleaved with declared keys
  • Added regression test to ensure pydantic-based actions are not impacted
  • Cleaned up lint/formatting issues

Core tests are passing locally and CI should reflect the same.

(.venv) PS C:\Users\gouta\OneDrive\Desktop\burr> python -m pytest tests/core -q
................................................................................................................................................................................................. [ 69%]
....................................................................................                                                                                                              [100%]
277 passed in 9.10s
(.venv) PS C:\Users\gouta\OneDrive\Desktop\burr> pre-commit run --all-files
black....................................................................Passed
trim trailing whitespace.................................................Passed
fix end of files.........................................................Passed
fix requirements.txt.....................................................Passed
check python ast.........................................................Passed
isort....................................................................Passed
flake8...................................................................Passed
frontend-lint-staged.....................................................Passed

Please let me know if any further adjustments are needed.

@Smitaambiger
Copy link
Contributor Author

@skrawcz Just following up — please let me know if anything else needs adjustment here. Happy to iterate further if needed.

Copy link
Contributor

@skrawcz skrawcz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great thanks!

@skrawcz skrawcz merged commit 8a975b0 into apache:main Mar 1, 2026
10 of 11 checks passed
@Smitaambiger
Copy link
Contributor Author

@skrawcz Thanks for the review and guidance — really enjoyed working on this one!

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.

Linter for action reads in state, function-based-actions

2 participants