Skip to content

Commit

Permalink
replace pyright by mypy (Lightning-AI#5021)
Browse files Browse the repository at this point in the history
* drop pyright & add mypy

* detail

* name

* fix

* flake8

* ver

Co-authored-by: Sean Naren <sean.narenthiran@gmail.com>
  • Loading branch information
Borda and SeanNaren committed Dec 9, 2020
1 parent 127454a commit 2c11d96
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 119 deletions.
93 changes: 30 additions & 63 deletions .github/workflows/code-formatting.yml
Expand Up @@ -22,88 +22,55 @@ jobs:
- name: Run isort
run: isort --settings-path=./pyproject.toml --check-only --diff .

code-black:
name: Check code formatting with Black
runs-on: ubuntu-20.04
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Set up Python 3.8
uses: actions/setup-python@v2
with:
python-version: 3.8
- name: Install Black
run: pip install black==19.10b0
- name: Run Black
run: echo "LGTM"
# run black --skip-string-normalization --config=pyproject.toml --check . # TODO, uncomment
#code-black:
# name: Check code formatting with Black
# runs-on: ubuntu-20.04
# steps:
# - name: Checkout
# uses: actions/checkout@v2
# - name: Set up Python 3.8
# uses: actions/setup-python@v2
# with:
# python-version: 3.8
# - name: Install Black
# run: pip install black==19.10b0
# - name: Run Black
# run: echo "LGTM"
# run black --skip-string-normalization --config=pyproject.toml --check . # TODO, uncomment

python-types:
name: Python static type checking with Pyright
python-pep8:
name: Python formatting PEP8
runs-on: ubuntu-20.04

# Timeout: https://stackoverflow.com/a/59076067/4521646
timeout-minutes: 15
timeout-minutes: 10
steps:
- name: Checkout
uses: actions/checkout@v2
- uses: actions/setup-python@v2
with:
python-version: 3.7

# Note: This uses an internal pip API and may not always work
# https://github.com/actions/cache/blob/master/examples.md#multiple-oss-in-a-workflow
- name: Cache pip
uses: actions/cache@v2
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-extras-${{ hashFiles('requirements.txt') }}-${{ hashFiles('requirements/extra.txt') }}
restore-keys: |
${{ runner.os }}-pip-extras-
- name: Install dependencies
env:
HOROVOD_BUILD_ARCH_FLAGS: "-mfma"
HOROVOD_WITHOUT_MXNET: 1
HOROVOD_WITHOUT_TENSORFLOW: 1
run: |
# python -m pip install --upgrade --user pip
pip install --requirement requirements.txt --upgrade-strategy only-if-needed --find-links https://download.pytorch.org/whl/cpu/torch_stable.html --quiet
pip install --requirement ./requirements/devel.txt --quiet
# pip install tox coverage
python --version ; pip --version ; pip list
shell: bash

- name: Set up node
uses: actions/setup-node@v1
with:
node-version: '12'

- name: Install pyright
run: |
npm install pyright
pip install flake8
- name: Run type checking
- name: Run checking
run: |
$(npm bin)/pyright --project .pyrightconfig.json
flake8 .
python-pep8:
name: Python formatting PEP8
python-typing-mypy:
name: Python typing check [mypy]
runs-on: ubuntu-20.04

# Timeout: https://stackoverflow.com/a/59076067/4521646
timeout-minutes: 10
steps:
- name: Checkout
uses: actions/checkout@v2
- uses: actions/checkout@master
- uses: actions/setup-python@v2
with:
python-version: 3.7

- name: Install dependencies
python-version: 3.8
- name: Install mypy
run: |
pip install flake8
- name: Run checking
pip install mypy
pip list
- name: mypy check
run: |
flake8 .
mypy
4 changes: 4 additions & 0 deletions .pre-commit-config.yaml
Expand Up @@ -30,3 +30,7 @@ repos:
args: [--settings-path, ./pyproject.toml]
language: system
types: [python]

- repo: https://github.com/pre-commit/mirrors-mypy
hooks:
- id: mypy
46 changes: 0 additions & 46 deletions .pyrightconfig.json

This file was deleted.

4 changes: 2 additions & 2 deletions pytorch_lightning/__init__.py
Expand Up @@ -47,9 +47,9 @@
# This variable is injected in the __builtins__ by the build
# process. It used to enable importing subpackages of skimage when
# the binaries are not built
__LIGHTNING_SETUP__
_ = None if __LIGHTNING_SETUP__ else None
except NameError:
__LIGHTNING_SETUP__ = False
__LIGHTNING_SETUP__: bool = False

if __LIGHTNING_SETUP__:
import sys # pragma: no-cover
Expand Down
18 changes: 10 additions & 8 deletions pytorch_lightning/setup_tools.py
Expand Up @@ -15,6 +15,7 @@
import os
import re
import warnings
from typing import Iterable, List
from urllib.error import HTTPError, URLError
from urllib.request import Request, urlopen

Expand All @@ -37,7 +38,7 @@
]


