Skip to content

Commit

Permalink
Merge pull request #332 from asi1024/mypy-ignore
Browse files Browse the repository at this point in the history
Add 'mypy: ignore-errors' in untyped files
  • Loading branch information
kmaehashi authored Sep 10, 2021
2 parents 09eb3a4 + aa84107 commit 342161b
Show file tree
Hide file tree
Showing 33 changed files with 72 additions and 11 deletions.
5 changes: 4 additions & 1 deletion .flexci/linux/unittest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@ MASTER_ADDR=127.0.0.1 MASTER_PORT=1236 mpirun -n 2 --allow-run-as-root python ex
python example/mnist_custom_logic.py --batch-size 2048 --test-batch-size 2048 --epochs 1

# Run pysen
pysen generate .
pysen run lint 2> /output/pysen.txt || true

# Run flake8
pysen generate .
flake8 .

# Run mypy
mypy pytorch_pfn_extras

mv htmlcov /output/htmlcov
4 changes: 2 additions & 2 deletions pytorch_pfn_extras/config_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import optuna


def optuna_types(trial: 'optuna.trial.Trial'):
def optuna_types(trial: 'optuna.trial.Trial') -> Dict[str, Any]:
types = {
"optuna_suggest_categorical": trial.suggest_categorical,
"optuna_suggest_discrete_uniform": trial.suggest_discrete_uniform,
Expand All @@ -25,7 +25,7 @@ def load_path_with_optuna_types(
trial: 'optuna.trial.Trial',
loader: Optional[config.Loader] = None,
types: Optional[Dict[str, Callable[..., Any]]] = None,
):
) -> config.Config:
if types is None:
types = {}
for key, value in optuna_types(trial).items():
Expand Down
2 changes: 2 additions & 0 deletions pytorch_pfn_extras/dataloaders/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# mypy: ignore-errors

import torch


Expand Down
2 changes: 2 additions & 0 deletions pytorch_pfn_extras/engine.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# mypy: ignore-errors

