Skip to content

Add an optional reference date for reproducible tests - #513

Open
ryanduguid wants to merge 2 commits into
arthurdejong:masterfrom
ryanduguid:fix/optional-test-reference-date
Open

ryanduguid wants to merge 2 commits into
arthurdejong:masterfrom
ryanduguid:fix/optional-test-reference-date

Conversation

@ryanduguid

@ryanduguid ryanduguid commented Sep 21, 2026 •

Copy link
Copy Markdown

Tests involving birth dates and century selection can change behaviour as the calendar advances. This adds STDNUM_TEST_DATE=YYYY-MM-DD for downstream builds that need a reproducible reference date, while leaving development and scheduled tests on the real date.

The pytest fixture validates the value before collection, temporarily replaces datetime.date.today() and restores the original class after the session. Ordinary date construction and isinstance checks remain compatible. Tox passes the variable through, and CONTRIBUTING explains how to choose a date appropriate for the release fixtures. No library validation code changes or runtime dependencies are added.

Eight subprocess regression cases cover unset, explicit and invalid dates, date behaviour and restoration. The child suites do not inherit the parent's coverage file, avoiding incompatible coverage data with pytest-cov 5. The mypy environment installs pytest<9 so the new test imports remain compatible with its Python 3.9 target.

Fixes #431.

Verification:

  • Upstream Linux CI passed at commit 6dbfc3f: CPython 3.8 through 3.14, PyPy 3.9, docs, flake8, mypy, headers and CodeQL. Run.
  • Local Python 3.8 and 3.12 suites: 421 passed, 9 skipped, 100% library coverage. Python 3.14 also passed before the subprocess coverage isolation adjustment.
  • Full Python 3.12 suite with STDNUM_TEST_DATE=2026-09-21: 421 passed, 9 skipped, 100% library coverage.
  • Local tox flake8, mypy and docs passed. Windows runs pass UTF-8 through tox. The native Windows header checker encounters path separator errors in unchanged update scripts; the upstream Linux header check passes.

Online tests were not run locally. The reference date must suit the release fixtures: a full run at 2024-01-01 fails a newer Romanian ONRC example, so the documentation uses 2026-09-21.

@ryanduguid

Copy link
Copy Markdown
Author

One design note, since the diff does not show the reasoning.

The reference-date fixture is hand-rolled rather than built on freezegun or time-machine because python-stdnum has install_requires=[] and the tox test environment installs only pytest and pytest-cov. Adding a test dependency to freeze a single date looked like the larger change, so the fixture subclasses date and overrides today(), with an __instancecheck__ metaclass so existing isinstance checks still hold.

If you would rather take the dependency, this collapses to a few lines and I am happy to rewrite it that way.

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.

test failures after 2038

1 participant