feat: helper to find key in model block#5079
Conversation
benfdking
commented
Jul 31, 2025
- to be used by linter in the future
- to be used by linter in the future
There was a problem hiding this comment.
Pull Request Overview
This PR adds a helper function to find the range of a specific key within a model block in SQL files. The function get_range_of_a_key_in_model_block is intended for future use by the linter to locate and validate specific model properties.
Key changes:
- Added
get_range_of_a_key_in_model_blockfunction to find key positions in model blocks - Added comprehensive test coverage using the sushi example models
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| sqlmesh/core/linter/helpers.py | Implements the new helper function to locate keys within model blocks |
| tests/core/linter/test_helpers.py | Adds test coverage that validates the function against real model files |
| key_token = next( | ||
| ( | ||
| t | ||
| for t in tokens_of_interest | ||
| if t.token_type is TokenType.VAR and t.text.upper() == key.upper() | ||
| ), | ||
| None, | ||
| ) |
There was a problem hiding this comment.
Just a heads up that this looks good as a best-effort approach, just not sure if it guarantees you'll match the exact token. Think that the token corresponding to the property's key could appear elsewhere in the MODEL block
Probably not worth trying to further improve it, but a simple heuristic if you notice any issues would be to check if the previous token is a comma, since properties are comma-separated.