gym icon indicating copy to clipboard operation
gym copied to clipboard

Possible typing issue in dict.py?

Open blumu opened this issue 3 years ago • 6 comments

Could there be a typing declaration error in:

https://github.com/openai/gym/blob/53d784eafed28d31ec41c36ebd9eee14b0dc6d41/gym/spaces/dict.py#L15

Looking at the definition of class Space[T_cov],

https://github.com/openai/gym/blob/53d784eafed28d31ec41c36ebd9eee14b0dc6d41/gym/spaces/space.py#L24

the generic parameter T_cov denotes the type of values returned by the sample function:

https://github.com/openai/gym/blob/53d784eafed28d31ec41c36ebd9eee14b0dc6d41/gym/spaces/space.py#L90

which cannot be of Space type.

So, should the Dict class be defined instead as follows?

class Dict(Space[TypingDict[str, object]], Mapping): 

or even better, if we could pass the dictionary value type as a generic parameter that would allow for proper type checking of the dictionary values, e.g.,:

_T_cov_dict= TypeVar("_T_cov_dict", covariant=True)
class Dict(Space[_T_cov_dict], Mapping): 

blumu avatar Sep 23 '22 19:09 blumu

Yes, the type hint is currently incorrect. I would change the proposed type hint to Dict(Space[str, Any], Mapping[str, Space[Any]])

However even this is technically incorrect as the dictionary key doesn't need to be a string however we could ignore this in the type hint

pseudo-rnd-thoughts avatar Sep 24 '22 17:09 pseudo-rnd-thoughts

@pseudo-rnd-thoughts did you mean Space[Any] instead of Space[str, Any]? (the Space class takes only one generic type parameter).

blumu avatar Sep 24 '22 18:09 blumu

I meant Space[TypingDict[str, Any]]

pseudo-rnd-thoughts avatar Sep 24 '22 20:09 pseudo-rnd-thoughts

Hi, I have implemented the changes suggested by @pseudo-rnd-thoughts and made a PR. Please Close this issue if it gets resolved by the PR, otherwise let me know if there are any changes. Thanks

Warlord-K avatar Oct 06 '22 11:10 Warlord-K

Hi @maintainers , Please review my PR and merge it if it closes the issue, otherwise please let me know if any modifications are needed.

Warlord-K avatar Oct 07 '22 08:10 Warlord-K

Hi, the PR looks good however, we are planning on reducing the number of accepted PRs to Gym due to some work behind the scenes that we are hoping to announce soon (in the next week hopefully)

pseudo-rnd-thoughts avatar Oct 07 '22 10:10 pseudo-rnd-thoughts