動作確認
通常実行
src/sample.py
import os
class Sample:
def __init__(self, x):
self.x = x
# flake8 src
src/sample.py:1:1: F401 'os' imported but unused
src/sample.py:3:1: E302 expected 2 blank lines, found 1
# flake8 --show-source src
src/sample.py:1:1: F401 'os' imported but unused
import os
^
src/sample.py:3:1: E302 expected 2 blank lines, found 1
class Sample:
^
Configファイルを指定した実行
tox.ini
[flake8]
ignore = F401
src/sample.py
import os
class Sample:
def __init__(self, x):
self.x = x
# flake8 --config=tox.ini src
src/sample.py:3:1: E302 expected 2 blank lines, found 1
max-line-length
tox.ini
[flake8]
max-line-length = 59
src/sample.py
class Sample:
def __init__(self, x):
self.x = x
self.y = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
# flake8 --config=tox.ini src
src/sample.py:4:60: E501 line too long (60 > 59 characters)
max-line-length + ignore E501
tox.ini
[flake8]
ignore = E501
max-line-length = 59
src/sample.py
class Sample:
def __init__(self, x):
self.x = x
self.y = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
# flake8 --config=tox.ini src
ライブラリはインストールされている必要はない
# pip freeze
flake8==3.8.3
mccabe==0.6.1
mypy==0.782
mypy-extensions==0.4.3
pycodestyle==2.6.0
pyflakes==2.2.0
typed-ast==1.4.1
typing-extensions==3.7.4.3
src/sample.py
import requests
class Sample:
def fetch_json(self, url: str) -> dict:
response = requests.get(url)
return response.json()
tox.ini
[flake8]
max-line-length = 59
# flake8 --config=tox.ini src
ディレクトリごとに異なる設定にする
src/sample.py
import requests
class Sample:
def fetch_json(self, url: str) -> dict:
response = requests.get(url)
return response.json()
src/tests/test_sample.py
from unittest import TestCase, main
from unittest.mock import MagicMock, call, patch
from src.sample import Sample
class TestSample(TestCase):
def mocked_requests_get(*args, **kwargs):
class MockResponse:
def __init__(self, json_data, status_code):
self.json_data = json_data
self.status_code = status_code
def json(self):
return self.json_data
if args[0] == 'http://example.com/test.json':
return MockResponse({'key1': 'value1'}, 200)
elif args[0] == 'http://example.com/another_test.json':
return MockResponse({'key2': 'value2'}, 200)
return MockResponse(None, 404)
@patch('requests.get', side_effect=mocked_requests_get)
def test_fetch_json(self, mock_get: MagicMock):
sample = Sample()
json_data = sample.fetch_json('http://example.com/test.json')
self.assertEqual(json_data, {'key1': 'value1'})
json_data = sample.fetch_json('http://example.com/another_test.json')
self.assertEqual(json_data, {'key2': 'value2'})
json_data = sample.fetch_json('http://no_example.com/test.json')
self.assertIsNone(json_data)
self.assertIn(
call('http://example.com/test.json'), mock_get.call_args_list
)
self.assertIn(
call('http://example.com/another_test.json'), mock_get.call_args_list
)
self.assertEqual(len(mock_get.call_args_list), 3)
if __name__ == '__main__':
main()
tox.ini
[flake8]
max-line-length = 79
# flake8 --config=tox.ini src
src/tests/test_sample.py:6:1: E302 expected 2 blank lines, found 1
src/tests/test_sample.py:38:80: E501 line too long (81 > 79 characters)
tox.ini
[flake8]
max-line-length = 79
per-file-ignores =
src/tests/*:E302,E501
# flake8 --config=tox.ini src
クラス外部から非パブリックメソッドにアクセスしていないことを確認する
src/sample.py
import requests
class Sample:
def fetch_json(self, url: str) -> dict:
response = self._get_response(url)
return response.json()
def _get_response(self, url: str) -> requests.models.Response:
return requests.get(url)
調べた限りだとできない
参考