Skip to content

Add support for arithmetic operators in number interpolations (fixes #91)#1229

Open
suneel944 wants to merge 12 commits into
omry:mainfrom
suneel944:feature/issue-91-arithmetic-operators
Open

Add support for arithmetic operators in number interpolations (fixes #91)#1229
suneel944 wants to merge 12 commits into
omry:mainfrom
suneel944:feature/issue-91-arithmetic-operators

Conversation

@suneel944
Copy link
Copy Markdown

This PR implements support for basic arithmetic operators (+, -, *, /) in number interpolations as requested in issue #91.

Changes

  • Implement arithmetic operations for numeric interpolations in grammar_visitor.py
  • Return int when both operands are int, float otherwise
  • Fall back to string concatenation for non-numeric operands (backward compatible)
  • Add comprehensive tests for all operations and edge cases
  • Optimize implementation for performance with single-pass processing

Example Usage

cfg = OmegaConf.create({'a': 1, 'b': 2.0})
cfg.d = '${a} + ${b}'  # Results in 3.0 (float)
cfg.e = '${a} - ${b}'  # Results in -1 (int if both int)

Testing

All tests pass, including:

  • Addition, subtraction, multiplication, division
  • Type checking (int vs float results)
  • Whitespace handling
  • Fallback to string concatenation for non-numeric operands

…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
@suneel944
Copy link
Copy Markdown
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
@suneel944
Copy link
Copy Markdown
Author

@rsokl could you give me a review here please

@omry
Copy link
Copy Markdown
Owner

omry commented Apr 21, 2026

@omry can I get a review on this?

@suneel944, yes. I started to catch up on some issues here.
I will take some time though.
Thanks for your work on this.

@omry
Copy link
Copy Markdown
Owner

omry commented May 10, 2026

@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.

@omry omry added this to the OmegaConf 3.0 milestone May 10, 2026
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.

2 participants