Skip to content

Commit f1db646

Browse files
committed
Add pre-commit hooks
1 parent 9bd3076 commit f1db646

File tree

5 files changed

+107
-17
lines changed

5 files changed

+107
-17
lines changed

.flake8

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[flake8]
2+
max-line-length = 88
3+
select = C,E,F,W,B,B950
4+
extend-ignore = E203, E501

.markdownlint.yaml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Default state for all rules
2+
default: true
3+
4+
# MD013/line-length - Line length
5+
MD013:
6+
# Number of characters
7+
line_length: 200
8+
tables: false

.pre-commit-config.yaml

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
---
2+
# pre-commit is a tool to perform a predefined set of tasks manually and/or
3+
# automatically before git commits are made.
4+
#
5+
# Config reference: https://pre-commit.com/#pre-commit-configyaml---top-level
6+
#
7+
# Common tasks
8+
#
9+
# - Run on all files: pre-commit run --all-files
10+
# - Register git hooks: pre-commit install --install-hooks
11+
#
12+
# See https://pre-commit.com for more information
13+
# See https://pre-commit.com/hooks.html for more hooks
14+
repos:
15+
# Autoupdate: Python code
16+
- repo: https://github.com/asottile/pyupgrade
17+
rev: v2.34.0
18+
hooks:
19+
- id: pyupgrade
20+
args: [--py39-plus]
21+
22+
# Automatically sort python imports
23+
- repo: https://github.com/pycqa/isort
24+
rev: 5.10.1
25+
hooks:
26+
- id: isort
27+
args: [--profile, black]
28+
29+
# Autoformat: Python code
30+
- repo: https://github.com/psf/black
31+
rev: 22.6.0
32+
hooks:
33+
- id: black
34+
args: [--target-version=py39]
35+
36+
# Check python code static typing
37+
- repo: https://github.com/pre-commit/mirrors-mypy
38+
rev: v0.961
39+
hooks:
40+
- id: mypy
41+
args: [--config, ./mypy.ini]
42+
additional_dependencies:
43+
["numpy", "pytest", "requests", "types-requests", "types-tabulate"]
44+
# Unfortunately, `pre-commit` only runs on changed files
45+
# This doesn't work well with `mypy --follow-imports error`
46+
# See: https://github.com/pre-commit/mirrors-mypy/issues/34#issuecomment-1062160321
47+
#
48+
# To work around this we run `mypy` only in manual mode
49+
# So it won't run as part of `git commit` command
50+
# But it will still be run as part of `pre-commit` workflow and give expected results
51+
stages: [manual]
52+
53+
# Autoformat: YAML, JSON, Markdown, etc.
54+
- repo: https://github.com/pre-commit/mirrors-prettier
55+
rev: v2.7.1
56+
hooks:
57+
- id: prettier
58+
59+
# `pre-commit sample-config` default hooks
60+
- repo: https://github.com/pre-commit/pre-commit-hooks
61+
rev: v4.3.0
62+
hooks:
63+
- id: check-added-large-files
64+
- id: end-of-file-fixer
65+
- id: trailing-whitespace
66+
67+
# Lint: Python
68+
- repo: https://github.com/PyCQA/flake8
69+
rev: 4.0.1
70+
hooks:
71+
- id: flake8
72+
73+
# Lint: Markdown
74+
- repo: https://github.com/igorshubovych/markdownlint-cli
75+
rev: v0.31.1
76+
hooks:
77+
- id: markdownlint
78+
args: ["--fix"]

setup.py

+15-16
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,34 @@
1-
from setuptools import setup, Extension
21
import sysconfig
32

43
from Cython.Build import cythonize
4+
from setuptools import Extension, setup
55

6+
language = "c++"
7+
std = "c++17"
68

7-
language = 'c++'
8-
std = 'c++17'
9-
10-
default_compile_args = sysconfig.get_config_var('CFLAGS').split()
9+
default_compile_args = sysconfig.get_config_var("CFLAGS").split()
1110
extra_compile_args = [f"-std={std}", "-Wall", "-Wextra", "-Werror", "-DNDEBUG", "-O3"]
1211

13-
print(f'Default compile arguments: {default_compile_args}')
14-
print(f'Extra compile arguments: {extra_compile_args}')
12+
print(f"Default compile arguments: {default_compile_args}")
13+
print(f"Extra compile arguments: {extra_compile_args}")
1514

1615
extension = Extension(
17-
'cpp_python_extension',
18-
sources=['sieve_python_wrapper.cpp', 'sieve.cpp'],
16+
"cpp_python_extension",
17+
sources=["sieve_python_wrapper.cpp", "sieve.cpp"],
1918
extra_compile_args=extra_compile_args,
20-
language=language
19+
language=language,
2120
)
2221

2322
cython_extension = Extension(
24-
'cython_sieve',
25-
sources=['cython_sieve.pyx', 'sieve.cpp'],
23+
"cython_sieve",
24+
sources=["cython_sieve.pyx", "sieve.cpp"],
2625
extra_compile_args=extra_compile_args,
27-
language='c++',
26+
language="c++",
2827
)
2928

3029
setup(
31-
name='cpp_python_extension',
32-
version='1.0',
33-
description='This is Example module written in C++',
30+
name="cpp_python_extension",
31+
version="1.0",
32+
description="This is Example module written in C++",
3433
ext_modules=cythonize([extension, cython_extension]),
3534
)

test_benchmark.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
import pytest
21
import cpp_python_extension
32
import cython_sieve
3+
import pytest
44

55

66
@pytest.mark.parametrize("input", [1000, 100000, 1000000])
77
def test_cpp(benchmark, input):
88
benchmark(cpp_python_extension.sieve_of_eratosthenes, input)
99

10+
1011
@pytest.mark.parametrize("input", [1000, 100000, 1000000])
1112
def test_cyth(benchmark, input):
1213
benchmark(cython_sieve.sieve_of_eratosthenes, input)

0 commit comments

Comments
 (0)