Skip to content

Commit

Permalink
Overhaul linting
Browse files Browse the repository at this point in the history
  • Loading branch information
nickdrozd committed Jan 7, 2025
1 parent 38870cb commit 52a43d5
Show file tree
Hide file tree
Showing 40 changed files with 237 additions and 199 deletions.
6 changes: 0 additions & 6 deletions .ruff.toml

This file was deleted.

2 changes: 0 additions & 2 deletions branch.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import sys

from test.test_turing import branch_last

from tm.machine import Machine


if __name__ == '__main__':
for prog in sys.stdin:
for i, branch in enumerate(branch_last(prog)):
Expand Down
1 change: 0 additions & 1 deletion c_prog.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from tools.c import make_c


if __name__ == '__main__':
for PROG in sys.stdin:
print(
Expand Down
11 changes: 7 additions & 4 deletions dump_graphs.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import sys
import shlex
import tempfile
import subprocess
import sys
import tempfile

from tools.dot import make_dot

Expand All @@ -12,5 +12,8 @@
with open(path, 'w') as temp:
temp.write(make_dot(prog))

subprocess.call(shlex.split(
f'dot {path} -T png -o {prog.replace(" ", "_")}.png'))
subprocess.call( # noqa: S603
shlex.split(
f'dot {path} -T png -o {prog.replace(" ", "_")}.png'
)
)
3 changes: 1 addition & 2 deletions dump_macros.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import sys

from tm.macro import show_comp, MacroProg
from tm.machine import Machine

from tm.macro import MacroProg, show_comp
from tools.normalize import normalize

if __name__ == '__main__':
Expand Down
3 changes: 2 additions & 1 deletion instr_seq.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
from tm.show import show_slot
from tools.instr_seq import instr_seq


def format_sequence(prog: str) -> None:
print(f' "{prog.strip()}": {{')

for partial, step, slot in instr_seq(prog):
print(
" \"{}\": ({:2d}, '{}'),".format(
" \"{}\": ({:2d}, '{}'),".format( # noqa: UP032
partial,
step,
show_slot(slot),
Expand Down
6 changes: 2 additions & 4 deletions just_so.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import sys
import argparse
import sys

from tm.reason import (
cant_halt,
cant_blank,
cant_halt,
cant_spin_out,

segment_cant_halt,
)


CYCLES = 2_000
CANT_REACH = cant_spin_out

Expand Down
4 changes: 2 additions & 2 deletions normalize.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import sys
import argparse
import sys

from tools.normalize import normalize, expand
from tools.normalize import expand, normalize
from tools.tree_norm import tree_norm

if __name__ == '__main__':
Expand Down
7 changes: 3 additions & 4 deletions perf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
from __future__ import annotations

import sys

from functools import reduce
from typing import TYPE_CHECKING

from test.utils import read_holdouts # noqa: F401
from test.utils import read_holdouts

if TYPE_CHECKING:
from collections.abc import Callable
Expand All @@ -17,7 +16,7 @@

def profile(function: Null) -> Null:
def wrapper() -> None:
import yappi # type: ignore
import yappi # type: ignore[import-not-found]

yappi.set_clock_type('cpu')
yappi.start()
Expand All @@ -36,7 +35,7 @@ def wrapper() -> None:
def get_holdouts() -> list[str]:
return sorted(
reduce(
lambda acc, cat: acc | read_holdouts(cat), # type: ignore
lambda acc, cat: acc | read_holdouts(cat), # type: ignore[arg-type, operator, return-value]
('32q', '23q', '42h', '42q', '24h'),
set(),
)
Expand Down
4 changes: 2 additions & 2 deletions perf/algebra.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from perf import profile
from test.prog_data import ALGEBRA
from tm.machine import Machine
from perf import profile


@profile
def main() -> None:
for data in ALGEBRA.values():
for prog in data.keys():
for prog in data:
print(prog)
_ = Machine(prog, opt_macro = 2_000).run()

Expand Down
43 changes: 43 additions & 0 deletions ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
line-length = 72

[lint]
select = ["ALL"]

ignore = [
"ANN204", # none annotation for __init__
"COM812", # missing trailing commas
"D", # docstrings
"E741", # ambiguous variable name
"EM", # exception strings
"ERA001", # commented-out code
"FBT001", # boolean positional arg
"FBT002", # boolean default arg
"N", # naming
"PLR2004", # magic value comparison
"PTH123", # Path.open
"PYI", # pyi / typing
"Q000", # double quotes
"RUF018", # assertion assignment
"S101", # assertions
"SIM300", # yoda comparison
"T201", # print
"TRY003", # exception messages
]

[lint.per-file-ignores]
"test/**" = [
"ANN", # missing annotations
"E501", # line too long
"PT009", # unittest assertions
"PT027", # unittest assert raise
]

[lint.pylint]
max-args = 7 # c
max-locals = 19 # machine
max-returns = 7 # prover
max-branches = 23 # num
max-statements = 70 # machine

[lint.mccabe]
max-complexity = 26 # num
17 changes: 12 additions & 5 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -37,31 +37,38 @@ paths_to_mutate = tm/tape.py

[pylint.'MESSAGES CONTROL']
disable =
format,
too-complex,
invalid-name,
unused-import,
duplicate-code,
wildcard-import,
compare-to-zero,
global-statement,
no-name-in-module,
consider-using-in,
wrong-import-order,
raise-missing-from,
redefined-loop-name,
unspecified-encoding,
used-before-assignment,
unused-wildcard-import,
too-few-public-methods,
magic-value-comparison,
missing-class-docstring,
too-many-public-methods,
consider-using-f-string,
redefined-variable-type,
missing-module-docstring,
too-many-return-statements,
missing-function-docstring,
misplaced-comparison-constant,

[pylint.FORMAT]
max-line-length = 72
good-names = maxDiff

[pylint.DESIGN]
max-args = 7 # c
max-locals = 19 # machine
max-returns = 7 # prover
max-branches = 23 # num
max-complexity = 22 # machine
max-attributes = 14 # machine
max-statements = 70 # machine

Expand Down
23 changes: 13 additions & 10 deletions test/lin_rec.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
from __future__ import annotations

from copy import copy
from typing import TYPE_CHECKING
from collections import defaultdict
from copy import copy
from dataclasses import dataclass, field
from typing import TYPE_CHECKING

from tm.parse import tcompile
from tm.rust_stuff import TermRes, MachineResult
from tm.rust_stuff import MachineResult, TermRes

if TYPE_CHECKING:
from typing import Self

from tm.parse import Color, Shift, State, Slot, CompProg
from tm.machine import Undfnd
from tm.parse import Color, CompProg, Shift, Slot, State

RecRes = tuple[int, int]
LinRec = tuple[int | None, int]
Expand Down Expand Up @@ -128,9 +128,12 @@ def __init__(

def __str__(self) -> str:
return ' '.join(
list(map(str, reversed(self.lspan)))
+ [f'[{self.scan} ({self.head})]']
+ list(map(str, self.rspan)))
[
*list(map(str, reversed(self.lspan))),
f"[{self.scan} ({self.head})]",
*list(map(str, self.rspan))
]
)

def copy(self) -> HeadTape:
return HeadTape(
Expand Down Expand Up @@ -425,7 +428,7 @@ def run(
step: int = 0
state: State = 0

for cycle in range(sim_lim or 1_000_000):
for cycle in range(sim_lim or 1_000_000): # noqa: B007
slot: Slot = state, tape.scan

if step >= check_rec:
Expand Down Expand Up @@ -498,7 +501,7 @@ def run(
step: int = 0
state: State = 0

for cycle in range(sim_lim or 1_000_000):
for cycle in range(sim_lim or 1_000_000): # noqa: B007
slot: Slot = state, tape.scan

if step in self.history.tapes:
Expand All @@ -513,7 +516,7 @@ def run(

color, shift, next_state = instr

step += tape.step(shift, color, False)
step += tape.step(shift, color, skip = False)

state = next_state

Expand Down
2 changes: 1 addition & 1 deletion test/prog_data.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# pylint: disable = line-too-long, too-many-lines, consider-using-namedtuple-or-dataclass
# pylint: disable = consider-using-namedtuple-or-dataclass

## test turing #######################################################

Expand Down
2 changes: 0 additions & 2 deletions test/test_coverage.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# pylint: disable = line-too-long

from unittest import TestCase

from tm.machine import Machine
Expand Down
12 changes: 5 additions & 7 deletions test/test_graph.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from unittest import TestCase

from test.prog_data import GRAPHS, KERNEL, MODULAR, SPAGHETTI
from test.utils import read_progs
from test.prog_data import GRAPHS, SPAGHETTI, KERNEL, MODULAR

from tools.graph import Graph
from tm.show import show_state
from tm.rust_stuff import py_is_connected as is_connected
from tm.show import show_state
from tools.graph import Graph


class TestGraph(TestCase):
Expand Down Expand Up @@ -59,7 +58,6 @@ def assert_exit_points(self, exits: dict[str, set[str]]):
)

def test_graph(self):
# pylint: disable = line-too-long
for prog, (flat, norm, conn, irr, zrefl, entries, exits) in GRAPHS.items():
self.graph = Graph(prog)

Expand All @@ -74,10 +72,10 @@ def test_graph(self):
self.assert_entry_points(entries)
self.assert_exit_points(exits)

self.assertTrue((
self.assertTrue(
self.graph.zero_reflexive_states
<= self.graph.reflexive_states
))
)

if len(self.graph.states) == 2:
self.assertTrue(
Expand Down
2 changes: 0 additions & 2 deletions test/test_holdouts.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
from unittest import TestCase

from test.utils import read_holdouts

from tm.machine import quick_term_or_rec


STEPS = 1_000

HOLDOUT_COUNTS = {
Expand Down
11 changes: 4 additions & 7 deletions test/test_lin_rado.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
from __future__ import annotations

from unittest import TestCase

import re
from itertools import product
from typing import TYPE_CHECKING
from unittest import TestCase

from test.utils import read_progs
from test.lin_rec import StrictLinRecMachine

from tm.show import show_state
from test.utils import read_progs
from tm.machine import quick_term_or_rec
from tm.show import show_state

if TYPE_CHECKING:
from collections.abc import Iterator
Expand Down Expand Up @@ -44,8 +42,7 @@ def run_lin_rado(
self.progs_strict, self.progs_loose = set(), set()

for prog in yield_programs(states, colors, bool(halt), rejects):
# pylint: disable = redefined-loop-name
prog = prog.replace('1R_', '...')
prog = prog.replace('1R_', '...') # noqa: PLW2901

if not quick_term_or_rec(prog, loose):
self.progs_loose.add(prog)
Expand Down
Loading

0 comments on commit 52a43d5

Please sign in to comment.