Skip to content
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

Fix type hints for callbacks, utils and VecTranspose #1648

Merged
merged 10 commits into from
Aug 29, 2023

Conversation

araffin
Copy link
Member

@araffin araffin commented Aug 20, 2023

Description

Motivation and Context

  • I have raised an issue to propose this change (required for new features and bug fixes)

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation (update in the documentation)

Checklist

  • I've read the CONTRIBUTION guide (required)
  • I have updated the changelog accordingly (required).
  • My change requires a change to the documentation.
  • I have updated the tests accordingly (required for a bug fix or a new feature).
  • I have updated the documentation accordingly.
  • I have opened an associated PR on the SB3-Contrib repository (if necessary)
  • I have opened an associated PR on the RL-Zoo3 repository (if necessary)
  • I have reformatted the code using make format (required)
  • I have checked the codestyle using make check-codestyle and make lint (required)
  • I have ensured make pytest and make type both pass. (required)
  • I have checked that the documentation builds using make doc (required)

Note: You can run most of the checks using make commit-checks.

Note: we are using a maximum length of 127 characters per line

Copy link
Collaborator

@ernestum ernestum left a comment

Choose a reason for hiding this comment

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

Good choice to add/improve type annotations! I left some comments and questions but LGTM overall.

stable_baselines3/common/callbacks.py Outdated Show resolved Hide resolved
stable_baselines3/common/callbacks.py Show resolved Hide resolved
stable_baselines3/common/save_util.py Show resolved Hide resolved
@@ -255,7 +260,7 @@ def open_path_pathlib(path: pathlib.Path, mode: str, verbose: int = 0, suffix: O

if mode == "r":
try:
path = path.open("rb")
path = path.open("rb") # type: ignore[assignment]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why not just use a new variable name here? I think it would be more readable and you would not need the type ignore.

Copy link
Member Author

Choose a reason for hiding this comment

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

that would be indeed the normal way to solve it but because of the dispatch, it is not possible I think (or we would need to do multiple calls to open_path instead of one.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh ok. It took me a while to get it and at first it looked like line 291 would lead to an infinite recursion in the "happy path". Only then I scrolled up and realized that the type assumptions are broken and this breaks the infinite recursion. I think it might have been easier to understand with two calls to to open_path. Or maybe even call open_path here and explicitly call open_path_pathlib below to make it even more clear that the below calls are "reattempts with better/corrected arguments.

Copy link
Member Author

Choose a reason for hiding this comment

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

should be fixed in ee24538

@@ -178,7 +178,9 @@ def json_to_data(json_string: str, custom_objects: Optional[Dict[str, Any]] = No


@functools.singledispatch
def open_path(path: Union[str, pathlib.Path, io.BufferedIOBase], mode: str, verbose: int = 0, suffix: Optional[str] = None):
def open_path(
path: Union[str, pathlib.Path, io.BufferedIOBase], mode: str, verbose: int = 0, suffix: Optional[str] = None
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why allow str paths when you reject them in the assert below?

Copy link
Member Author

Choose a reason for hiding this comment

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

I need to check, might be because of the dispatch. (I didn't write that code)

Copy link
Member Author

Choose a reason for hiding this comment

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

yes, because of the dispatch

@@ -255,7 +260,7 @@ def open_path_pathlib(path: pathlib.Path, mode: str, verbose: int = 0, suffix: O

if mode == "r":
try:
path = path.open("rb")
path = path.open("rb") # type: ignore[assignment]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh ok. It took me a while to get it and at first it looked like line 291 would lead to an infinite recursion in the "happy path". Only then I scrolled up and realized that the type assumptions are broken and this breaks the infinite recursion. I think it might have been easier to understand with two calls to to open_path. Or maybe even call open_path here and explicitly call open_path_pathlib below to make it even more clear that the below calls are "reattempts with better/corrected arguments.

@araffin araffin requested a review from ernestum August 25, 2023 19:13
@araffin araffin merged commit e9f0f23 into master Aug 29, 2023
4 checks passed
@araffin araffin deleted the chores/fix-types-utils branch August 29, 2023 14:04
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