def _load_requirements(path_dir, file_name='requirements.txt', comment_char='#'):
def _load_requirements(path_dir: str , file_name: str = 'requirements.txt', comment_char: str = '#') -> List[str]:
"""Load requirements from a file
>>> _load_requirements(PROJECT_ROOT) # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
Expand All @@ -58,7 +59,7 @@ def _load_requirements(path_dir, file_name='requirements.txt', comment_char='#')
return reqs


def _parse_for_badge(text: str, path_badges: str = _PATH_BADGES, badge_names: list = _DEFAULT_BADGES):
def _parse_for_badge(text: str, path_badges: str = _PATH_BADGES, badge_names: Iterable = _DEFAULT_BADGES) -> str:
""" Returns the new parsed text with url change with local downloaded files
>>> _parse_for_badge('Some text here... ' # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
Expand Down Expand Up @@ -90,25 +91,25 @@ def _parse_for_badge(text: str, path_badges: str = _PATH_BADGES, badge_names: li
return text


def _save_file(url_badge, save, extension, headers):
def _save_file(url_badge: str, save_path: str, extension: str, headers: dict) -> None:
"""function for saving the badge either in `.png` or `.svg`"""

# because there are two badge with name `PyPI Status` the second one is download
if 'https://pepy.tech/badge/pytorch-lightning' in url_badge:
save += '_downloads'
save_path += '_downloads'

try:
req = Request(url=url_badge, headers=headers)
resp = urlopen(req)
except URLError:
warnings.warn("Error while downloading the badge", UserWarning)
else:
save += extension
with open(save, 'wb') as download_file:
save_path += extension
with open(save_path, 'wb') as download_file:
download_file.write(resp.read())


def _download_badge(url_badge, badge_name, target_dir):
def _download_badge(url_badge: str, badge_name: str, target_dir: str) -> str:
"""Download badge from url
>>> path_img = _download_badge('https://img.shields.io/pypi/pyversions/pytorch-lightning',
Expand Down Expand Up @@ -143,9 +144,10 @@ def _download_badge(url_badge, badge_name, target_dir):
url_badge = url_badge.replace('.png', '.svg')
_save_file(url_badge, save_path, extension='.svg', headers=headers)
return save_path + '.svg'
return ''


def _load_long_description(path_dir):
def _load_long_description(path_dir: str) -> str:
"""Load readme as decribtion
>>> _load_long_description(PROJECT_ROOT) # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
Expand Down
1 change: 1 addition & 0 deletions requirements/test.txt
Expand Up @@ -12,6 +12,7 @@ scikit-learn>=0.22.2
scikit-image>=0.17.2
black>=20.8b1
isort>=5.6.4
mypy>=0.720
pre-commit>=1.0

cloudpickle>=1.3
Expand Down
85 changes: 85 additions & 0 deletions setup.cfg
Expand Up @@ -93,3 +93,88 @@ convention = pep257
# D202: Ignore a blank line after docstring (collision with Python Black in decorators)
add-ignore = D104,D107,D202
max-line-length = 120


[mypy]
# Typing tests is low priority, but enabling type checking on the
# untyped test functions (using `--check-untyped-defs`) is still
# high-value because it helps test the typing.
files = pytorch_lightning, pl_examples, benchmarks, tests
disallow_untyped_defs = True
ignore_missing_imports = True

# todo: add proper typing to this module...
[mypy-pytorch_lightning.callbacks.*]
ignore_errors = True

# todo: add proper typing to this module...
[mypy-pytorch_lightning.core.*]
ignore_errors = True

# todo: add proper typing to this module...
[mypy-pytorch_lightning.accelerators.*]
ignore_errors = True

# todo: add proper typing to this module...
[mypy-pytorch_lightning.cluster_environments.*]
ignore_errors = True

# todo: add proper typing to this module...
[mypy-pytorch_lightning.loggers.*]
ignore_errors = True

# todo: add proper typing to this module...
[mypy-pytorch_lightning.logging.*]
ignore_errors = True

# todo: add proper typing to this module...
[mypy-pytorch_lightning.metrics.*]
ignore_errors = True

# todo: add proper typing to this module...
[mypy-pytorch_lightning.overrides.*]
ignore_errors = True

# todo: add proper typing to this module...
[mypy-pytorch_lightning.profiler.*]
ignore_errors = True

# todo: add proper typing to this module...
[mypy-pytorch_lightning.pt_overrides.*]
ignore_errors = True

# todo: add proper typing to this module...
[mypy-pytorch_lightning.plugins.*]
ignore_errors = True

# todo: add proper typing to this module...
[mypy-pytorch_lightning.root_module.*]
ignore_errors = True

# todo: add proper typing to this module...
[mypy-pytorch_lightning.trainer.*]
ignore_errors = True

# todo: add proper typing to this module...
[mypy-pytorch_lightning.distributed.*]
ignore_errors = True

# todo: add proper typing to this module...
[mypy-pytorch_lightning.tuner.*]
ignore_errors = True

# todo: add proper typing to this module...
[mypy-pytorch_lightning.utilities.*]
ignore_errors = True

# todo: add proper typing to this module...
[mypy-pl_examples.*]
ignore_errors = True

# todo: add proper typing to this module...
[mypy-benchmarks.*]
ignore_errors = True

# todo: add proper typing to this module...
[mypy-tests.*]
ignore_errors = True

0 comments on commit 2c11d96

Please sign in to comment.