Skip to content

Conversation

@tsvikas
Copy link

@tsvikas tsvikas commented Dec 18, 2025

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)

The method documentation states it accepts 'any yaml serializable dict',
but the type annotation was restricted to dict[str, Any]. Since YAML
supports integer keys (which are commonly used for test case indexing),
this updates the annotation to dict[str | int, Any].

Added test_integer_keys() to demonstrate integer key support.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
@nicoddemus
Copy link
Member

Thanks @tsvikas

The Python 3.9 job failed; we will likely drop support for 3.9 soon, but probably simpler in your case to just update the type annotations for this PR.

Replace `str | int` with `Union[str, int]` in type annotation to support
Python 3.9, where the | operator for unions is not available (only in 3.10+).
@tsvikas
Copy link
Author

tsvikas commented Dec 18, 2025

Done :)

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.

3 participants