Repository navigation
Fix Discrete.contains raising OverflowError for out-of-dtype ints - #1648
Conversation
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.
b9a337c to
28e8fc2
Compare
| as_np = np.dtype(self.dtype).type(x) | ||
| try: | ||
| as_np = np.dtype(self.dtype).type(x) | ||
| except OverflowError: |
There was a problem hiding this comment.
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.
| as_np = np.dtype(self.dtype).type(x) | ||
| try: | ||
| as_np = np.dtype(self.dtype).type(x) | ||
| except OverflowError: |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
Thanks for the PR, looks good
Description
Discrete.contains(x)raisesOverflowErrorinstead of returningFalsewhenxis a Pythonintoutside the range representable by the space's dtype: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,containsshould returnFalse. 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(OverflowErroratdiscrete.py:204), passes with the fix; fulltests/spaces/test_discrete.pygreen (38 passed) on Python 3.12 / numpy 2.5.1.Type of change
Disclosure, for the record: Claude Code helped spot this and draft the fix/test; I reproduced and verified it myself.