Add pyright to precommit (#2796)

This commit is contained in:
Mark Towers
2022-05-09 16:22:49 +01:00
committed by GitHub
parent 40c26ab9c6
commit 31e6f23e67
6 changed files with 61 additions and 56 deletions

View File

@@ -1,26 +0,0 @@
---
name: lint_python
on: [pull_request, push]
jobs:
pyright:
name: Check types with pyright
runs-on: ubuntu-latest
strategy:
matrix:
python-platform: ["Linux"]
python-version: ["3.7", "3.8", "3.9", "3.10"]
fail-fast: false
env:
PYRIGHT_VERSION: 1.1.235
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- run: pip install -e .[nomujoco]
- uses: jakebailey/pyright-action@v1
with:
version: ${{ env.PYRIGHT_VERSION }}
python-platform: ${{ matrix.python-platform }}
python-version: ${{ matrix.python-version }}
no-comments: ${{ matrix.python-version != '3.9' || matrix.python-platform != 'Linux' }} # Having each job create the same comment is too noisy.

View File

@@ -32,3 +32,14 @@ repos:
- id: pyupgrade
# TODO: remove `--keep-runtime-typing` option
args: ["--py37-plus", "--keep-runtime-typing"]
- repo: local
hooks:
- id: pyright
name: pyright
entry: pyright
language: node
pass_filenames: false
types: [python]
additional_dependencies: ["pyright"]
args:
- --project=pyproject.toml

View File

@@ -23,18 +23,23 @@ This section contains technical instructions & hints for the contributors.
## Type checking
The project uses `pyright` to check types.
To type check locally, install `pyright` per official [instructions](https://github.com/microsoft/pyright#command-line).
It's configuration lives under a section of `pyproject.toml`. It includes list of files currently supporting type checks. Use `pyright` CLI command to launch type checks.
It's configuration lives within `pyproject.toml`. It includes list of included and excluded files currently supporting type checks.
To run `pyright` for the project, run the pre-commit process (`pre-commit run --all-files`) or `pyright --project=pyproject.toml`
Alternatively, pyright is a built-in feature of VSCode that will automatically provide type hinting.
### Adding typing to more modules and packages
If you would like to add typing to a module in the project,
add the path to the file(s) in the include section
(pyproject.toml -> [tool.pyright] -> include).
Then you can run `pyright` to see list of type problems in the newly added file, and fix them.
the list of included, excluded and strict files can be found in pyproject.toml (pyproject.toml -> [tool.pyright]).
To run `pyright` for the project, run the pre-commit process (`pre-commit run --all-files`) or `pyright`
## Git hooks
The CI will run several checks on the new code pushed to the Gym repository. These checks can also be run locally without waiting for the CI by following the steps below:
1. [install `pre-commit`](https://pre-commit.com/#install),
2. install the Git hooks by running `pre-commit install`.
2. Install the Git hooks by running `pre-commit install`.
Once those two steps are done, the Git hooks will be run automatically at every new commit. The Git hooks can also be run manually with `pre-commit run --all-files`, and if needed they can be skipped (not recommended) with `git commit --no-verify`. **Note:** you may have to run `pre-commit run --all-files` manually a couple of times to make it pass when you commit, as each formatting tool will first format the code and fail the first time but should pass the second time.
Once those two steps are done, the Git hooks will be run automatically at every new commit.
The Git hooks can also be run manually with `pre-commit run --all-files`, and if needed they can be skipped (not recommended) with `git commit --no-verify`.
**Note:** you may have to run `pre-commit run --all-files` manually a couple of times to make it pass when you commit, as each formatting tool will first format the code and fail the first time but should pass the second time.
Additionally, for pull requests, the project runs a number of tests for the whole project using [pytest](https://docs.pytest.org/en/latest/getting-started.html#install-pytest).
These tests can be run locally with `pytest` in the root folder.

View File

@@ -1,4 +1,4 @@
from typing import Dict, Tuple
from typing import Dict, Optional, Tuple
# The following is a map of environments which have been relocated
# to a different namespace. This map is important when reporting
@@ -6,7 +6,7 @@ from typing import Dict, Tuple
# This map should be removed eventually once users
# are sufficiently aware of the environment relocations.
# The value of the mapping is (namespace, package,).
internal_env_relocation_map: Dict[str, Tuple[str, str]] = {
internal_env_relocation_map: Dict[str, Tuple[Optional[str], str]] = {
"Adventure": ("ALE", "ale-py"),
"AdventureDeterministic": (None, "ale-py"),
"AdventureNoFrameskip": (None, "ale-py"),
@@ -386,13 +386,10 @@ internal_env_relocation_map: Dict[str, Tuple[str, str]] = {
"HandReach": (None, "gym-robotics"),
"HandManipulateBlockRotateZ": (None, "gym-robotics"),
"HandManipulateBlockRotateZTouchSensors": (None, "gym-robotics"),
"HandManipulateBlockRotateZTouchSensors": (None, "gym-robotics"),
"HandManipulateBlockRotateParallel": (None, "gym-robotics"),
"HandManipulateBlockRotateParallelTouchSensors": (None, "gym-robotics"),
"HandManipulateBlockRotateParallelTouchSensors": (None, "gym-robotics"),
"HandManipulateBlockRotateXYZ": (None, "gym-robotics"),
"HandManipulateBlockRotateXYZTouchSensors": (None, "gym-robotics"),
"HandManipulateBlockRotateXYZTouchSensors": (None, "gym-robotics"),
"HandManipulateBlockFull": (None, "gym-robotics"),
"HandManipulateBlock": (None, "gym-robotics"),
"HandManipulateBlockTouchSensors": (None, "gym-robotics"),
@@ -413,13 +410,10 @@ internal_env_relocation_map: Dict[str, Tuple[str, str]] = {
"HandReachDense": (None, "gym-robotics"),
"HandManipulateBlockRotateZDense": (None, "gym-robotics"),
"HandManipulateBlockRotateZTouchSensorsDense": (None, "gym-robotics"),
"HandManipulateBlockRotateZTouchSensorsDense": (None, "gym-robotics"),
"HandManipulateBlockRotateParallelDense": (None, "gym-robotics"),
"HandManipulateBlockRotateParallelTouchSensorsDense": (None, "gym-robotics"),
"HandManipulateBlockRotateParallelTouchSensorsDense": (None, "gym-robotics"),
"HandManipulateBlockRotateXYZDense": (None, "gym-robotics"),
"HandManipulateBlockRotateXYZTouchSensorsDense": (None, "gym-robotics"),
"HandManipulateBlockRotateXYZTouchSensorsDense": (None, "gym-robotics"),
"HandManipulateBlockFullDense": (None, "gym-robotics"),
"HandManipulateBlockDense": (None, "gym-robotics"),
"HandManipulateBlockTouchSensorsDense": (None, "gym-robotics"),

View File

@@ -148,10 +148,7 @@ class APIError(Error):
return self._message
def __str__(self):
try: # Python 2
return unicode(self).encode("utf-8")
except NameError: # Python 3
return self.__unicode__()
return self.__unicode__()
class APIConnectionError(APIError):

View File

@@ -1,20 +1,44 @@
[tool.pyright]
include = [
"gym/version.py",
"gym/logger.py",
"gym/envs/registration.py",
"gym/spaces/**.py",
"gym/core.py",
"gym/utils/seeding.py",
"gym/envs/classic_control/*.py"
"gym/**",
"tests/**"
]
exclude = [
"**/node_modules",
"**/__pycache__",
]
strict = ["gym/version.py", "gym/logger.py"]
reportMissingImports = true
"gym/envs/box2d/**",
# "gym/envs/classic_control/**",
"gym/envs/mujoco/**",
"gym/envs/toy_text/**",
"gym/spaces/**",
"gym/utils/**",
"gym/vector/**",
"gym/wrappers/**",
"tests/**"
]
strict = [
]
typeCheckingMode = "basic"
pythonVersion = "3.7"
typeshedPath = "typeshed"
enableTypeIgnoreComments = true
# This is required as the CI pre-commit does not download the module (i.e. numpy, pygame, box2d)
# Therefore, we have to ignore missing imports
reportMissingImports = "none"
reportUnknownMemberType = "none"
reportUnknownParameterType = "none"
reportUnknownVariableType = "none"
reportUnknownArgumentType = "none"
reportPrivateUsage = "warning"
reportUntypedFunctionDecorator = "none"
reportMissingTypeStubs = false
verboseOutput = true
reportUnboundVariable = "warning"
reportGeneralTypeIssues ="none"