Add support for arithmetic operators in number interpolations (fixes #91)#1229
Open
suneel944 wants to merge 12 commits into
Open
Add support for arithmetic operators in number interpolations (fixes #91)#1229suneel944 wants to merge 12 commits into
suneel944 wants to merge 12 commits into
Conversation
…mry#91) - Implement arithmetic operations (+, -, *, /) for numeric interpolations - Return int when both operands are int, float otherwise - Fall back to string concatenation for non-numeric operands - Add comprehensive tests for all operations and edge cases - Optimize implementation for performance with single-pass processing
When one operand is not numeric, arithmetic detection fails and falls back to string concatenation, which includes the operator text.
- Multiple operations in sequence - Operations without whitespace - Negative numbers and results - Zero values - Division by zero - Large and small numbers - Float precision - Chained operations - Missing values - None values - Non-numeric fallbacks (lists, dicts, booleans) - String prefixes/suffixes - Nested interpolations - Mixed operation types
Author
|
@omry can I get a review on this? |
…s returns float - Exclude boolean values from arithmetic (bool is subclass of int in Python) - Division always returns float even when result is whole number - Fixes test failures for boolean fallback and division type checking
- Test single interpolation without operator - Test operator followed by text (non-interpolation) - Test no operator between interpolations - Test invalid operator characters - Test operator without second operand
- Add type annotation for operator_map - Use intermediate variable for type narrowing before visitInterpolation calls - Extract operator function to variable for type checking
- Optimize _try_arithmetic_expression for efficiency with single-pass processing - Add comprehensive test coverage for arithmetic operations - Mark unreachable defensive code paths with pragma no cover comments - Ensure 100% code coverage for grammar_visitor.py
- Use isinstance() checks before comparisons to help mypy understand types - Use bracket notation for dict access instead of attribute access - Remove type: ignore comments in favor of proper type handling
Author
|
@rsokl could you give me a review here please |
Owner
@suneel944, yes. I started to catch up on some issues here. |
Owner
|
@suneel944, for now I will defer this to after 2.4.0 lands. this is too big of an enhancement to include at this stage. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR implements support for basic arithmetic operators (+, -, *, /) in number interpolations as requested in issue #91.
Changes
grammar_visitor.pyExample Usage
Testing
All tests pass, including: