Skip to content

Fix pytest-randomly seed overflow in numpy legacy API#732

Open
I-am-Uchenna wants to merge 2 commits into
PyPortfolio:mainfrom
I-am-Uchenna:fix-seed-overflow
Open

Fix pytest-randomly seed overflow in numpy legacy API#732
I-am-Uchenna wants to merge 2 commits into
PyPortfolio:mainfrom
I-am-Uchenna:fix-seed-overflow

Conversation

@I-am-Uchenna
Copy link
Copy Markdown

Closes #725

Problem

When pytest-randomly (older versions) generates seeds that exceed 2**32 - 1, numpy.random.seed() raises a ValueError because its legacy API only accepts seeds in the range [0, 2**32 - 1].

ValueError: Seed must be between 0 and 2**32 - 1

This surfaces as random, hard-to-reproduce test failures depending on the seed pytest-randomly picks.

Fix

Adds a tests/conftest.py that monkey-patches numpy.random.seed at module load time to constrain any integer seed via seed % (2**32). Non-integer and None seeds pass through unchanged.

The patch is minimal and project-scoped - it only affects the test suite, not library code.

Tests

Adds tests/test_conftest.py with 7 tests covering:

  • Seeds exceeding uint32 max
    • Seed at the uint32 boundary (2**32)
    • Seed zero
    • Seed at max valid value (2**32 - 1)
    • Seed None (random)
    • Determinism preserved after patching
    • Large seed determinism (consistent wrapping)
      All tests pass. Both files are Black-formatted.

Reproduction

pip install pytest-randomly==1.2.3
pytest tests/ -p randomly --randomly-seed=5765063658
# ValueError: Seed must be between 0 and 2**32 - 1

# With the fix applied:
pytest tests/ -p randomly --randomly-seed=5765063658
# All tests pass

Wraps numpy.random.seed to handle seeds exceeding 2**32 - 1, which older pytest-randomly versions can generate. Fixes PyPortfolio#725.
7 tests covering boundary cases, None seeds, and determinism preservation for the numpy.random.seed wrapper.
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.

[BUG] ValueError issues during testing changes

1 participant