Skip to content

fix(rl): accept numpy ints and reject bool in is_positive_integer#1552

Open
kitrakrev wants to merge 1 commit into
google:mainfrom
kitrakrev:fix-is-positive-integer-numpy-bool
Open

fix(rl): accept numpy ints and reject bool in is_positive_integer#1552
kitrakrev wants to merge 1 commit into
google:mainfrom
kitrakrev:fix-is-positive-integer-numpy-bool

Conversation

@kitrakrev

Copy link
Copy Markdown

Summary

tunix.rl.utils.is_positive_integer currently has two edge cases:

  1. Rejects numpy integer scalars. isinstance(np.int64(5), int) is False, so passing any numpy scalar (common when values flow through JAX/numpy) raises ValueError for a perfectly valid positive integer.
  2. Accepts True / False. bool is a subclass of int in Python, so True passes the check and False is rejected only for being <= 0. Neither is semantically a valid positive integer in this context.

This PR:

  • accepts np.integer scalars in addition to Python int
  • explicitly rejects bool values
  • preserves existing behavior for None, 0, and negatives

Changes

  • tunix/rl/utils.py — update is_positive_integer logic + docstring
  • tests/rl/rl_utils_test.py — add IsPositiveIntegerTest with 9 cases

Test plan

  • python -m absl.testing tests/rl/rl_utils_test.pyIsPositiveIntegerTest passes
  • Logic verified standalone against int / np.int32/64/uint8 / float / bool / str / None / zero / negative

Related

Supersedes the stalled PRs #903 and #1000 (both attempted variants of the same fix; neither landed). Closes the remaining edges on the original report in #902 / #944 (the value.is_integer() crash itself is already fixed on main).

@google-cla

google-cla Bot commented May 29, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

`is_positive_integer` previously rejected numpy integer scalars
(e.g. `np.int64`) since `isinstance(np.int64(5), int)` is False, and
silently accepted `True`/`False` since `bool` is a subclass of `int`.

This change:
- accepts `np.integer` scalars in addition to Python `int`
- explicitly rejects `bool` values
- preserves existing behavior for `None`, `0`, and negative values

Adds a dedicated `IsPositiveIntegerTest` covering int, numpy ints, bool,
float, str, None, zero, negatives, and error message format.

Supersedes the stalled PRs google#903 and google#1000.
@kitrakrev kitrakrev force-pushed the fix-is-positive-integer-numpy-bool branch from 1f9167b to 62e2543 Compare May 29, 2026 19:16
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.

2 participants