Skip to content

Commit

Permalink
Un-ignore F403 in .flake8 (pytorch#55838)
Browse files Browse the repository at this point in the history
Summary:
Generally wildcard imports are bad for the reasons described here: https://www.flake8rules.com/rules/F403.html

This PR replaces wildcard imports with an explicit list of imported items where possible, and adds a `# noqa: F403` comment in the other cases (mostly re-exports in `__init__.py` files).

This is a prerequisite for pytorch#55816, because currently [`tools/codegen/dest/register_dispatch_key.py` simply fails if you sort its imports](https://github.com/pytorch/pytorch/actions/runs/742505908).

Pull Request resolved: pytorch#55838

Test Plan: CI. You can also run `flake8` locally.

Reviewed By: jbschlosser

Differential Revision: D27724232

Pulled By: samestep

fbshipit-source-id: 269fb09cb4168f8a51fd65bfaacc6cda7fb87c34
  • Loading branch information
samestep authored and facebook-github-bot committed Apr 13, 2021
1 parent 75eb026 commit 4753100
Show file tree
Hide file tree
Showing 64 changed files with 250 additions and 123 deletions.
2 changes: 1 addition & 1 deletion .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ max-line-length = 120
# C408 ignored because we like the dict keyword argument syntax
# E501 is not flexible enough, we're using B950 instead
ignore =
E203,E305,E402,E501,E721,E741,F403,F405,F821,F841,F999,W503,W504,C408,E302,W291,E303,
E203,E305,E402,E501,E721,E741,F405,F821,F841,F999,W503,W504,C408,E302,W291,E303,
# shebang has extra meaning in fbcode lints, so I think it's not worth trying
# to line this up with executable bit
EXE001,
Expand Down
4 changes: 2 additions & 2 deletions benchmarks/fastrnns/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .cells import *
from .factory import *
from .cells import * # noqa: F403
from .factory import * # noqa: F403

# (output, next_state) = cell(input, state)
seqLength = 100
Expand Down
7 changes: 6 additions & 1 deletion benchmarks/fastrnns/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
import torch
import torchvision.models as cnn

from .factory import *
from .factory import (dropoutlstm_creator, imagenet_cnn_creator,
layernorm_pytorch_lstm_creator, lnlstm_creator,
lstm_creator, lstm_multilayer_creator,
lstm_premul_bias_creator, lstm_premul_creator,
lstm_simple_creator, pytorch_lstm_creator,
varlen_lstm_creator, varlen_pytorch_lstm_creator)


class DisableCuDNN():
Expand Down
17 changes: 16 additions & 1 deletion test/jit/test_hooks.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
import os
import sys
import unittest
from typing import Tuple

import torch
from jit.test_hooks_modules import *
from jit.test_hooks_modules import (
ModuleDirectFowardSubmodCall, ModuleForwardSingleInput,
ModuleForwardTupleInput, create_forward_tuple_input,
create_module_forward_multiple_inputs, create_module_forward_single_input,
create_module_hook_return_nothing,
create_module_multiple_hooks_multiple_inputs,
create_module_multiple_hooks_single_input, create_module_no_forward_input,
create_module_same_hook_repeated, create_submodule_forward_multiple_inputs,
create_submodule_forward_single_input,
create_submodule_forward_single_input_return_not_tupled,
create_submodule_hook_return_nothing,
create_submodule_multiple_hooks_multiple_inputs,
create_submodule_multiple_hooks_single_input,
create_submodule_no_forward_input, create_submodule_same_hook_repeated,
create_submodule_to_call_directly_with_hooks)

# Make the helper files in test/ importable
pytorch_test_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
Expand Down
13 changes: 12 additions & 1 deletion test/jit_hooks/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,18 @@
# grab modules from test_jit_hooks.cpp
pytorch_test_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
sys.path.append(pytorch_test_dir)
from jit.test_hooks_modules import *
from jit.test_hooks_modules import (
create_forward_tuple_input, create_module_forward_multiple_inputs,
create_module_forward_single_input, create_module_hook_return_nothing,
create_module_multiple_hooks_multiple_inputs,
create_module_multiple_hooks_single_input, create_module_no_forward_input,
create_module_same_hook_repeated, create_submodule_forward_multiple_inputs,
create_submodule_forward_single_input,
create_submodule_hook_return_nothing,
create_submodule_multiple_hooks_multiple_inputs,
create_submodule_multiple_hooks_single_input,
create_submodule_same_hook_repeated,
create_submodule_to_call_directly_with_hooks)

# Create saved modules for JIT forward hooks and pre-hooks
def main():
Expand Down
4 changes: 2 additions & 2 deletions test/mobile/test_lite_script_module.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import torch
import torch.utils.bundled_inputs
from torch.utils.mobile_optimizer import *
from torch.utils.mobile_optimizer import optimize_for_mobile
import io
from typing import NamedTuple
from typing import Dict, List, NamedTuple
from collections import namedtuple

from torch.jit.mobile import _load_for_lite_interpreter, _export_operator_list
Expand Down
8 changes: 4 additions & 4 deletions test/onnx/model_defs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .squeezenet import *
from .super_resolution import *
from .op_test import *
from .srresnet import *
from .squeezenet import * # noqa: F403
from .super_resolution import * # noqa: F403
from .op_test import * # noqa: F403
from .srresnet import * # noqa: F403
2 changes: 1 addition & 1 deletion test/onnx/test_pytorch_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
pytorch_test_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
sys.path.insert(-1, pytorch_test_dir)

from torch.testing._internal.common_utils import * # noqa: F401
from torch.testing._internal.common_utils import * # noqa: F401,F403

torch.set_default_tensor_type('torch.FloatTensor')

Expand Down
2 changes: 1 addition & 1 deletion test/test_jit_fuser_legacy.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys
sys.argv.append("--jit_executor=legacy")
from test_jit_fuser import *
from test_jit_fuser import * # noqa: F403

if __name__ == '__main__':
run_tests()
2 changes: 1 addition & 1 deletion test/test_jit_legacy.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys
sys.argv.append("--jit_executor=legacy")
from test_jit import *
from test_jit import * # noqa: F403

if __name__ == '__main__':
run_tests()
Expand Down
2 changes: 1 addition & 1 deletion test/test_jit_profiling.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys
sys.argv.append("--jit_executor=profiling")
from test_jit import *
from test_jit import * # noqa: F403

if __name__ == '__main__':
run_tests()
Expand Down
2 changes: 1 addition & 1 deletion test/test_jit_simple.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys
sys.argv.append("--jit_executor=simple")
from test_jit import *
from test_jit import * # noqa: F403

if __name__ == '__main__':
run_tests()
Expand Down
4 changes: 3 additions & 1 deletion test/test_mobile_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import torch.utils.bundled_inputs
from torch.testing._internal.common_utils import TestCase, run_tests
from torch.testing._internal.jit_utils import get_forward, get_forward_graph
from torch.utils.mobile_optimizer import *
from torch.utils.mobile_optimizer import (LintCode,
generate_mobile_module_lints,
optimize_for_mobile)
from torch.nn import functional as F
from torch._C import MobileOptimizerType
from torch.testing._internal.common_quantized import override_quantized_engine
Expand Down
2 changes: 1 addition & 1 deletion tools/autograd/gen_annotated_fn_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

from tools.codegen.gen import parse_native_yaml, FileManager
from tools.codegen.context import with_native_function
from tools.codegen.model import *
from tools.codegen.model import BaseOperatorName, NativeFunction
import tools.codegen.api.python as python
from .gen_python_functions import should_generate_py_binding, is_py_torch_function, \
is_py_nn_function, is_py_linalg_function, is_py_variable_method
Expand Down
9 changes: 5 additions & 4 deletions tools/autograd/gen_autograd_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@

from typing import List, Sequence, Tuple

from tools.codegen.api.autograd import *
from tools.codegen.api.types import *
from tools.codegen.api.autograd import (Derivative, DifferentiabilityInfo,
SavedAttribute, uses_retain_variables,
uses_single_grad)
from tools.codegen.api.types import Binding
from tools.codegen.code_template import CodeTemplate
from tools.codegen.gen import FileManager
from tools.codegen.model import *
from tools.codegen.utils import *
from tools.codegen.model import Argument

FUNCTION_DECLARATION = CodeTemplate("""\
struct TORCH_API ${op} : public ${superclass} {
Expand Down
20 changes: 16 additions & 4 deletions tools/autograd/gen_python_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,24 @@

from tools.codegen.code_template import CodeTemplate
from tools.codegen.api import cpp
from tools.codegen.api.types import *
from tools.codegen.api.python import *
from tools.codegen.api.types import CppSignatureGroup
from tools.codegen.api.python import (PythonArgument, PythonSignature,
PythonSignatureDeprecated,
PythonSignatureGroup,
PythonSignatureNativeFunctionPair,
arg_parser_output_exprs,
argument_type_str, cpp_dispatch_exprs,
cpp_dispatch_target,
dispatch_lambda_args,
dispatch_lambda_exprs,
dispatch_lambda_return_str,
has_tensor_options,
namedtuple_fieldnames, signature)
from tools.codegen.gen import cpp_string, parse_native_yaml, FileManager
from tools.codegen.context import with_native_function
from tools.codegen.model import *
from tools.codegen.utils import *
from tools.codegen.model import (Argument, BaseOperatorName, NativeFunction,
Type, Variant)
from tools.codegen.utils import split_name_params

from typing import Dict, Optional, List, Tuple, Set, Sequence, Callable

Expand Down
5 changes: 3 additions & 2 deletions tools/autograd/gen_trace_type.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import itertools
from typing import Optional, List, Sequence, Union

from tools.codegen.api.types import *
from tools.codegen.api.types import CppSignatureGroup, DispatcherSignature
from tools.codegen.api import cpp
from tools.codegen.code_template import CodeTemplate
from tools.codegen.context import with_native_function
from tools.codegen.utils import mapMaybe
from tools.codegen.gen import parse_native_yaml, FileManager
from tools.codegen.model import *
from tools.codegen.model import (Argument, NativeFunction, SchemaKind,
TensorOptionsArguments)

# Note [Manual Backend kernels]
# For these ops, we want to manually register to dispatch key Backend and
Expand Down
4 changes: 2 additions & 2 deletions tools/autograd/gen_variable_factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
import re
from typing import Optional, List

from tools.codegen.api.types import *
from tools.codegen.api.types import CppSignatureGroup
from tools.codegen.api import cpp
import tools.codegen.api.python as python
from tools.codegen.gen import parse_native_yaml, FileManager
from tools.codegen.context import with_native_function
from tools.codegen.utils import mapMaybe
from tools.codegen.model import *
from tools.codegen.model import NativeFunction, TensorOptionsArguments, Variant

OPTIONAL_TYPE_PATTERN = re.compile(r"c10::optional<(.+)>")
TYPE_PATTERN = re.compile(r"(?:const\s+)?([A-Z]\w+)")
Expand Down
10 changes: 7 additions & 3 deletions tools/autograd/gen_variable_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,18 @@
ASSIGN_RETURN_VALUE, gen_formals,
)

from tools.codegen.api.types import *
from tools.codegen.api.autograd import *
from tools.codegen.api.types import Binding, DispatcherSignature
from tools.codegen.api.autograd import (
DifferentiableInput, NativeFunctionWithDifferentiabilityInfo,
SavedAttribute, dispatch_strategy, gen_differentiable_outputs,
is_differentiable)
from tools.codegen.api import cpp
from tools.codegen.code_template import CodeTemplate
from tools.codegen.context import with_native_function
from tools.codegen.gen import FileManager
from tools.codegen.utils import mapMaybe
from tools.codegen.model import *
from tools.codegen.model import (Argument, NativeFunction, SchemaKind,
SelfArgument, TensorOptionsArguments)
from typing import Callable, List, Optional, Sequence, Union

# We don't set or modify grad_fn on these methods. Generally, they return
Expand Down
9 changes: 5 additions & 4 deletions tools/autograd/load_derivatives.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
from typing import Sequence, Any, Tuple, List, Set, Dict, Match, Optional
import yaml

from tools.codegen.api.autograd import *
from tools.codegen.api.types import *
from tools.codegen.api.autograd import (Derivative, DifferentiabilityInfo,
SavedAttribute)
from tools.codegen.api.types import Binding, CppSignatureGroup
from tools.codegen.api import cpp
from tools.codegen.gen import parse_native_yaml
from tools.codegen.context import with_native_function
from tools.codegen.model import *
from tools.codegen.utils import *
from tools.codegen.model import FunctionSchema, NativeFunction
from tools.codegen.utils import IDENT_REGEX, split_name_params

try:
# use faster C loader if available
Expand Down
4 changes: 2 additions & 2 deletions tools/codegen/api/autograd.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from typing import Optional, Sequence, List, Tuple

from tools.codegen.api import cpp
from tools.codegen.api.types import *
from tools.codegen.model import *
from tools.codegen.api.types import Binding
from tools.codegen.model import NativeFunction, Type, SchemaKind
from tools.codegen.utils import IDENT_REGEX

# Represents a saved attribute involved in backward calculation.
Expand Down
9 changes: 7 additions & 2 deletions tools/codegen/api/cpp.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
from tools.codegen.model import *
from tools.codegen.api.types import *
from tools.codegen.model import (Argument, Arguments, BaseTy, BaseType,
FunctionSchema, ListType, NativeFunction,
OptionalType, Return, SelfArgument,
TensorOptionsArguments, Type, assert_never)
from tools.codegen.api.types import (ArgName, BaseCType, Binding,
ConstRefCType, CType, MutRefCType,
OptionalCType, SpecialArgName)
from typing import Optional, Sequence, Union, List, Set

# This file describes the translation of JIT schema to the public C++
Expand Down
6 changes: 4 additions & 2 deletions tools/codegen/api/dispatcher.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from tools.codegen.model import *
from tools.codegen.model import (Argument, FunctionSchema, Return,
SelfArgument, TensorOptionsArguments, Type,
assert_never)

from tools.codegen.api.types import *
from tools.codegen.api.types import ArgName, Binding, CType
from tools.codegen.api import cpp

import itertools
Expand Down
3 changes: 1 addition & 2 deletions tools/codegen/api/meta.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from tools.codegen.model import *
from tools.codegen.api.types import *
from tools.codegen.model import NativeFunctionsGroup

# Follows dispatcher calling convention, but:
# - Mutable arguments not allowed. Meta functions are always
Expand Down
8 changes: 6 additions & 2 deletions tools/codegen/api/native.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
from tools.codegen.model import *
from tools.codegen.model import (Argument, FunctionSchema, Return,
SelfArgument, TensorOptionsArguments, Type,
assert_never)

from tools.codegen.api.types import *
from tools.codegen.api.types import (ArgName, BaseCType, Binding,
ConstRefCType, CType, MutRefCType,
OptionalCType)
from tools.codegen.api import cpp

from typing import Union, Sequence, List, Optional
Expand Down
6 changes: 4 additions & 2 deletions tools/codegen/api/python.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from dataclasses import dataclass
from typing import Optional, Union, Sequence, Set, List, Dict, Tuple

from tools.codegen.api.types import *
from tools.codegen.api.types import Binding, CppSignature, CppSignatureGroup
from tools.codegen.api import cpp
from tools.codegen.gen import pythonify_default
from tools.codegen.model import *
from tools.codegen.model import (Argument, BaseTy, BaseType, ListType,
NativeFunction, OptionalType, Return, Type,
Variant)

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
#
Expand Down
8 changes: 6 additions & 2 deletions tools/codegen/api/structured.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
from tools.codegen.model import *
from tools.codegen.model import (Argument, BaseTy, BaseType, ListType,
NativeFunctionsGroup, OptionalType,
SelfArgument, TensorOptionsArguments, Type,
assert_never)

from tools.codegen.api.types import *
from tools.codegen.api.types import (ArgName, BaseCType, Binding,
ConstRefCType, CType, OptionalCType)
from tools.codegen.api import cpp

from typing import Union, List
Expand Down
4 changes: 3 additions & 1 deletion tools/codegen/api/translate.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from typing import Dict, Sequence, List, NoReturn, Union
from tools.codegen.api.types import *
from tools.codegen.api.types import (BaseCType, Binding, ConstRefCType, CType,
Expr, MutRefCType, OptionalCType,
SpecialArgName)

# This file implements a small program synthesis engine that implements
# conversions between one API to another.
Expand Down
3 changes: 2 additions & 1 deletion tools/codegen/api/types.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from tools.codegen.model import *
from tools.codegen.model import (Argument, FunctionSchema, NativeFunction,
SelfArgument, TensorOptionsArguments)
from dataclasses import dataclass
from typing import Optional, Union, Sequence, TypeVar, List, Set
from enum import Enum
Expand Down
4 changes: 2 additions & 2 deletions tools/codegen/context.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from tools.codegen.utils import *
from tools.codegen.model import *
from tools.codegen.utils import S, T, context
from tools.codegen.model import NativeFunction, NativeFunctionsGroup
import tools.codegen.local as local

import functools
Expand Down
8 changes: 4 additions & 4 deletions tools/codegen/dest/native_functions.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from typing import List, Union, Set, Any

from tools.codegen.context import *
from tools.codegen.utils import *
from tools.codegen.model import *
from tools.codegen.api.types import *
from tools.codegen.context import with_native_function
from tools.codegen.utils import concatMap
from tools.codegen.model import (NativeFunction, NativeFunctionsGroup,
is_structured_dispatch_key)
import tools.codegen.api.meta as meta
import tools.codegen.api.native as native
import tools.codegen.api.structured as structured
Expand Down
Loading

0 comments on commit 4753100

Please sign in to comment.