Commit 3446f28
fix(core): avoid KeyError when batch_size is given without num_envs (#423)
## Problem
`envpool.make(...)` raises a `KeyError: 'num_envs'` whenever
`batch_size` is supplied without an explicit `num_envs`.
In `EnvRegistry._make_env_spec` (`envpool/registration.py`), the
`batch_size` validation indexed `kwargs["num_envs"]` directly:
```python
if "batch_size" in kwargs:
assert 0 <= kwargs["batch_size"] <= kwargs["num_envs"] # KeyError
```
…while the two seed-normalization blocks immediately above already use
the safe form `kwargs.get("num_envs", 1)`. `num_envs` is never injected
into `kwargs` — it only appears there if the user passes it — so the
bare subscript crashes before `gen_config` is ever reached.
This is reachable through the documented public API:
- `batch_size` is a first-class `make()` argument
([README](../blob/main/README.md) /
`docs/content/python_interface.rst`).
- `batch_size=0` is a **valid** value meaning "default to `num_envs`"
(`core/env_spec.h` turns `batch_size == 0` into `num_envs`).
## Reproduction
```python
import envpool
envpool.make("Pong-v5", env_type="gymnasium", batch_size=0)
# KeyError: 'num_envs' <- before this PR
```
## Fix
Use the same default as the neighboring validation (one-line change,
matches the existing `kwargs.get("num_envs", 1)` pattern):
```python
if "batch_size" in kwargs:
assert 0 <= kwargs["batch_size"] <= kwargs.get("num_envs", 1)
```
Behavior after the fix:
| call | before | after |
| --- | --- | --- |
| `batch_size=0` (no `num_envs`) | `KeyError` ✗ | **OK** (defaults to
`num_envs`) |
| `batch_size=1` (no `num_envs`) | `KeyError` ✗ | **OK** |
| `batch_size=2` (no `num_envs`, out of range) | `KeyError` ✗ |
`AssertionError` (correctly rejected) |
| `num_envs=64, batch_size=16` (README usage) | OK | OK (unchanged) |
The documented multi-env usage is unaffected; only the spurious
`KeyError` is removed and out-of-range values are now rejected with a
proper `AssertionError`.
## Test
Added `test_make_batch_size_without_num_envs` to `envpool/make_test.py`,
which fails (`KeyError`) before the fix and passes after.
## Validation
```
make ruff py-format # pass
make mypy # changed files clean (under py311 target)
```
Co-authored-by: hobostay <hobostay@users.noreply.github.com>
Co-authored-by: Claude <noreply@anthropic.com>1 parent 8849255 commit 3446f28
2 files changed
Lines changed: 13 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
282 | 282 | | |
283 | 283 | | |
284 | 284 | | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
285 | 297 | | |
286 | 298 | | |
287 | 299 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
235 | 235 | | |
236 | 236 | | |
237 | 237 | | |
238 | | - | |
| 238 | + | |
239 | 239 | | |
240 | 240 | | |
241 | 241 | | |
| |||
0 commit comments