from typing import (
Any, Callable, Dict, List, Optional, Tuple, Type, Union, TYPE_CHECKING
)
Expand Down
4 changes: 2 additions & 2 deletions pytorch_pfn_extras/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ def train_step(
batch (torch.Tensor, list of torch.Tensor, dict of torch.Tensor):
Input tensors feeded to the model of the current step.
"""
with torch_autocast(enabled=self._autocast): # type: ignore[no-untyped-call] # NOQA
with torch_autocast(enabled=self._autocast):
optimizers[self.model_name].zero_grad()
outs = self._forward(models[self.model_name], batch)
to_back_outs = outs
Expand Down Expand Up @@ -374,7 +374,7 @@ def __init__(
self._logic = logic
self.consume_options(options)

def consume_options(self, options):
def consume_options(self, options: Dict[str, Any]) -> None:
"""A method to update options of Handler.
Note that the given dict will be modified.
Expand Down
9 changes: 6 additions & 3 deletions pytorch_pfn_extras/nn/parallel/distributed.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ def _reduce(
# flatten values to improve the runtime perfomance of all-reduce
coalesced = torch.empty(size, device=values[0].device,
dtype=values[0].dtype)
coalesced_views = get_foreach_wrapper().unflatten(coalesced, values)
coalesced_views = get_foreach_wrapper().unflatten( # type: ignore[no-untyped-call]
coalesced, values)
get_foreach_wrapper().multi_tensor_scale(values, coalesced_views, 1.0)

with record(
Expand All @@ -103,12 +104,14 @@ def _broadcast(
group: Optional[dist.ProcessGroup]
) -> None:
with torch.no_grad(): # type: ignore[no-untyped-call]
coalesced = get_foreach_wrapper().flatten(values)
coalesced = get_foreach_wrapper().flatten( # type: ignore[no-untyped-call]
values)
with record(
"torch.distributed.broadcast", use_cuda=torch.cuda.is_available()
):
dist.broadcast(coalesced, 0, group=group) # type: ignore[no-untyped-call]
src = get_foreach_wrapper().unflatten(coalesced, values)
src = get_foreach_wrapper().unflatten( # type: ignore[no-untyped-call]
coalesced, values)
get_foreach_wrapper().multi_tensor_scale(src, values, 1.0)


Expand Down
2 changes: 2 additions & 0 deletions pytorch_pfn_extras/profiler/_time_summary.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# mypy: ignore-errors

import atexit
from contextlib import contextmanager
import os
Expand Down
5 changes: 2 additions & 3 deletions pytorch_pfn_extras/reporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,10 +310,9 @@ def make_statistics(self) -> Tuple[Scalar, Scalar]:
mean = x / n
var = self._x2 / n - mean * mean
if isinstance(var, torch.Tensor):
std = torch.sqrt(var)
return mean, torch.sqrt(var)
else:
std = numpy.sqrt(var)
return mean, std
return mean, numpy.sqrt(var)

def state_dict(self) -> Dict[str, Any]:
state = {}
Expand Down
2 changes: 2 additions & 0 deletions pytorch_pfn_extras/runtime/_runtime.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# mypy: ignore-errors

from typing import (
Any, Dict, Generator, Iterable, List, Optional, Tuple, Union, TYPE_CHECKING
)
Expand Down
2 changes: 2 additions & 0 deletions pytorch_pfn_extras/training/_evaluator.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# mypy: ignore-errors

import contextlib
import queue
from typing import (
Expand Down
2 changes: 2 additions & 0 deletions pytorch_pfn_extras/training/_trainer.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# mypy: ignore-errors

import queue
import time
from typing import Optional
Expand Down
2 changes: 2 additions & 0 deletions pytorch_pfn_extras/training/_transform_model.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# mypy: ignore-errors

from torch.nn.parallel import DistributedDataParallel

from pytorch_pfn_extras.nn.parallel import (
Expand Down
2 changes: 2 additions & 0 deletions pytorch_pfn_extras/training/extensions/_snapshot.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# mypy: ignore-errors

import os

import torch
Expand Down
2 changes: 2 additions & 0 deletions pytorch_pfn_extras/training/extensions/fail_on_non_number.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# mypy: ignore-errors

import torch

from pytorch_pfn_extras.training import extension
Expand Down
2 changes: 2 additions & 0 deletions pytorch_pfn_extras/training/extensions/log_report.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# mypy: ignore-errors

import collections
import json

Expand Down
2 changes: 2 additions & 0 deletions pytorch_pfn_extras/training/extensions/lr_scheduler.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# mypy: ignore-errors

from pytorch_pfn_extras.training import extension
from pytorch_pfn_extras.training import trigger as trigger_module
from torch.optim.lr_scheduler import ReduceLROnPlateau
Expand Down
2 changes: 2 additions & 0 deletions pytorch_pfn_extras/training/extensions/micro_average.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# mypy: ignore-errors

from pytorch_pfn_extras import reporting
from pytorch_pfn_extras.training import extension
from pytorch_pfn_extras.training import trigger as trigger_module
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# mypy: ignore-errors

import torch

from pytorch_pfn_extras import reporting
Expand Down
2 changes: 2 additions & 0 deletions pytorch_pfn_extras/training/extensions/plot_report.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# mypy: ignore-errors

import json
import warnings

Expand Down
2 changes: 2 additions & 0 deletions pytorch_pfn_extras/training/extensions/print_report.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# mypy: ignore-errors

from copy import deepcopy
import os
import sys
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# mypy: ignore-errors

import sys
from typing import Any, IO, List, Optional, Union

Expand Down
2 changes: 2 additions & 0 deletions pytorch_pfn_extras/training/extensions/profile_report.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# mypy: ignore-errors

import json
from collections import OrderedDict

Expand Down
2 changes: 2 additions & 0 deletions pytorch_pfn_extras/training/extensions/progress_bar.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# mypy: ignore-errors

import datetime
import sys

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# mypy: ignore-errors

import datetime
import sys
import time
Expand Down
2 changes: 2 additions & 0 deletions pytorch_pfn_extras/training/extensions/value_observation.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# mypy: ignore-errors

from pytorch_pfn_extras.training import extension


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# mypy: ignore-errors

import warnings

import numpy
Expand Down
2 changes: 2 additions & 0 deletions pytorch_pfn_extras/training/manager.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# mypy: ignore-errors

import collections
import contextlib
import copy
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# mypy: ignore-errors

import operator
from typing import Tuple, TYPE_CHECKING
import warnings
Expand Down
2 changes: 2 additions & 0 deletions pytorch_pfn_extras/training/triggers/interval_trigger.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# mypy: ignore-errors

from typing import Tuple, TYPE_CHECKING

from pytorch_pfn_extras.training import trigger
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# mypy: ignore-errors

from typing import List, Union, TYPE_CHECKING

from pytorch_pfn_extras.training import trigger
Expand Down
2 changes: 2 additions & 0 deletions pytorch_pfn_extras/training/triggers/minmax_value_trigger.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# mypy: ignore-errors

from typing import Any, Callable, Dict, Optional, TYPE_CHECKING

from pytorch_pfn_extras import reporting
Expand Down
2 changes: 2 additions & 0 deletions pytorch_pfn_extras/training/triggers/once_trigger.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# mypy: ignore-errors

from typing import Any, Dict, TYPE_CHECKING

from pytorch_pfn_extras.training import trigger
Expand Down
2 changes: 2 additions & 0 deletions pytorch_pfn_extras/utils/comparer.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# mypy: ignore-errors

import threading
import concurrent.futures

Expand Down

0 comments on commit 342161b

Please sign in to comment.