Fix type annotation to allow int keys in data_regression.check()
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:39fromdict[str, Any]todict[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
trueand"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)