Skip to content

Full type hinting #2942

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 35 commits into from
Jul 4, 2022
Merged

Conversation

pseudo-rnd-thoughts
Copy link
Contributor

A follow up PR to #2939 that was inspired by #2918 to fully type check the whole gym project (along with tests).
This is removes all files and folders from the exlude folder however more work is required to add the core API to the strict parameter.
However, this should catch any weird or incorrect type hints but at this time not force PRs to include type hints

In the process, I discovered a couple of strange tests for async vector envs which needs fixing however it was outside the scope of this PR

obs, info = self.env.reset(**kwargs)

if self.is_vector_env:
Copy link
Contributor

Choose a reason for hiding this comment

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

This also looks like a change of behavior, is this intended? Can you explain it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I believe that it is not a change in behaviour. The problem previous was that info could have been unbounded. To avoid the issue, I have all of the return info code in one part of the if statement and without info is the second part. This has the cost of copying the if vector code twice but it shouldn't be a issue

finally:
env.close()

env = AsyncVectorEnv(env_fns, shared_memory=shared_memory)
Copy link
Contributor

Choose a reason for hiding this comment

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

This is one of the situations where it might be better to do a pyright ignore, or maybe something more clever.

Basically what might (maybe?) happen here is that something crashes inside the environment, the test gets aborted, and then the environment is never properly closed, which can lead to a memory leak, because there are some hanging processes. The finally clause prevents that.

It might be possible by using a feature that's existed in gym very quietly, which is using an env as a context manager:

with AsyncVectorEnv(env_fns, shared_memory=shared_memory) as env:
    ... # do whatever

I'm not 100% sure, but I think this will make sure to close the environment even if it crashes

Copy link
Contributor

Choose a reason for hiding this comment

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

(on second thought, I'm not sure if this is implemented for VectorEnv, but it definitely is for normal envs)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The with style would work as VectorEnv inherit from gym.Env.
However, if the crash is intended then the pytest.raises will catch the error.
Otherwise, the test will fail prevent the CI from passing. Therefore, I don't think that we are losing much
In either case, there won't be a long lasting issue.

env_spec
for env_spec in all_testing_env_specs
if any(
f"gym.{ep}" in env_spec.entry_point
f"gym.envs.{ep}" in env_spec.entry_point
Copy link
Contributor

Choose a reason for hiding this comment

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

Why was this changed? Is this a bugfix unrelated to the typing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is unrelated to type hinting but it fixes a previous bug

@jkterry1 jkterry1 merged commit 2ede090 into openai:master Jul 4, 2022
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.

4 participants