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

Add mypy and static type checking to Habitat Lab #492

Merged
merged 27 commits into from
Oct 13, 2020
Merged

Conversation

Skylion007
Copy link
Contributor

@Skylion007 Skylion007 commented Oct 3, 2020

Motivation and Context

Actually uses all the TypeHints we have in Habitat-Lab by adding a pre-commit hook for them.
Before doing that, there are quite a lot of type signature errors that need to be fixed in habitat.

This actually required quite a lot of refactoring just to get the base habitat to use properly use these type hints and to fix existing errors in typing. I had to add quite a few "ignores" to fix some pretty serious issues we have with inheritance and overloading variables / types of Python classes. I disabled the inheritance errors with a brand new feature of mypy, we should fix it longterm, but this solution should suffice for now.

As of this typing, I have fixed all except the eqa_task and tasks/nav/nav.py. These two classes alone account for 84 typing errors in Habitat, currently. The only area I haven't touched is the slam agents in the habitat_baselines since we don't interface a lot with that baseline code.

How Has This Been Tested

With PyTest and MyPy
Like with Habitat-Sim I used monkeytype to help out, but since so many existing type hints were wrong and the fact that we have class names that are the same throughout habitat, it required quite a large number of manual changes so far.

Types of changes

  • Large Refactor and Dev Tools update

Checklist

  • My code follows the code style of this project.
  • I have read the CONTRIBUTING document.
  • I have completed my CLA (see CONTRIBUTING)
  • All new and existing tests passed.

@facebook-github-bot facebook-github-bot added the CLA Signed Do not delete this pull request or issue due to inactivity. label Oct 3, 2020
@Skylion007
Copy link
Contributor Author

@erikwijmans @mathfac a huge pain problem here is the our core classes do not defined any call signatures which is making overloading them quite annoying as I have to add ignores all for every single sensor or measure. We should standardized a Sensor / Measurement API to fix this. See incompatible overrides for more info: https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides

@Skylion007
Copy link
Contributor Author

I added enough updated typehints and ignore comments for the bare minimum of it to stop complaining. We tend to extend classes in very non-pythonic ways around Habitat-Lab.

We also should really fix the call signature of the base Sensor and Measure class so that it's more consistent.

@Skylion007 Skylion007 changed the title [WIP] Add mypy and static type checking to Habitat Lab Add mypy and static type checking to Habitat Lab Oct 5, 2020
@Skylion007 Skylion007 mentioned this pull request Oct 6, 2020
7 tasks
Copy link
Contributor

@erikwijmans erikwijmans left a comment

Choose a reason for hiding this comment

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

I get the motivation of doing this, but right now there are so many # type: ignore's floating around that I am not sure if the value add vs. PITA ratio is right.

Maybe we start with typing and enforcing typing in at least habitat/core and maybe habitat and slowly expand it?

habitat/datasets/object_nav/object_nav_dataset.py Outdated Show resolved Hide resolved
habitat/tasks/nav/object_nav_task.py Outdated Show resolved Hide resolved
habitat/core/registry.py Outdated Show resolved Hide resolved
@Skylion007 Skylion007 requested a review from erikwijmans October 7, 2020 17:59
@Skylion007
Copy link
Contributor Author

Skylion007 commented Oct 7, 2020

@mosra Really confused how this broke our doc build. Any ideas? Was it the abstract class constructor?

@mosra
Copy link
Contributor

mosra commented Oct 7, 2020

Heh. Looks like the newly added hints hit some corner case in the doc generator, and it might be some Py3.6-specific thing, even. I suppose you don't have the doc build running locally so you could run python.py with --debug to see at least what class/module it blows up on?

I'll check this tomorrow, please ping me on Slack in case I forget ;)

@mosra
Copy link
Contributor

mosra commented Oct 8, 2020

Found the culprit and made a workaround in m.css, it's something deep inside undocumented Python typing internals again. Currently fighting some CI breakage annoyances, hopefully I'll have it landed in m.css master later today.

