pytest-regressions icon indicating copy to clipboard operation
pytest-regressions copied to clipboard

Fix type annotation to allow int keys in data_regression.check()

Open tsvikas opened this issue 2 months ago • 2 comments

Summary

The DataRegressionFixture.check() method's type annotation was overly restrictive, only allowing dict[str, Any] when the documentation states it accepts "any yaml serializable dict". This PR updates the type annotation to dict[str | int, Any] to allow integer keys.

Motivation

Integer keys are commonly used in test data (e.g., for indexing test cases by number), and YAML fully supports them. Users currently have to convert int keys to strings unnecessarily to satisfy type checkers, even though the code works correctly at runtime.

Changes

  • Updated type annotation in src/pytest_regressions/data_regression.py:39 from dict[str, Any] to dict[str | int, Any]
  • Added test_integer_keys() test case demonstrating integer key support

Type Choice Rationale

We chose str | int rather than broader types because:

  • int: Commonly used for test case indexing and numbering
  • str: Already supported
  • Not float: Floating-point keys are problematic for dictionary lookups due to precision issues
  • Not bool: Can cause confusion in YAML between true and "true"

Testing

The new test case test_integer_keys() verifies that integer keys work correctly with the data regression fixture.

Real-World Use Case

This fix enables code like:

# Previously required str(n) to satisfy type checker
data = {
    n: test_function(n)  # Now works with type checking!
    for n in numbers
}
data_regression.check(data)

tsvikas avatar Dec 18 '25 11:12 tsvikas