Skip to content

Commit 61018d4

Browse files
chore(internal): enable more lint rules (#217)
1 parent 14c8df0 commit 61018d4

File tree

9 files changed

+40
-28
lines changed

9 files changed

+40
-28
lines changed

pyproject.toml

+20-11
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,18 @@ Repository = "https://github.com/Finch-API/finch-api-python"
4545

4646
[tool.rye]
4747
managed = true
48+
# version pins are in requirements-dev.lock
4849
dev-dependencies = [
49-
"pyright==1.1.332",
50-
"mypy==1.7.1",
51-
"black==23.3.0",
52-
"respx==0.20.2",
53-
"pytest==7.1.1",
54-
"pytest-asyncio==0.21.1",
55-
"ruff==0.0.282",
56-
"isort==5.10.1",
57-
"time-machine==2.9.0",
58-
"nox==2023.4.22",
50+
"pyright",
51+
"mypy",
52+
"black",
53+
"respx",
54+
"pytest",
55+
"pytest-asyncio",
56+
"ruff",
57+
"isort",
58+
"time-machine",
59+
"nox",
5960
"dirty-equals>=0.6.0",
6061

6162
]
@@ -132,9 +133,11 @@ extra_standard_library = ["typing_extensions"]
132133

133134
[tool.ruff]
134135
line-length = 120
135-
format = "grouped"
136+
output-format = "grouped"
136137
target-version = "py37"
137138
select = [
139+
# bugbear rules
140+
"B",
138141
# remove unused imports
139142
"F401",
140143
# bare except statements
@@ -145,6 +148,12 @@ select = [
145148
"T201",
146149
"T203",
147150
]
151+
ignore = [
152+
# lru_cache in methods, will be fixed separately
153+
"B019",
154+
# mutable defaults
155+
"B006",
156+
]
148157
unfixable = [
149158
# disable auto fix for print statements
150159
"T201",

requirements-dev.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pytest-asyncio==0.21.1
4343
python-dateutil==2.8.2
4444
pytz==2023.3.post1
4545
respx==0.20.2
46-
ruff==0.0.282
46+
ruff==0.1.7
4747
six==1.16.0
4848
sniffio==1.3.0
4949
time-machine==2.9.0

src/finch/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
for __name in __all__:
7676
if not __name.startswith("__"):
7777
try:
78-
setattr(__locals[__name], "__module__", "finch")
78+
__locals[__name].__module__ = "finch"
7979
except (TypeError, AttributeError):
8080
# Some of our exported symbols are builtins which we can't set attributes for.
8181
pass

src/finch/_streaming.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def __stream__(self) -> Iterator[ResponseT]:
5151
yield process_data(data=sse.json(), cast_to=cast_to, response=response)
5252

5353
# Ensure the entire stream is consumed
54-
for sse in iterator:
54+
for _sse in iterator:
5555
...
5656

5757

@@ -94,7 +94,7 @@ async def __stream__(self) -> AsyncIterator[ResponseT]:
9494
yield process_data(data=sse.json(), cast_to=cast_to, response=response)
9595

9696
# Ensure the entire stream is consumed
97-
async for sse in iterator:
97+
async for _sse in iterator:
9898
...
9999

100100

src/finch/_types.py

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444

4545

4646
class BinaryResponseContent(ABC):
47+
@abstractmethod
4748
def __init__(
4849
self,
4950
response: Any,

src/finch/_utils/_utils.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,8 @@ def extract_type_arg(typ: type, index: int) -> type:
194194
args = get_args(typ)
195195
try:
196196
return cast(type, args[index])
197-
except IndexError:
198-
raise RuntimeError(f"Expected type {typ} to have a type argument at index {index} but it did not")
197+
except IndexError as err:
198+
raise RuntimeError(f"Expected type {typ} to have a type argument at index {index} but it did not") from err
199199

200200

201201
def deepcopy_minimal(item: _T) -> _T:
@@ -275,7 +275,9 @@ def wrapper(*args: object, **kwargs: object) -> object:
275275
try:
276276
given_params.add(positional[i])
277277
except IndexError:
278-
raise TypeError(f"{func.__name__}() takes {len(positional)} argument(s) but {len(args)} were given")
278+
raise TypeError(
279+
f"{func.__name__}() takes {len(positional)} argument(s) but {len(args)} were given"
280+
) from None
279281

280282
for key in kwargs.keys():
281283
given_params.add(key)

src/finch/resources/webhooks.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ def verify_signature(
5656

5757
try:
5858
parsedSecret = base64.b64decode(secret)
59-
except Exception:
60-
raise ValueError("Bad secret")
59+
except Exception as err:
60+
raise ValueError("Bad secret") from err
6161

6262
msg_id = get_required_header(headers, "finch-event-id")
6363
msg_timestamp = get_required_header(headers, "finch-timestamp")
@@ -68,8 +68,8 @@ def verify_signature(
6868

6969
try:
7070
timestamp = datetime.fromtimestamp(float(msg_timestamp), tz=timezone.utc)
71-
except Exception:
72-
raise ValueError("Invalid timestamp header: " + msg_timestamp + ". Could not convert to timestamp")
71+
except Exception as err:
72+
raise ValueError("Invalid timestamp header: " + msg_timestamp + ". Could not convert to timestamp") from err
7373

7474
# too old
7575
if timestamp < (now - webhook_tolerance):
@@ -152,8 +152,8 @@ def verify_signature(
152152

153153
try:
154154
parsedSecret = base64.b64decode(secret)
155-
except Exception:
156-
raise ValueError("Bad secret")
155+
except Exception as err:
156+
raise ValueError("Bad secret") from err
157157

158158
msg_id = get_required_header(headers, "finch-event-id")
159159
msg_timestamp = get_required_header(headers, "finch-timestamp")
@@ -164,8 +164,8 @@ def verify_signature(
164164

165165
try:
166166
timestamp = datetime.fromtimestamp(float(msg_timestamp), tz=timezone.utc)
167-
except Exception:
168-
raise ValueError("Invalid timestamp header: " + msg_timestamp + ". Could not convert to timestamp")
167+
except Exception as err:
168+
raise ValueError("Invalid timestamp header: " + msg_timestamp + ". Could not convert to timestamp") from err
169169

170170
# too old
171171
if timestamp < (now - webhook_tolerance):

tests/test_utils/test_proxy.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ def test_recursive_proxy() -> None:
1919
assert repr(proxy) == "RecursiveLazyProxy"
2020
assert str(proxy) == "RecursiveLazyProxy"
2121
assert dir(proxy) == []
22-
assert getattr(type(proxy), "__name__") == "RecursiveLazyProxy"
22+
assert type(proxy).__name__ == "RecursiveLazyProxy"
2323
assert type(operator.attrgetter("name.foo.bar.baz")(proxy)).__name__ == "RecursiveLazyProxy"

tests/utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def assert_matches_type(
9191
traceback.print_exc()
9292
continue
9393

94-
assert False, "Did not match any variants"
94+
raise AssertionError("Did not match any variants")
9595
elif issubclass(origin, BaseModel):
9696
assert isinstance(value, type_)
9797
assert assert_matches_model(type_, cast(Any, value), path=path)

0 commit comments

Comments
 (0)