8000 pre-commit: Add codespell and other checks (#263) · wolfmib/pyscript_tutorial@fadb4a6 · GitHub
[go: up one dir, main page]

Skip to content

Commit fadb4a6

Browse files
authored
pre-commit: Add codespell and other checks (pyscript#263)
1 parent 990b0b2 commit fadb4a6

File tree

9 files changed

+69
-28
lines changed

9 files changed

+69
-28
lines changed

.pre-commit-config.yaml

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,59 @@ repos:
66
- repo: https://github.com/pre-commit/pre-commit-hooks
77
rev: v4.2.0
88
hooks:
9+
- id: check-builtin-literals
10+
- id: check-case-conflict
11+
- id: check-docstring-first
12+
- id: check-toml
13+
- id: check-xml
14+
- id: check-yaml
15+
- id: detect-private-key
916
- id: end-of-file-fixer
1017
exclude: \.min\.js$
1118
- id: trailing-whitespace
1219
- id: check-json
1320
exclude: tsconfig.json
1421
- id: check-yaml
15-
- repo: https://github.com/macisamuele/language-formatters-pre-commit-hooks
16-
rev: v2.3.0
22+
23+
- repo: https://github.com/PyCQA/bandit
24+
rev: 1.7.4
1725
hooks:
18-
- id: pretty-format-yaml
19-
args: [--autofix, --indent, '4']
26+
- id: bandit
27+
args:
28+
- --skip=B201
29+
2030
- repo: https://github.com/psf/black
2131
rev: 22.3.0
2232
hooks:
2333
- id: black
34+
35+
- repo: https://github.com/codespell-project/codespell
36+
rev: v2.1.0
37+
hooks:
38+
- id: codespell # See 'setup.cfg' for args
39+
40+
- repo: https://gitlab.com/pycqa/flake8
41+
rev: 3.9.2
42+
hooks:
43+
- id: flake8 # See 'setup.cfg' for args
44+
additional_dependencies: [flake8-bugbear, flake8-comprehensions]
45+
2446
- repo: https://github.com/pycqa/isort
2547
rev: 5.10.1
2648
hooks:
2749
- id: isort
2850
name: isort (python)
2951
args: [--profile, black]
52+
53+
- repo: https://github.com/macisamuele/language-formatters-pre-commit-hooks
54+
rev: v2.3.0
55+
hooks:
56+
- id: pretty-format-yaml
57+
args: [--autofix, --indent, '4']
58+
59+
- repo: https://github.com/asottile/pyupgrade
60+
rev: v2.32.1
61+
hooks:
62+
- id: pyupgrade
63+
args:
64+
- --py310-plus

pyscriptjs/examples/fractals.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from typing import Tuple
2-
31
import numpy as np
42
from numpy.polynomial import Polynomial
53

@@ -14,7 +12,7 @@ def mandelbrot(
1412
max_iterations: int = 100
1513
) -> np.array:
1614
"""
17- From https://www.learnpythonwithrune.org/numpy-compute-mandelbrot-set-by-vectorization/.
15+
https://www.learnpythonwithrune.org/numpy-compute-mandelbrot-set-by-vectorization
1816
"""
1917
# To make navigation easier we calculate these values
2018
x_width, y_height = 1.5, 1.5 * height / width
@@ -56,7 +54,7 @@ def julia(
5654
max_iterations: int = 100
5755
) -> np.array:
5856
"""
59-
From https://www.learnpythonwithrune.org/numpy-calculate-the-julia-set-with-vectorization/.
57+
https://www.learnpythonwithrune.org/numpy-calculate-the-julia-set-with-vectorization
6058
"""
6159
# To make navigation easier we calculate these values
6260
x_width, y_height = 1.5, 1.5 * height / width
@@ -84,7 +82,7 @@ def julia(
8482
return div_time
8583

8684

87-
Range = Tuple[float, float]
85+
Range = tuple[float, float]
8886

8987

9088
def newton(
@@ -96,7 +94,7 @@ def newton(
9694
xr: Range = (-2.5, 1),
9795
yr: Range = (-1, 1),
9896
max_iterations: int = 100
99-
) -> (np.array, np.array):
97+
) -> tuple[np.array, np.array]:
10098
""" """
10199
# To make navigation easier we calculate these values
102100
x_from, x_to = xr

pyscriptjs/examples/handtrack/lib/handtrack.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyscriptjs/examples/micrograd_ai.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ <h1>Micrograd - A tiny Autograd engine (with a bite! :))</h1><br>
4242
<p>
4343
Currently the <code>&gt;</code> symbol is being imported incorrectly as <code>&ampgt;</code> into the REPL's.
4444
In this app the <code>&gt;</code> symbol has been replaced with <code>().__gt__()</code> so you can run the code
45-
without issue. Ex: intead of <code>a &gt; b</code>, you will see <code>(a).__gt__(b)</code> instead. <br>
45+
without issue. Ex: instead of <code>a &gt; b</code>, you will 10000 see <code>(a).__gt__(b)</code> instead. <br>
4646
</p>
4747
<p>
4848
<py-script>import js; js.document.getElementById('python-status').innerHTML = 'Python is now ready. You may proceed.'</py-script>

pyscriptjs/examples/micrograd_ai.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
# cell
1010
from micrograd.engine import Value
11-
from micrograd.nn import MLP, Layer, Neuron
11+
from micrograd.nn import MLP
1212

1313
print_statements = []
1414

@@ -43,7 +43,8 @@ def micrograd_demo(*args, **kwargs):
4343
random.seed(1337)
4444

4545
# cell
46-
# An adaptation of sklearn's make_moons function https://scikit-learn.org/stable/modules/generated/sklearn.datasets.make_moons.html
46+
# An adaptation of sklearn's make_moons function
47+
# https://scikit-learn.org/stable/modules/generated/sklearn.datasets.make_moons.html
4748
def make_moons(n_samples=100, noise=None):
4849
n_samples_out, n_samples_in = n_samples, n_samples
4950

@@ -102,7 +103,7 @@ def loss(batch_size=None):
102103
data_loss = sum(losses) * (1.0 / len(losses))
103104
# L2 regularization
104105
alpha = 1e-4
105-
reg_loss = alpha * sum((p * p for p in model.parameters()))
106+
reg_loss = alpha * sum(p * p for p in model.parameters())
106107
total_loss = data_loss + reg_loss
107108

108109
# also get accuracy
@@ -120,7 +121,7 @@ def loss(batch_size=None):
120121
for k in range(20): # was 100
121122

122123
# forward
123-
total_loss, acc = loss()
124+
total_loss, _ = loss()
124125

125126
# backward
126< 10000 /code>127
model.zero_grad()
@@ -146,7 +147,7 @@ def loss(batch_size=None):
146147
Z = np.array([(s.data).__gt__(0) for s in scores])
147148
Z = Z.reshape(xx.shape)
148149

149-
fig = plt.figure()
150+
_ = plt.figure()
150151
plt.contourf(xx, yy, Z, cmap=plt.cm.Spectral, alpha=0.8)
151152
plt.scatter(X[:, 0], X[:, 1], c=y, s=40, cmap=plt.cm.Spectral)
152153
plt.xlim(xx.min(), xx.max())

pyscriptjs/examples/todo.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from datetime import datetime as dt
22

3-
from js import console
43
from utils import add_class, remove_class
54

65
tasks = []
@@ -27,7 +26,8 @@ def add_task(*ags, **kws):
2726

2827
tasks.append(task)
2928

30-
# add the task element to the page as new node in the list by cloning from a template
29+
# add the task element to the page as new node in the list by cloning from a
30+
# template
3131
task_html = task_template.clone(task_id, to=task_list)
3232
task_html_content = task_html.select("p")
3333
task_html_content.element.innerText = task["content"]

pyscriptjs/src/components/pybutton.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export class PyButton extends BaseEvalElement {
1616
if (this.hasAttribute('label')) {
1717
this.label = this.getAttribute('label');
1818
}
19-
19+
2020
// Styling does the same thing as class in normal HTML. Using the name "class" makes the style to malfunction
2121
if (this.hasAttribute('styling')) {
2222
let klass = this.getAttribute('styling');
@@ -25,7 +25,7 @@ export class PyButton extends BaseEvalElement {
2525
}else{
2626
klass = klass.trim()
2727
const newClassArray = klass.split(' ');
28-
// trim each element to remove unecessary spaces which makes the button style to malfunction
28+
// trim each element to remove unnecessary spaces which makes the button style to malfunction
2929
this.class = (() => {const concatenatedString = []; for (let i = 0; i < newClassArray.length; i++) {if (newClassArray[i].trim() !== '')(concatenatedString.push(newClassArray[i].trim()));} return concatenatedString;})();
3030
}
3131
}

pyscriptjs/src/pyscript.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
import sys
55
import time
66

7-
import micropip
8-
from js import console, document, setInterval, setTimeout
7+
import micropip # noqa: F401
8+
from js import console, document
99

1010
loop = asyncio.get_event_loop()
1111

@@ -72,10 +72,9 @@ def format_mime(obj):
7272

7373
mimebundle = eval_formatter(obj, "_repr_mimebundle_")
7474
if isinstance(mimebundle, tuple):
75-
format_dict, md_dict = mimebundle
75+
format_dict, _ = mimebundle
7676
else:
7777
format_dict = mimebundle
78-
md_dict = {}
7978

8079
output, not_available = None, []
8180
for method, mime_type in reversed(MIME_METHODS.items()):
@@ -130,7 +129,7 @@ def write(element_id, value, append=False, exec_id=0):
130129

131130
@staticmethod
132131
def run_until_complete(f):
133-
p = loop.run_until_complete(f)
132+
_ = loop.run_until_complete(f)
134133

135134

136135
class Element:
@@ -311,7 +310,6 @@ def data(self):
311310
return [c.data for c in self._children]
312311

313312
def render_children(self):
314-
out = []
315313
binds = {}
316314
for i, c in enumerate(self._children):
317315
txt = c.element.innerHTML
@@ -328,7 +326,7 @@ def foo(evt):
328326
srcEl.element.onclick()
329327
evtEl.classList = srcEl.element.classList
330328

331-
for new_id, old_id in binds.items():
329+
for new_id in binds:
332330
Element(new_id).element.onclick = foo
333331

334332
def connect(self):

setup.cfg

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[codespell]
2+
skip = pyscriptjs/node_modules/*,*.js,*.json
3+
4+
[flake8]
5+
builtins=Element,PyItemTemplate,PyListTemplate,pyscript
6+
max-complexity = 10
7+
max-line-length = 100
8+
show-source = True
9+
statistics = True

0 commit comments

Comments
 (0)
0