habitat-sim
habitat-sim copied to clipboard
Pyupgrade to modern typing syntax and enable pyupgrade pre-commit hook
Motivation and Context
- This upgrades our type annotation to modern Python syntax. It's a lot more succinct and removes a lot of typing imports which may potentially allow faster startups. ~This may break some old runtime type checkers, but we don't use TypeGuard at all anymore and mypy has full support.
- For instance '|' can now replace Union[] and Optional etc.
- This enables a future flag which will be the default in 3.11 anyhow so might as well get the code written
- This also adds a pre-commit hook called pyupgrade which ensures we are using the latest syntax available for 3.7 and other best practices.
- I also added an isort config which adds the future import for annotations to all python files which do at least one import.~
- The only change this does now is delayed type annotation inference through the
__future__ import annotationsmodule and adds pyupgrade.
How Has This Been Tested
- locally with pre-commit and pytest
Types of changes
- [X] Docs change / refactoring / dependency upgrade
- [X] Bug fix (non-breaking change which fixes an issue)
Checklist
- [X] My code follows the code style of this project.
- [X] My change requires a change to the documentation.
- [X] I have updated the documentation accordingly.
- [X] I have read the CONTRIBUTING document.
- [X] I have completed my CLA (see CONTRIBUTING)
- [X] I have added tests to cover my changes.
- [X] All new and existing tests passed.
@mosra This change seems blocked by m.css support?
Nope, m.css just passes through a typing exception.
The issue is on your side, PEP604 (| instead of Union) is only since Python 3.10.
@mosra Okay so interesting, it's not 'SUPPORTED' per say, but mypy and stuff can parse all those fields out fine as long as delayed annotations is enabled (since they don't bother the interpreter until runtime)'.
Ah
PEP 585 normally requires Python 3.9 and PEP 604 would require 3.10. The use in versions prior to that, the current default is 3.8, is only possible with postponed evaluation of type annotations, [PEP 563](https://www.python.org/dev/peps/pep-0563/). This can be enabled starting in 3.7 with from __future__ import annotations. However this workaround also means that any dynamic evaluation will throw an error. Some common examples are the use of typing.get_type_hints or eval.
Yes, but I'm relying on the interpreter being able to understand the types, so I can link them etc. So if you run m.css on 3.7, it'll fail. You'd have to run m.css on 3.10. (EDIT: Unlike mypy and stuff, I don't want to implement my own python parser for type annotations, that would make everything a lot more complicated.)
I'd say let's just postpone changes that require 3.10 until 3.10 is our minimum version.