Skip to content

Fix Discrete.contains raising OverflowError for out-of-dtype ints - #1648

Merged
pseudo-rnd-thoughts merged 2 commits into
Farama-Foundation:mainfrom
nikolauspschuetz:fix/discrete-contains-overflow
Aug 4, 2026
Merged

pseudo-rnd-thoughts merged 2 commits into
Farama-Foundation:mainfrom
nikolauspschuetz:fix/discrete-contains-overflow

Conversation

@nikolauspschuetz

@nikolauspschuetz nikolauspschuetz commented Jul 23, 2026 •

Copy link
Copy Markdown
Contributor

Description

Discrete.contains(x) raises OverflowError instead of returning False when x is a Python int outside the range representable by the space's dtype:

from gymnasium.spaces import Discrete
Discrete(5).contains(10**20)   # OverflowError: Python int too large to convert to C long

The normalization cast np.dtype(self.dtype).type(x) overflows for such values. Since a value that doesn't fit the space's dtype cannot be a member, contains should return False. Companion to #1616, which fixed the comparison overflow but not this input-cast one.

The fix guards the cast (try/except OverflowError -> return False) and adds a regression test covering ±10**20, 2**63, and -2**63 - 1.

Verified: fails on main (OverflowError at discrete.py:204), passes with the fix; full tests/spaces/test_discrete.py green (38 passed) on Python 3.12 / numpy 2.5.1.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Disclosure, for the record: Claude Code helped spot this and draft the fix/test; I reproduced and verified it myself.

@nikolauspschuetz
nikolauspschuetz marked this pull request as ready for review July 23, 2026 19:01
Discrete.contains(x) cast a Python int to the space's dtype before the
membership check; for values outside the dtype range (e.g. Discrete(5)
.contains(10**20)) that cast raised OverflowError instead of returning
False. Guard the cast and return False, since a value that does not fit
the dtype cannot be a member. Companion to Farama-Foundation#1616 (comparison overflow).
Adds a regression test.
@nikolauspschuetz
nikolauspschuetz force-pushed the fix/discrete-contains-overflow branch from b9a337c to 28e8fc2 Compare July 24, 2026 04:59
Comment thread gymnasium/spaces/discrete.py Outdated
as_np = np.dtype(self.dtype).type(x)
try:
as_np = np.dtype(self.dtype).type(x)
except OverflowError:

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Rationale: casting a Python int to the space's dtype (np.dtype(self.dtype).type(x)) raises OverflowError when x is outside the dtype range (e.g. Discrete(5).contains(10**20)). A value that does not fit the dtype cannot be a member, so we return False rather than let the cast raise. Companion to #1616, which fixed the comparison overflow but not this input-cast one.

Comment thread gymnasium/spaces/discrete.py Outdated
as_np = np.dtype(self.dtype).type(x)
try:
as_np = np.dtype(self.dtype).type(x)
except OverflowError:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This only catches values large enough for NumPy to raise. With the supported NumPy 1.x, Discrete(3, start=125, dtype=np.int8).contains(-129) casts -129 to 127 and returns True; checking the Python int against np.iinfo(self.dtype) before the cast would cover both paths.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch — you're right. The OverflowError guard only covered numpy 2.x; on numpy 1.x np.int8(-129) silently wraps to 127 and was wrongly reported as contained. Switched to checking the Python int against np.iinfo(self.dtype) before the cast (0419669), which handles both paths, and extended the test with the small-dtype wrap cases. Verified across numpy 1.22 → 2.3 on Python 3.10–3.13.

The OverflowError guard only caught out-of-range ints on numpy 2.x. On
numpy 1.x a too-small/large int silently wraps into range (np.int8(-129)
-> 127) and was wrongly reported as contained. Check the Python int
against np.iinfo(self.dtype) first, covering both numpy majors.

@pseudo-rnd-thoughts pseudo-rnd-thoughts left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the PR, looks good

@pseudo-rnd-thoughts
pseudo-rnd-thoughts merged commit 24c59a7 into Farama-Foundation:main Aug 4, 2026
12 checks passed
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