Skip to content

Conversation

@tsvikas
Copy link
Contributor

@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 <noreply@anthropic.com>
@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
Contributor Author

tsvikas commented Dec 18, 2025

Done :)

@tsvikas
Copy link
Contributor Author

tsvikas commented Jan 8, 2026

Hi @nicoddemus , just a gentle nudge on this. I’ve updated the type annotations to Union for Python 3.9 compatibility as requested. Let me know if there’s anything else needed to get the CI workflows running!

@nicoddemus
Copy link
Member

Thanks for the nudge, this went under my radar!

def check(
self,
data_dict: dict[str, Any],
data_dict: dict[Union[str, int], Any],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we might as well just use

Suggested change
data_dict: dict[Union[str, int], Any],
data_dict: dict[Any, Any],

here?

We don't have a type for "anything yaml serializable", so might as well use Any and hope for the best.

If you agree, we can also just remove the regression test, seems overkill to have a test for a specific data type.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's up to you, ultimately. My logic: if we look at allowed YAML keys (bool, float, str, int, null), then str | int is the reasonable choice because:

  • str and int are practical and commonly used
  • bool can cause confusion (true vs "true", yes vs "yes")
  • float has precision issues for dict lookups
  • null as a key is weird, and don't speaks well with the Python world (None vs float('na'))

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good points, lets go with your suggestion then!

@tsvikas
Copy link
Contributor Author

tsvikas commented Jan 8, 2026

The CI failure is unrelated to this PR - it's a matplotlib/pyparsing deprecation warning that only appears on Python 3.9. The test is importing matplotlib which uses pyparsing's deprecated oneOf (should be one_of). Since Python 3.9 reached EOL in October 2025, matplotlib/pyparsing likely won't fix it for py39.

I suggest that we will approve this PR, and write a separate PR that drop py39 support (remove from tox.ini envlist). alternatively, we can add a filterwarning to ignore this specific deprecation

@nicoddemus
Copy link
Member

I suggest that we will approve this PR, and write a separate PR that drop py39 support

Agreed, I will go ahead and merge this.

@nicoddemus nicoddemus merged commit ac67f98 into ESSS:master Jan 8, 2026
11 of 13 checks passed
@tsvikas tsvikas deleted the fix/data-regression-dict-key-types branch January 8, 2026 14:11
nicoddemus added a commit that referenced this pull request Jan 9, 2026
Unfortunately the annotation added in #219 caused many regressions.

While at it, added type annotations to all tests to catch this kind of regression in the future.
nicoddemus added a commit that referenced this pull request Jan 9, 2026
Unfortunately the annotation added in #219 caused many regressions.

While at it, added type annotations to all tests to catch this kind of regression in the future.
nicoddemus added a commit that referenced this pull request Jan 9, 2026
Unfortunately the annotation added in #219 caused many regressions.

While at it, added type annotations to all tests to catch this kind of regression in the future.
nicoddemus added a commit that referenced this pull request Jan 9, 2026
Unfortunately the annotation added in #219 caused many regressions.

While at it, added type annotations to all tests to catch this kind of regression in the future.
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