Skip to content

Commit

Permalink
[Python] Replace global linting excludes with local line-level exclud…
Browse files Browse the repository at this point in the history
…es ("noqa")

Replace the project global linting rule excludes (as defined in .pep8) with
fine-grained "# noqa" annotations.

By using noqa annotation the excludes are made on a per line basis instead of
globally.

These annotations are used where we make deliberate deviations from the standard
linting rules.

To lint the Python code in the project:

  $ flake8

To install flake8:

  $ pip install flake8

See https://flake8.readthedocs.org/en/latest/ for details.

To enable checking of the PEP-8 naming conventions, install the optional
extension pep8-naming:

  $ pip install pep8-naming

To enable checking of blind "except:" statements, install the optional
extension flake8-blind-except:

  $ pip install flake8-blind-except

To enable checking of import statement order, install the optional
extension flake8-import-order:

  $ pip install flake8-import-order
  • Loading branch information
practicalswift committed Mar 10, 2016
1 parent 2eb227d commit d5326bf
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 22 deletions.
1 change: 0 additions & 1 deletion .pep8
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
[flake8]
filename = *.py,Benchmark_Driver,Benchmark_DTrace.in,Benchmark_GuardMalloc.in,Benchmark_RuntimeLeaksRunner.in,build-script,gyb,line-directive,ns-html2rst,recursive-lipo,rth,submit-benchmark-results,update-checkout,viewcfg
ignore = D100,D101,D102,D103,D104,D105,E402
max-line-length = 80
2 changes: 1 addition & 1 deletion benchmark/scripts/Benchmark_DTrace.in
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ DRIVER_LIBRARY_PATH = "@PATH_TO_DRIVER_LIBRARY@"
sys.path.append(DRIVER_LIBRARY_PATH)
DTRACE_PATH = os.path.join(DRIVER_LIBRARY_PATH, 'swift_stats.d')

import perf_test_driver
import perf_test_driver # noqa (E402 module level import not at top of file)

# Regexes for the XFAIL_LIST. Matches against '([Onone|O|Ounchecked],TestName)'
XFAIL_LIST = [
Expand Down
2 changes: 1 addition & 1 deletion benchmark/scripts/Benchmark_GuardMalloc.in
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import sys

sys.path.append("@PATH_TO_DRIVER_LIBRARY@")

import perf_test_driver
import perf_test_driver # noqa (E402 module level import not at top of file)

# Regexes for the XFAIL_LIST. Matches against '([Onone|O|Ounchecked],TestName)'
XFAIL_LIST = [
Expand Down
2 changes: 1 addition & 1 deletion benchmark/scripts/Benchmark_RuntimeLeaksRunner.in
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import sys

sys.path.append("@PATH_TO_DRIVER_LIBRARY@")

import perf_test_driver
import perf_test_driver # noqa (E402 module level import not at top of file)

# This is a hacked up XFAIL list. It should really be a json file, but it will
# work for now. Add in the exact name of the pass to XFAIL.
Expand Down
13 changes: 8 additions & 5 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,17 +265,20 @@
#

# Pull in the Swift lexers
from os.path import abspath, dirname, join as join_paths
from os.path import abspath, dirname, join as join_paths # noqa (E402)
sys.path = [
join_paths(dirname(dirname(abspath(__file__))), 'utils', 'pygments')
] + sys.path

import swift as swift_pygments_lexers
import swift as swift_pygments_lexers # noqa (E402 module level import not at top of file)

sys.path.pop(0)

# Monkeypatch pygments.lexers.get_lexer_by_name to return our lexers
from pygments.lexers import get_lexer_by_name as original_get_lexer_by_name
# Monkeypatch pygments.lexers.get_lexer_by_name to return our lexers. The
# ordering required to allow for monkeypatching causes the warning
# "I100 Import statements are in the wrong order." when linting using
# flake8-import-order. "noqa" is used to suppress this warning.
from pygments.lexers import get_lexer_by_name as original_get_lexer_by_name # noqa (E402)


def swift_get_lexer_by_name(_alias, *args, **kw):
Expand All @@ -286,5 +289,5 @@ def swift_get_lexer_by_name(_alias, *args, **kw):
else:
return original_get_lexer_by_name(_alias, *args, **kw)

import pygments.lexers
import pygments.lexers # noqa (I100 Import statements are in the wrong order.)
pygments.lexers.get_lexer_by_name = swift_get_lexer_by_name
19 changes: 10 additions & 9 deletions utils/build-script
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import textwrap

# FIXME: Instead of modifying the system path in order to enable imports from
# other directories, all Python modules related to the build script
# should be moved to the `swift_build_support` module.
# should be moved to the `swift_build_support` module. Remove "noqa"
# markers when this is fixed.
# For additional information, see: https://bugs.swift.org/browse/SR-237.
sys.path.append(os.path.dirname(__file__))
from SwiftBuildSupport import (
Expand All @@ -34,17 +35,17 @@ from SwiftBuildSupport import (
get_preset_options,
print_with_argv0,
quote_shell_command,
)
) # noqa (E402 module level import not at top of file)

sys.path.append(os.path.join(os.path.dirname(__file__), 'swift_build_support'))

import swift_build_support.clang
import swift_build_support.cmake
import swift_build_support.debug
from swift_build_support.migration import migrate_impl_args
import swift_build_support.ninja
import swift_build_support.tar
import swift_build_support.targets
import swift_build_support.clang # noqa (E402 module level import not at top of file)
import swift_build_support.cmake # noqa (E402)
import swift_build_support.debug # noqa (E402)
from swift_build_support.migration import migrate_impl_args # noqa (E402)
import swift_build_support.ninja # noqa (E402)
import swift_build_support.tar # noqa (E402)
import swift_build_support.targets # noqa (E402)


# Main entry point for the preset mode.
Expand Down
4 changes: 2 additions & 2 deletions utils/pass-pipeline/scripts/pipeline_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
sys.path.append(os.path.join(os.path.dirname(
os.path.dirname(os.path.abspath(__file__))), 'src'))

import pass_pipeline_library
import pass_pipeline_library # noqa (E402 module level import not at top of file)

import passes
import passes # noqa (E402)

normal_pipeline = list(pass_pipeline_library.normal_passpipelines())
pass_pipelines = [x.identifier for x in normal_pipeline]
Expand Down
2 changes: 1 addition & 1 deletion utils/pass-pipeline/scripts/pipelines_build_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
sys.path.append(os.path.join(os.path.dirname(
os.path.dirname(os.path.abspath(__file__))), 'src'))

import passes
import passes # noqa (E402 module level import not at top of file)

# TODO: This should not be hard coded.
PIPELINES = ["PreSpecialize", "HighLevel", "EarlyLoopOpt",
Expand Down
2 changes: 1 addition & 1 deletion utils/update-checkout
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ from SwiftBuildSupport import (
WorkingDirectory,
check_call,
check_output,
)
) # noqa (E402 module level import not at top of file)

REPOSITORIES = {
'llvm': 'apple/swift-llvm',
Expand Down

0 comments on commit d5326bf

Please sign in to comment.