@mosra
Copy link
Contributor

mosra commented Oct 8, 2020

@Skylion007 Fix is in mosra/m.css@0b94575. Pull the doc/m.css submodule in habitat-sim to get it.

@Skylion007
Copy link
Contributor Author

Skylion007 commented Oct 8, 2020

@erikwijmans Any further reduction in type ignores requires us to remove most of the args / kwargs passing in habitat.core or requiring newer versions of numpy / torch that have better type hints.

@Skylion007
Copy link
Contributor Author

@erikwijmans Great news, a version of MyPy just dropped yesterday that allows me to do global/module level ignores just like Flake8 does. This should solve the issue. I also went back and removed most of the type ignores and replaced with explicit PEP528 casting where I could or defined the missing attributes on the parent objects.

Skylion007 and others added 2 commits October 10, 2020 15:46
Co-authored-by: Erik Wijmans <etw@gatech.edu>
@Skylion007
Copy link
Contributor Author

Whoops, didn't mean to push that.

Copy link
Contributor

@mathfac mathfac left a comment

Choose a reason for hiding this comment

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

Thank you Aaron for improving checks on API promises. Looks great to go.

@Skylion007 Skylion007 merged commit 9a5003f into master Oct 13, 2020
@Skylion007 Skylion007 deleted the monketype_typing branch October 13, 2020 17:31
@Skylion007
Copy link
Contributor Author

Grr, just realized a bug where all the Habitat_Sim type imports now block a soft install of habitat_lab without habitat_sim. We need to create a CI Test for having habitat-lab work without habitat_sim. Throughts @erikwijmans @mathfac ?

@Skylion007
Copy link
Contributor Author

Fixed in #498

srama2512 pushed a commit that referenced this pull request Mar 15, 2023
* Add SceneNode attachment option to addObject and makeRigidObject.

* Added RigidObject::visualNode_ to clearly separate Drawable branch of a physical object's SceneNode hierarchy. 

* Enabled optional deletion of visualNode_ and object SceneNode hierarchy when removing objects from PhysicsManager.

* Added python bindings and unit tests in python and C++.
dannymcy pushed a commit to dannymcy/habitat-lab that referenced this pull request Jul 8, 2024
* typed Habitat Core

* Fix more habitat typing issues

* Fix objnav typecheck

* Fix incorrect config type

* complete habitat lab mypy conversion

* Migrate mypy config to mypy.ini

* Remove unused mypy config values

* Re-enable episodes setter

* Finish typing habitat_baselines

* Stricten type hints in habitat.core.simulator

* Fix registry type add constructor to Simulator

* Fix import error

* Make max singleton

* Remove list copy

* Update mypy with global ignores

* Do better list casting

* Update pre-commit hooks

* unify pre-commit style

* Add check-yaml pre-commit hook

* Bugfix and optimize ci tests

* Remove one more type ignore

* Update habitat_baselines/rl/ppo/ppo.py

* Fix ppo imports

* remove dead code

* Revert "remove dead code"

This reverts commit acf9fd8.
HHYHRHY pushed a commit to SgtVincent/EMOS that referenced this pull request Aug 31, 2024
* typed Habitat Core

* Fix more habitat typing issues

* Fix objnav typecheck

* Fix incorrect config type

* complete habitat lab mypy conversion

* Migrate mypy config to mypy.ini

* Remove unused mypy config values

* Re-enable episodes setter

* Finish typing habitat_baselines

* Stricten type hints in habitat.core.simulator

* Fix registry type add constructor to Simulator

* Fix import error

* Make max singleton

* Remove list copy

* Update mypy with global ignores

* Do better list casting

* Update pre-commit hooks

* unify pre-commit style

* Add check-yaml pre-commit hook

* Bugfix and optimize ci tests

* Remove one more type ignore

* Update habitat_baselines/rl/ppo/ppo.py

* Fix ppo imports

* remove dead code

* Revert "remove dead code"

This reverts commit acf9fd8.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed Do not delete this pull request or issue due to inactivity.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants