Skip to content

Add wrapper checker utility for validating wrapper implementations#1556

Open
Jatin-Shihora wants to merge 3 commits intoFarama-Foundation:mainfrom
Jatin-Shihora:feature/add-wrapper-checker
Open

Add wrapper checker utility for validating wrapper implementations#1556
Jatin-Shihora wants to merge 3 commits intoFarama-Foundation:mainfrom
Jatin-Shihora:feature/add-wrapper-checker

Conversation

@Jatin-Shihora
Copy link
Copy Markdown

Description

Add a check_wrapper() utility function for validating that wrapper implementations follow Gymnasium's Wrapper API correctly, similar to the existing check_env() for environments.

The checker verifies:

  • The wrapper is a valid gymnasium.Wrapper instance with an inner environment
  • Observation and action spaces exist and are valid
  • unwrapped returns the base environment
  • reset() returns (obs, info) with correct types and obs in space
  • step() returns (obs, reward, terminated, truncated, info) with correct types
  • Reward is a valid number (warns on NaN/infinity)
  • render() and close() don't crash

Fixes #6

Type of change

  • New feature (non-breaking change which adds functionality)

Checklist:

  • I have run the pre-commit checks with pre-commit run --all-files (see CONTRIBUTING.md instructions to set it up)
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation ()
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

pseudo-rnd-thoughts

This comment was marked as duplicate.

Copy link
Copy Markdown
Member

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

Choose a reason for hiding this comment

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

Thanks for the PR, this overall looks good.
Could you add a test to check every wrapper that we implement to check that they all work as expected

@Jatin-Shihora
Copy link
Copy Markdown
Author

Thanks for the PR, this overall looks good. Could you add a test to check every wrapper that we implement to check that they all work as expected

Hi @pseudo-rnd-thoughts thanks for reviewing the PR. I just added tests that run check_wrapper() against 27 of built-in wrappers to verify they all pass the checker.

I split the wrappers into three groups based on the environment they need:

  1. CartPole (Discrete actions, Box obs): 19 wrappers including TimeLimit, Autoreset, FlattenObservation, NormalizeObservation, ClipReward, StickyAction, etc.
  2. MountainCarContinuous (Box actions): 3 wrappers that require continuous action spaces: ClipAction, RescaleAction, TransformAction
  3. Special environments via GenericTestEnv: 5 wrappers that need custom setups
    • GrayscaleObservation: using a Box(0, 255, shape=(25,25,3), dtype=uint8) image env
    • DiscretizeObservation: using a finite-bounded Box obs env
    • DiscretizeAction: using a finite-bounded Box action env
    • RenderCollection and AddRenderObservation: using render_mode="rgb_array" with a mock render function

The remaining wrappers I couldn't find a clean way to test:

  • ResizeObservation: imports cv2 (OpenCV) at init time, so it raises DependencyNotInstalled if opencv isn't available. Would need gymnasium[other] installed in the test environment.
  • AtariPreprocessing: requires Atari ROM files to construct the base environment.
  • RecordVideo: requires moviepy to be installed for video encoding.
  • HumanRendering, AddWhiteNoise, ObstructView: these rendering wrappers need a real display backend (pygame/OpenGL), which doesn't work in headless CI.
  • JaxToNumpy, JaxToTorch, NumpyToTorch: lazy-loaded and require jax/torch to be installed.

If there's a preferred way to handle these (maybe pytest.importorskip for the ones that just need optional deps?), happy to add those too.

total tests: 41 tests (14 original + 27 new ones)

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.

[Proposal] Add wrapper checker

2 participants