Skip to content

Commit caa2f31

Browse files
committed
Disable some rules and fix minor linting issues
1 parent f31a574 commit caa2f31

8 files changed

Lines changed: 81 additions & 51 deletions

File tree

minfraud/models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def __eq__(self, other: object) -> bool:
1919
def __ne__(self, other: object) -> bool:
2020
return not self.__eq__(other)
2121

22-
def to_dict(self) -> dict:
22+
def to_dict(self) -> dict: # noqa: C901
2323
"""Return a dict of the object suitable for serialization."""
2424
result = {}
2525
for key, value in self.__dict__.items():
@@ -96,7 +96,7 @@ class GeoIP2Location(geoip2.records.Location):
9696
`RFC 3339 <https://tools.ietf.org/html/rfc3339>`_. For instance, the
9797
local time in Boston might be returned as 2015-04-27T19:17:24-04:00."""
9898

99-
def __init__(self, *args, **kwargs) -> None:
99+
def __init__(self, *args, **kwargs) -> None: # noqa: ANN002
100100
"""Initialize a GeoIP2Location instance."""
101101
self.local_time = kwargs.get("local_time")
102102
super().__init__(*args, **kwargs)

minfraud/request.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,10 @@
266266
}
267267

268268

269-
def prepare_report(request: dict[str, Any], validate: bool) -> dict[str, Any]:
269+
def prepare_report(
270+
request: dict[str, Any],
271+
validate: bool, # noqa: FBT001
272+
) -> dict[str, Any]:
270273
"""Validate and prepare minFraud report."""
271274
cleaned_request = _copy_and_clean(request)
272275
if validate:
@@ -280,8 +283,8 @@ def prepare_report(request: dict[str, Any], validate: bool) -> dict[str, Any]:
280283

281284
def prepare_transaction(
282285
request: dict[str, Any],
283-
validate: bool,
284-
hash_email: bool,
286+
validate: bool, # noqa: FBT001
287+
hash_email: bool, # noqa: FBT001
285288
) -> dict[str, Any]:
286289
"""Validate and prepare minFraud transaction."""
287290
cleaned_request = _copy_and_clean(request)
@@ -340,7 +343,7 @@ def maybe_hash_email(transaction: dict[str, Any]) -> None:
340343
if domain != "" and "domain" not in email:
341344
email["domain"] = domain
342345

343-
email["address"] = hashlib.md5(address.encode("UTF-8")).hexdigest()
346+
email["address"] = hashlib.md5(address.encode("UTF-8")).hexdigest() # noqa: S324
344347

345348

346349
def _clean_domain(domain: str) -> str:

minfraud/validation.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import urllib.parse
1515
import uuid
1616
from decimal import Decimal
17-
from typing import Optional
1817

1918
from email_validator import validate_email
2019
from voluptuous import (

minfraud/webservice.py

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ def _exception_for_4xx_status(
164164
)
165165
return HTTPError(
166166
"Error response contains JSON but it does not "
167-
+ f"specify code or error keys: {raw_body}",
167+
f"specify code or error keys: {raw_body}",
168168
status,
169169
uri,
170170
raw_body,
@@ -232,7 +232,7 @@ class AsyncClient(BaseClient):
232232
_existing_session: aiohttp.ClientSession
233233
_proxy: str | None
234234

235-
def __init__(
235+
def __init__( # noqa: PLR0913
236236
self,
237237
account_id: int,
238238
license_key: str,
@@ -267,8 +267,8 @@ def __init__(
267267
async def factors(
268268
self,
269269
transaction: dict[str, Any],
270-
validate: bool = True,
271-
hash_email: bool = False,
270+
validate: bool = True, # noqa: FBT001, FBT002
271+
hash_email: bool = False, # noqa: FBT001, FBT002
272272
) -> Factors:
273273
"""Query Factors endpoint with transaction data.
274274
@@ -306,8 +306,8 @@ async def factors(
306306
async def insights(
307307
self,
308308
transaction: dict[str, Any],
309-
validate: bool = True,
310-
hash_email: bool = False,
309+
validate: bool = True, # noqa: FBT001, FBT002
310+
hash_email: bool = False, # noqa: FBT001, FBT002
311311
) -> Insights:
312312
"""Query Insights endpoint with transaction data.
313313
@@ -345,8 +345,8 @@ async def insights(
345345
async def score(
346346
self,
347347
transaction: dict[str, Any],
348-
validate: bool = True,
349-
hash_email: bool = False,
348+
validate: bool = True, # noqa: FBT001, FBT002
349+
hash_email: bool = False, # noqa: FBT001, FBT002
350350
) -> Score:
351351
"""Query Score endpoint with transaction data.
352352
@@ -384,7 +384,7 @@ async def score(
384384
async def report(
385385
self,
386386
report: dict[str, str | None],
387-
validate: bool = True,
387+
validate: bool = True, # noqa: FBT001, FBT002
388388
) -> None:
389389
"""Send a transaction report to the Report Transaction endpoint.
390390
@@ -418,8 +418,8 @@ async def _response_for(
418418
uri: str,
419419
model_class: Callable,
420420
request: dict[str, Any],
421-
validate: bool,
422-
hash_email: bool,
421+
validate: bool, # noqa: FBT001
422+
hash_email: bool, # noqa: FBT001
423423
) -> Score | Factors | Insights:
424424
"""Send request and create response object."""
425425
prepared_request = prepare_transaction(request, validate, hash_email)
@@ -476,7 +476,7 @@ class Client(BaseClient):
476476
_proxies: dict[str, str] | None
477477
_session: requests.Session
478478

479-
def __init__(
479+
def __init__( # noqa: PLR0913
480480
self,
481481
account_id: int,
482482
license_key: str,
@@ -525,8 +525,8 @@ def __init__(
525525
def factors(
526526
self,
527527
transaction: dict[str, Any],
528-
validate: bool = True,
529-
hash_email: bool = False,
528+
validate: bool = True, # noqa: FBT001, FBT002
529+
hash_email: bool = False, # noqa: FBT001, FBT002
530530
) -> Factors:
531531
"""Query Factors endpoint with transaction data.
532532
@@ -564,8 +564,8 @@ def factors(
564564
def insights(
565565
self,
566566
transaction: dict[str, Any],
567-
validate: bool = True,
568-
hash_email: bool = False,
567+
validate: bool = True, # noqa: FBT001, FBT002
568+
hash_email: bool = False, # noqa: FBT001, FBT002
569569
) -> Insights:
570570
"""Query Insights endpoint with transaction data.
571571
@@ -603,8 +603,8 @@ def insights(
603603
def score(
604604
self,
605605
transaction: dict[str, Any],
606-
validate: bool = True,
607-
hash_email: bool = False,
606+
validate: bool = True, # noqa: FBT001, FBT002
607+
hash_email: bool = False, # noqa: FBT001, FBT002
608608
) -> Score:
609609
"""Query Score endpoint with transaction data.
610610
@@ -639,7 +639,11 @@ def score(
639639
),
640640
)
641641

642-
def report(self, report: dict[str, str | None], validate: bool = True) -> None:
642+
def report(
643+
self,
644+
report: dict[str, str | None],
645+
validate: bool = True, # noqa: FBT001, FBT002
646+
) -> None:
643647
"""Send a transaction report to the Report Transaction endpoint.
644648
645649
:param report: A dictionary containing the transaction report to be sent
@@ -672,8 +676,8 @@ def _response_for(
672676
uri: str,
673677
model_class: Callable,
674678
request: dict[str, Any],
675-
validate: bool,
676-
hash_email: bool,
679+
validate: bool, # noqa: FBT001
680+
hash_email: bool, # noqa: FBT001
677681
) -> Score | Factors | Insights:
678682
"""Send request and create response object."""
679683
prepared_request = prepare_transaction(request, validate, hash_email)

pyproject.toml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,18 @@ ignore = [
6464
# Skip type annotation on **_
6565
"ANN003",
6666

67+
# Redundant as the formatter handles missing trailing commas.
68+
"COM812",
69+
6770
# documenting magic methods
6871
"D105",
6972

73+
# Conflicts with D211
74+
"D203",
75+
76+
# Conflicts with D212
77+
"D213",
78+
7079
# Magic numbers for HTTP status codes seem ok most of the time.
7180
"PLR2004",
7281

@@ -80,7 +89,7 @@ ignorelist = ["id"]
8089

8190
[tool.ruff.lint.per-file-ignores]
8291
"docs/*" = ["ALL"]
83-
"minfraud/models,.py" = [ "D107", "PLR0913" ]
92+
"minfraud/models.py" = [ "PLR0913" ]
8493
"tests/*" = ["ANN201", "D"]
8594

8695
[tool.setuptools.package-data]

tests/test_models.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,13 @@ def test_ip_address(self) -> None:
150150
risk_reasons=[
151151
{
152152
"code": "ANONYMOUS_IP",
153-
"reason": "The IP address belongs to an anonymous network. See /ip_address/traits for more details.",
153+
"reason": "The IP address belongs to an anonymous network. "
154+
"See /ip_address/traits for more details.",
154155
},
155156
{
156157
"code": "MINFRAUD_NETWORK_ACTIVITY",
157-
"reason": "Suspicious activity has been seen on this IP address across minFraud customers.",
158+
"reason": "Suspicious activity has been seen on this IP address "
159+
"across minFraud customers.",
158160
},
159161
],
160162
traits={
@@ -187,13 +189,15 @@ def test_ip_address(self) -> None:
187189

188190
self.assertEqual("ANONYMOUS_IP", address.risk_reasons[0].code)
189191
self.assertEqual(
190-
"The IP address belongs to an anonymous network. See /ip_address/traits for more details.",
192+
"The IP address belongs to an anonymous network. "
193+
"See /ip_address/traits for more details.",
191194
address.risk_reasons[0].reason,
192195
)
193196

194197
self.assertEqual("MINFRAUD_NETWORK_ACTIVITY", address.risk_reasons[1].code)
195198
self.assertEqual(
196-
"Suspicious activity has been seen on this IP address across minFraud customers.",
199+
"Suspicious activity has been seen on this IP address "
200+
"across minFraud customers.",
197201
address.risk_reasons[1].reason,
198202
)
199203

tests/test_validation.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,21 @@ def check_transaction(self, transaction: dict[str, Any]) -> None:
2929
except MultipleInvalid as e:
3030
self.fail(f"MultipleInvalid {e.msg} thrown for {transaction}")
3131

32-
def check_transaction_str_type(self, object: str, key: str) -> None:
33-
self.check_transaction({object: {key: "string"}})
34-
self.check_invalid_transaction({object: {key: 12}})
32+
def check_transaction_str_type(self, obj: str, key: str) -> None:
33+
self.check_transaction({obj: {key: "string"}})
34+
self.check_invalid_transaction({obj: {key: 12}})
3535

3636
def check_positive_number(self, f: Callable) -> None:
3737
for good in (1, 1.1, Decimal("1.1")):
3838
self.check_transaction(f(good))
3939
for bad in ("1.2", "1", -1, -1.1, 0):
4040
self.check_invalid_transaction(f(bad))
4141

42-
def check_bool(self, object: str, key: str) -> None:
42+
def check_bool(self, obj: str, key: str) -> None:
4343
for good in (True, False):
44-
self.check_transaction({object: {key: good}})
44+
self.check_transaction({obj: {key: good}})
4545
for bad in ("", 0, "True"):
46-
self.check_invalid_transaction({object: {key: bad}})
46+
self.check_invalid_transaction({obj: {key: bad}})
4747

4848
def setup_report(self, report: dict[str, Any]) -> None:
4949
if "ip_address" not in report:

tests/test_webservice.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,19 @@ def setUp(self) -> None:
4545
"abcdef123456",
4646
host=f"{self.httpserver.host}:{self.httpserver.port}",
4747
)
48-
test_dir = os.path.join(os.path.dirname(__file__), "data")
49-
with builtins.open(
50-
os.path.join(test_dir, self.request_file),
48+
test_dir = os.path.join( # noqa: PTH118
49+
os.path.dirname(__file__), # noqa: PTH120
50+
"data",
51+
)
52+
with builtins.open( # noqa: PTH123
53+
os.path.join(test_dir, self.request_file), # noqa: PTH118
5154
encoding="utf-8",
5255
) as file:
5356
content = file.read()
5457
self.full_request = json.loads(content)
5558

56-
with builtins.open(
57-
os.path.join(test_dir, self.response_file),
59+
with builtins.open( # noqa: PTH123
60+
os.path.join(test_dir, self.response_file), # noqa: PTH118
5861
encoding="utf-8",
5962
) as file:
6063
self.response = file.read()
@@ -146,7 +149,9 @@ def create_error(
146149
content_type = (
147150
"application/json"
148151
if self.type == "report"
149-
else "application/vnd.maxmind.com-error+json; charset=UTF-8; version=2.0"
152+
else (
153+
"application/vnd.maxmind.com-error+json; charset=UTF-8; version=2.0"
154+
)
150155
)
151156
self.httpserver.expect_request(uri, method="POST").respond_with_data(
152157
text,
@@ -175,15 +180,18 @@ def create_success(
175180
status = 204 if self.type == "report" else 200
176181
self.httpserver.expect_request(uri, method="POST").respond_with_data(
177182
response,
178-
content_type=f"application/vnd.maxmind.com-minfraud-{self.type}+json; charset=UTF-8; version=2.0",
183+
content_type=(
184+
f"application/vnd.maxmind.com-minfraud-{self.type}+json; "
185+
"charset=UTF-8; version=2.0"
186+
),
179187
status=status,
180188
)
181189
if client is None:
182190
client = self.client
183191

184192
return self.run_client(getattr(client, self.type)(request))
185193

186-
def run_client(self, v):
194+
def run_client(self, v): # noqa: ANN001
187195
return v
188196

189197
def test_named_constructor_args(self) -> None:
@@ -193,8 +201,8 @@ def test_named_constructor_args(self) -> None:
193201
self.client_class(account_id=id, license_key=key),
194202
self.client_class(account_id=id, license_key=key),
195203
):
196-
self.assertEqual(client._account_id, str(id))
197-
self.assertEqual(client._license_key, key)
204+
self.assertEqual(client._account_id, str(id)) # noqa: SLF001
205+
self.assertEqual(client._license_key, key) # noqa: SLF001
198206

199207
def test_missing_constructor_args(self) -> None:
200208
with self.assertRaises(TypeError):
@@ -256,7 +264,10 @@ def test_200_with_email_hashing(self) -> None:
256264
},
257265
).respond_with_data(
258266
self.response,
259-
content_type=f"application/vnd.maxmind.com-minfraud-{self.type}+json; charset=UTF-8; version=2.0",
267+
content_type=(
268+
f"application/vnd.maxmind.com-minfraud-{self.type}+json; "
269+
"charset=UTF-8; version=2.0"
270+
),
260271
status=200,
261272
)
262273

@@ -389,7 +400,7 @@ def tearDown(self) -> None:
389400
self._loop.close()
390401
super().tearDown()
391402

392-
def run_client(self, v):
403+
def run_client(self, v): # noqa: ANN001
393404
return self._loop.run_until_complete(v)
394405

395406

0 commit comments

Comments
 (0)