Skip to content

Commit 0af1e57

Browse files
authored
Introduce another set of flake8 extensions (apache#174)
1 parent e9ab442 commit 0af1e57

28 files changed

Lines changed: 154 additions & 63 deletions

File tree

.flake8

Lines changed: 0 additions & 25 deletions
This file was deleted.

Makefile

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
1616

1717
VERSION ?= latest
1818

19+
# determine host platform
20+
VENV_DIR = venv
21+
VENV = $(VENV_DIR)/bin
22+
ifeq (win32,$(shell python3 -c "import sys; print(sys.platform)"))
23+
VENV=$(VENV_DIR)/Scripts
24+
endif
25+
1926
.PHONY: license
2027

2128
setup:
@@ -29,14 +36,26 @@ gen:
2936
python3 -m grpc_tools.protoc --version || python3 -m pip install grpcio-tools
3037
python3 tools/codegen.py
3138

32-
# flake8 configurations should go to the file .flake8
39+
# flake8 configurations should go to the file setup.cfg
3340
lint: clean
34-
flake8 --version || python3 -m pip install flake8 flake8-quotes flake8-use-fstring
41+
python3 -m pip install -r requirements-style.txt
3542
flake8 .
3643

37-
dev-check:
38-
flake8 --version || python3 -m pip install flake8 flake8-quotes flake8-use-fstring
39-
flake8 .
44+
# used in development
45+
dev-setup:
46+
$(VENV)/python -m pip install -r requirements-style.txt
47+
48+
dev-check: dev-setup
49+
$(VENV)/flake8 .
50+
51+
# fix problems described in CodingStyle.md - verify outcome with extra care
52+
dev-fix: dev-setup
53+
$(VENV)/isort .
54+
$(VENV)/unify -r --in-place .
55+
$(VENV)/flynt -tc -v .
56+
57+
doc-gen:
58+
$(VENV)/python tools/doc/plugin_doc_gen.py
4059

4160
license: clean
4261
python3 tools/check-license-header.py skywalking tests tools

docs/en/contribution/CodingStyle.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Since Python 3.5 is end of life, we fully utilize the clarity and performance bo
66
Please do not use other styles - `+`, `%` or `.format` unless f-string is absolutely unfeasible in the context, or
77
it is a logger message, which is [optimized](https://docs.python.org/3/howto/logging.html#optimization) for the `%` style
88

9-
Run [flynt](https://github.com/ikamensh/flynt) to convert other formats to f-string, pay **extra care** to possible corner
9+
Run `make dev-fix` to invoke [flynt](https://github.com/ikamensh/flynt) to convert other formats to f-string, pay **extra care** to possible corner
1010
cases leading to a semantically different conversion.
1111

1212
### Quotes
@@ -23,7 +23,7 @@ foo = f"I'm a string"
2323
bar = f"This repo is called 'skywalking-python'"
2424
```
2525

26-
Run [unify](https://github.com/myint/unify) `unify -r your_target --in-place` to fix your quotes if flake8 complaints about it.
26+
Run `make dev-fix` to invoke [unify](https://github.com/myint/unify) to deal with your quotes if flake8 complaints about it.
2727

2828
## Debug messages
2929
Please import the `logger_debug_enabled` variable and wrap your debug messages with a check.
@@ -33,4 +33,17 @@ This should be done for all performance critical components.
3333
```python
3434
if logger_debug_enabled:
3535
logger.debug('Message - %s', some_func())
36-
```
36+
```
37+
38+
# Imports
39+
Please make sure the imports are placed in a good order, or flake8-isort will notify you of the violations.
40+
41+
Run `make dev-fix` to automatically fix the sorting problem.
42+
43+
# Naming
44+
In PEP8 convention, we are required to use snake_case as the accepted style.
45+
46+
However, there are special cases. For example, you are overriding/monkey-patching a method which happens to use the old style camelCase naming,
47+
then it is acceptable to have the original naming convention to preserve context.
48+
49+
Please mark the line with `# noqa` to avoid linting.

requirements-style.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
flake8 # Plain flake8
2+
3+
## AUTO FIXERS
4+
#isort # import sorter
5+
unify # unify quotes
6+
flynt # fix f-strings
7+
8+
## CODING STYLE
9+
flake8-quotes # checks for inconsistent single/double quotes
10+
flake8-use-fstring # enforce usage of f-strings
11+
pep8-naming # check pep8 naming convention
12+
Darglint # docstring description matches the definition
13+
flake8-eradicate # dead code checker (commented)
14+
#flake8-isort # isort checker | disabled temporarily
15+
flake8-docstrings # docstring checker
16+
17+
## DESIGN CHECKER
18+
flake8-bugbear # bug checker
19+
flake8-comprehensions # misuse of list comprehension
20+
21+
## REGRESSION CHECKER
22+
flake8-2020 # misuse of version_check in Py3.10 or future 4

requirements.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
pytest
2-
unify
3-
flynt
42
grpcio_tools==1.41.0
53
wrapt==1.13.2
64
requests==2.26.0

setup.cfg

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
[flake8]
18+
19+
# run with `--disable-noqa` to reevaluate skipped inspections
20+
# Do not use ignore option, it will override some plugin's default ignore
21+
extend-ignore =
22+
E303, # Too many blank lines | Conflict with Pycharm
23+
E126, # Continuation line over-indented for hanging indent | Conflict with Pycharm
24+
E501, # Line too long | Customized by max-line-length
25+
W503, # Line break occurred before a binary operator | Conflict with W504
26+
# temporarily banned rules for various reasons, likely to re-enable in the future
27+
E800, # Found commented out code | Temporarily disabled for legacy code
28+
D, # docstring | To be enhanced, large number
29+
DAR, # docstring structure | To be fixed
30+
31+
32+
max-line-length = 120
33+
max-complexity = 20
34+
exclude = venv*,*egg_info,skywalking/protocol
35+
count = True
36+
show-source = True
37+
statistics = True
38+
docstring-convention = google
39+
40+
[bandit]
41+
exclude = tests,docs
42+
tests: B101
43+
44+
[darglint]
45+
docstring_style = google
46+
47+
[isort]
48+
line_length = 120
49+
multi_line_output = 11

skywalking/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,6 @@ def is_exit(self):
7878

7979
class Log(object):
8080

81-
def __init__(self, timestamp: time = time.time(), items: List[LogItem] = None):
81+
def __init__(self, timestamp: time = time.time(), items: List[LogItem] = None): # noqa
8282
self.timestamp = timestamp
8383
self.items = items or []

skywalking/log/formatter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def __init__(self, fmt, tb_limit):
2727
logging.Formatter.__init__(self, fmt)
2828
self.tb_limit = tb_limit
2929

30-
def formatException(self, ei):
30+
def formatException(self, ei): # noqa
3131
sio = io.StringIO()
3232
tb = ei[2]
3333
traceback.print_exception(ei[0], ei[1], tb, self.tb_limit, sio)

skywalking/loggings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from skywalking import config
2121

2222

23-
def getLogger(name=None):
23+
def getLogger(name=None): # noqa
2424
logger = logging.getLogger(name)
2525
ch = logging.StreamHandler()
2626
formatter = logging.Formatter('%(name)s [%(threadName)s] [%(levelname)s] %(message)s')

skywalking/plugins/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def install():
3636
disable_patterns = [re.compile(p.strip()) for p in disable_patterns.split(',') if p.strip()]
3737
else:
3838
disable_patterns = [re.compile(p.strip()) for p in disable_patterns if p.strip()]
39-
for importer, modname, ispkg in pkgutil.iter_modules(skywalking.plugins.__path__):
39+
for importer, modname, _ispkg in pkgutil.iter_modules(skywalking.plugins.__path__):
4040
if any(pattern.match(modname) for pattern in disable_patterns):
4141
logger.info("plugin %s is disabled and thus won't be installed", modname)
4242
continue
@@ -50,7 +50,7 @@ def install():
5050
"won't be installed", modname)
5151
continue
5252

53-
if not hasattr(plugin, 'install') or inspect.ismethod(getattr(plugin, 'install')):
53+
if not hasattr(plugin, 'install') or inspect.ismethod(plugin.install):
5454
logger.warning("no `install` method in plugin %s, thus the plugin won't be installed", modname)
5555
continue
5656

0 commit comments

Comments
 (0)