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

Improve typing: better type hints, increased type safety, mypy support #534

Merged
merged 214 commits into from
Oct 8, 2022

Conversation

Rocamonde
Copy link
Member

@Rocamonde Rocamonde commented Aug 23, 2022

Description

Fixes #532.

Features

  • Ensures all classes have type annotations in their attributes
  • Enforces no mypy type errors on new diffs from PRs
  • Fixes 200+ type errors and several exposed bugs as a result
  • Redesigns internal random number generation conventions to use modern np.random.Generator and require explicit RNG passing to all instances that require random numbers if and only if RNG is being used.
  • Disallows # type: ignore that does not specify the specific rule to be ignored, e.g. # type: ignore[return-value].

@codecov
Copy link

codecov bot commented Aug 23, 2022

Codecov Report

Merging #534 (f935873) into master (a738955) will increase coverage by 0.16%.
The diff coverage is 98.46%.

@@            Coverage Diff             @@
##           master     #534      +/-   ##
==========================================
+ Coverage   97.26%   97.43%   +0.16%     
==========================================
  Files          88       88              
  Lines        8116     8375     +259     
==========================================
+ Hits         7894     8160     +266     
+ Misses        222      215       -7     
Impacted Files Coverage Δ
src/imitation/scripts/parallel.py 64.15% <25.00%> (+4.50%) ⬆️
src/imitation/util/sacred.py 90.14% <75.00%> (-0.91%) ⬇️
src/imitation/algorithms/adversarial/common.py 97.20% <88.23%> (-0.39%) ⬇️
src/imitation/data/buffer.py 97.50% <95.00%> (+0.06%) ⬆️
src/imitation/algorithms/density.py 94.40% <96.36%> (+8.29%) ⬆️
src/imitation/util/util.py 98.98% <97.29%> (-1.02%) ⬇️
src/imitation/algorithms/adversarial/airl.py 100.00% <100.00%> (ø)
src/imitation/algorithms/base.py 98.78% <100.00%> (-0.05%) ⬇️
src/imitation/algorithms/bc.py 100.00% <100.00%> (ø)
src/imitation/algorithms/dagger.py 99.11% <100.00%> (+0.05%) ⬆️
... and 57 more

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@Rocamonde Rocamonde requested a review from AdamGleave October 7, 2022 18:37
Copy link
Member

@AdamGleave AdamGleave left a comment

Choose a reason for hiding this comment

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

LGTM! There's a couple minor changes outlined in comments, most substantive of one is avoiding adding some spurious "output": [] in notebooks (I think spurious -- if there's a good reason for it happy to change my mind). But I don't think this needs my review again unless you end up making substantial changes.

Thanks for seeing this PR through to the end :)

.circleci/config.yml Outdated Show resolved Hide resolved
ci/check_typeignore.py Show resolved Hide resolved
if cell["cell_type"] == "code" and not cell["source"]:
if check_only:
raise UncleanNotebookError(f"Notebook {file} has empty code cell")
nb.cells.remove(cell)
was_dirty = True
for field, default in fields_defaults:
if cell.get(field) != default:
Copy link
Member

Choose a reason for hiding this comment

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

There's a slight semantic change here which I think wasn't intended. Previously you were OK with "field" not in cell" -- but now for outputs/metadata you add the default. This has caused the notebooks to have "outputs": [] even for text entries that don't usually produce any output.

Probably want to change this to something like if field in cell and cell.get(field) != default? Or have a list of permissible values as well as defaults, like:

for field, default, allowed in fields_defaults:
    if cell.get(field) not in allowed:
        # ....

where you could include None in allowed for "outputs" even while keeping [] as the default.

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 think the "correct" way to format is to have an empty list for code cells, no entry for any other cell. Not sure if the best way to do this is by allowing different defaults by different cell types. What you're suggesting achieves the same result when using a notebook executor because md cells never add the "outputs" key, but it would not roll it back automatically from e.g. the state it is now.

Copy link
Member Author

Choose a reason for hiding this comment

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

Came up with a solution for this, it is somewhat different to what we've discussed, let me know if you want to take a look before I merge.

docs/tutorials/1_train_bc.ipynb Outdated Show resolved Hide resolved
@Rocamonde
Copy link
Member Author

@AdamGleave your comment on ValueError is actually on a previous commit -- if you check the current branch, I have this:

try:
check_files(file_list)
except InvalidTypeIgnore as e:
print(e)

That's why I marked it as resolved previously

Copy link
Member

@AdamGleave AdamGleave left a comment

Choose a reason for hiding this comment

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

LGTM once tests pass -- one suggestion on naming

ci/clean_notebooks.py Outdated Show resolved Hide resolved
@AdamGleave
Copy link
Member

@AdamGleave your comment on ValueError is actually on a previous commit -- if you check the current branch, I have this:

Ah, that explains it, sorry about that.

@Rocamonde Rocamonde merged commit 005c15f into master Oct 8, 2022
@Rocamonde Rocamonde deleted the add-mypy branch October 8, 2022 17:30
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.

Add mypy as a second type-checker
2 participants