Skip to content

Commit

Permalink
Adds type checks via pyright to CI (#2477)
Browse files Browse the repository at this point in the history
* Adds type checks via `pyright` to CI

* Move developer instructions into CONTRIBUTING.md

* black formatting

* Explicit list of strictly checked files

Co-authored-by: Ilya Kamen <[email protected]>
  • Loading branch information
ikamensh and ikamensh authored Nov 17, 2021
1 parent 01b4519 commit 590f250
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 13 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/lint_python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,22 @@ jobs:
- run: pytest . || true
- run: pytest --doctest-modules . || true
- run: shopt -s globstar && pyupgrade --py36-plus **/*.py || true

pyright:
name: Check types with pyright
runs-on: ubuntu-latest
strategy:
matrix:
python-platform: [ "Linux", "Windows", "Darwin" ]
python-version: [ "3.6", "3.7", "3.8", "3.9" ]
fail-fast: false
env:
PYRIGHT_VERSION: 1.1.183
steps:
- uses: actions/checkout@v2
- 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.
16 changes: 16 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,19 @@ If you wish to make a Gym environment, follow the instructions in [Creating Envi


Edit July 27, 2021: Please see https://github.com/openai/gym/issues/2259 for new contributing standards

# Development
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.

### 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.

32 changes: 19 additions & 13 deletions gym/logger.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sys
import warnings
from typing import Optional, Type

from gym.utils import colorize

Expand All @@ -9,42 +10,47 @@
ERROR = 40
DISABLED = 50

MIN_LEVEL = 30
min_level = 30


def set_level(level):
def set_level(level: int) -> None:
"""
Set logging threshold on current logger.
"""
global MIN_LEVEL
MIN_LEVEL = level
global min_level
min_level = level


def debug(msg, *args):
if MIN_LEVEL <= DEBUG:
def debug(msg: str, *args: object):
if min_level <= DEBUG:
print(f"DEBUG: {msg % args}", file=sys.stderr)


def info(msg, *args):
if MIN_LEVEL <= INFO:
def info(msg: str, *args: object):
if min_level <= INFO:
print(f"INFO: {msg % args}", file=sys.stderr)


def warn(msg, *args, category=None, stacklevel=1):
if MIN_LEVEL <= WARN:
def warn(
msg: str,
*args: object,
category: Optional[Type[Warning]] = None,
stacklevel: int = 1,
):
if min_level <= WARN:
warnings.warn(
colorize(f"WARN: {msg % args}", "yellow"),
category=category,
stacklevel=stacklevel + 1,
)


def deprecation(msg, *args):
def deprecation(msg: str, *args: object):
warn(msg, *args, category=DeprecationWarning, stacklevel=2)


def error(msg, *args):
if MIN_LEVEL <= ERROR:
def error(msg: str, *args: object):
if min_level <= ERROR:
print(colorize(f"ERROR: {msg % args}", "red"), file=sys.stderr)


Expand Down
12 changes: 12 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[tool.pyright]

include = ["gym/version.py", "gym/logger.py"]
exclude = [
"**/node_modules",
"**/__pycache__",
]
strict = ["gym/version.py", "gym/logger.py"]

reportMissingImports = true
reportMissingTypeStubs = false
verboseOutput = true

0 comments on commit 590f250

Please sign in to comment.