Skip to content

Commit 1e2110a

Browse files
Add stricter Ruff linting (#151)
1 parent 96644dd commit 1e2110a

12 files changed

Lines changed: 53 additions & 19 deletions

File tree

dissect/cstruct/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from dissect.cstruct.bitbuffer import BitBuffer
24
from dissect.cstruct.cstruct import cstruct, ctypes, ctypes_type
35
from dissect.cstruct.exceptions import (

dissect/cstruct/compiler.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from dissect.cstruct.bitbuffer import BitBuffer
1212
from dissect.cstruct.types import (
1313
Array,
14-
BaseType,
1514
Char,
1615
CharArray,
1716
Flag,
@@ -34,6 +33,9 @@
3433
from types import MethodType
3534

3635
from dissect.cstruct.cstruct import cstruct
36+
from dissect.cstruct.types import (
37+
BaseType,
38+
)
3739
from dissect.cstruct.types.structure import Field
3840

3941
SUPPORTED_TYPES = (

dissect/cstruct/cstruct.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,10 @@
1212
from dissect.cstruct.parser import CStyleParser, TokenParser
1313
from dissect.cstruct.types import (
1414
LEB128,
15-
Array,
1615
BaseArray,
1716
BaseType,
1817
Char,
1918
Enum,
20-
Field,
2119
Flag,
2220
Int,
2321
Packed,
@@ -32,6 +30,11 @@
3230
from collections.abc import Iterable
3331
from typing import TypeAlias
3432

33+
from dissect.cstruct.types import (
34+
Array,
35+
Field,
36+
)
37+
3538

3639
T = TypeVar("T", bound=BaseType)
3740

dissect/cstruct/exceptions.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from __future__ import annotations
2+
3+
14
class Error(Exception):
25
pass
36

dissect/cstruct/expression.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,6 @@ def is_number(self, token: str) -> bool:
225225

226226
def evaluate(self, cs: cstruct, context: dict[str, int] | None = None) -> int:
227227
"""Evaluates an expression using a Shunting-Yard implementation."""
228-
229228
self.stack = []
230229
self.queue = []
231230
operators = set(self.binary_operators.keys()) | set(self.unary_operators.keys())

dissect/cstruct/parser.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111
ParserError,
1212
)
1313
from dissect.cstruct.expression import Expression
14-
from dissect.cstruct.types import BaseArray, BaseType, Field, Structure
14+
from dissect.cstruct.types import BaseArray, Field, Structure
1515

1616
if TYPE_CHECKING:
1717
from dissect.cstruct import cstruct
18+
from dissect.cstruct.types import BaseType
1819

1920

2021
class Parser:
@@ -37,7 +38,8 @@ def parse(self, data: str) -> None:
3738

3839

3940
class TokenParser(Parser):
40-
"""
41+
"""Definition parser for C-like structure syntax.
42+
4143
Args:
4244
cs: An instance of cstruct.
4345
compiled: Whether structs should be compiled or not.
@@ -421,8 +423,7 @@ def _replacer(match: re.Match) -> str:
421423

422424
@staticmethod
423425
def _lineno(tok: Token) -> int:
424-
"""Quick and dirty line number calculator"""
425-
426+
"""Quick and dirty line number calculator."""
426427
match = tok.match
427428
return match.string.count("\n", 0, match.start()) + 1
428429

@@ -474,7 +475,7 @@ def parse(self, data: str) -> None:
474475

475476

476477
class CStyleParser(Parser):
477-
"""Definition parser for C-like structure syntax.
478+
"""Definition parser for C-like structure syntax (legacy parser).
478479
479480
Args:
480481
cs: An instance of cstruct

dissect/cstruct/types/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from dissect.cstruct.types.base import Array, BaseArray, BaseType, MetaType
24
from dissect.cstruct.types.char import Char, CharArray
35
from dissect.cstruct.types.enum import Enum

dissect/cstruct/types/enum.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
from enum import EnumMeta, IntEnum, IntFlag
66
from typing import TYPE_CHECKING, Any, BinaryIO, TypeVar, overload
77

8-
from dissect.cstruct.types.base import Array, BaseType, MetaType
8+
from dissect.cstruct.types.base import BaseType, MetaType
99

1010
if TYPE_CHECKING:
1111
from typing_extensions import Self
1212

1313
from dissect.cstruct.cstruct import cstruct
14+
from dissect.cstruct.types.base import Array
1415

1516

1617
PY_311 = sys.version_info >= (3, 11, 0)

dissect/cstruct/types/structure.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import io
44
from collections import ChainMap
5-
from collections.abc import Callable, MutableMapping
5+
from collections.abc import MutableMapping
66
from contextlib import contextmanager
77
from enum import Enum
88
from functools import lru_cache
@@ -23,7 +23,7 @@
2323
from dissect.cstruct.types.pointer import Pointer
2424

2525
if TYPE_CHECKING:
26-
from collections.abc import Iterator, Mapping
26+
from collections.abc import Callable, Iterator, Mapping
2727
from types import FunctionType
2828

2929
from typing_extensions import Self
@@ -139,7 +139,7 @@ def _update_fields(
139139

140140
if cls.__compiled__:
141141
# If the previous class was compiled try to compile this too
142-
from dissect.cstruct import compiler # noqa: PLC0415
142+
from dissect.cstruct import compiler
143143

144144
try:
145145
classdict["_read"] = compiler.Compiler(cls.cs).compile_read(fields, cls.__name__, align=cls.__align__)

pyproject.toml

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,24 +94,43 @@ select = [
9494
"SLOT",
9595
"SIM",
9696
"TID",
97-
"TCH",
97+
"TC",
9898
"PTH",
9999
"PLC",
100100
"TRY",
101101
"FLY",
102102
"PERF",
103103
"FURB",
104104
"RUF",
105+
"D"
105106
]
106-
ignore = ["E203", "B904", "UP024", "ANN002", "ANN003", "ANN204", "ANN401", "SIM105", "TRY003"]
107+
108+
ignore = [
109+
"E203", "B904", "UP024", "ANN002", "ANN003", "ANN204", "ANN401", "SIM105", "TRY003", "PLC0415",
110+
# Ignore some pydocstyle rules for now as they require a larger cleanup
111+
"D1",
112+
"D205",
113+
"D301",
114+
"D417",
115+
# Seems bugged: https://github.com/astral-sh/ruff/issues/16824
116+
"D402",
117+
]
118+
future-annotations = true
119+
120+
[tool.ruff.lint.pydocstyle]
121+
convention = "google"
122+
123+
[tool.ruff.lint.flake8-type-checking]
124+
strict = true
107125

108126
[tool.ruff.lint.per-file-ignores]
109-
"tests/**" = ["S101", "PLC0415"]
127+
"tests/**" = ["S101"]
110128
"tests/_docs/**" = ["INP001"]
111129

112130
[tool.ruff.lint.isort]
113131
known-first-party = ["dissect.cstruct"]
114132
known-third-party = ["dissect"]
133+
required-imports = ["from __future__ import annotations"]
115134

116135
[tool.setuptools.packages.find]
117136
include = ["dissect.*"]

0 commit comments

Comments
 (0)