Skip to content

Commit 7b4f0bc

Browse files
committed
Autofixes from ruff
1 parent 7a489b4 commit 7b4f0bc

10 files changed

+146
-101
lines changed

docs/conf.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#!/usr/bin/env python3
2-
# -*- coding: utf-8 -*-
32
#
43
# This file is execfile()d with the current directory set to its containing dir.
54
#
@@ -9,8 +8,8 @@
98
# All configuration values have a default; values that are commented out
109
# serve to show the default.
1110

12-
import sys
1311
import os
12+
import sys
1413

1514
# If extensions (or modules to document with autodoc) are in another directory,
1615
# add these directories to sys.path here. If the directory is relative to the

minfraud/__init__.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@
77

88
# flake8: noqa: F401
99
from .errors import (
10-
MinFraudError,
1110
AuthenticationError,
1211
HTTPError,
13-
InvalidRequestError,
1412
InsufficientFundsError,
13+
InvalidRequestError,
14+
MinFraudError,
1515
)
16-
17-
from .webservice import AsyncClient, Client
1816
from .version import __version__
17+
from .webservice import AsyncClient, Client
1918

2019
__author__ = "Gregory Oschwald"

minfraud/models.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77
"""
88

99
# pylint:disable=too-many-lines,too-many-instance-attributes,too-many-locals
10-
from typing import Dict, List, Optional, Sequence
10+
from collections.abc import Sequence
11+
from typing import Dict, List, Optional
1112

12-
from geoip2.mixins import SimpleEquality
1313
import geoip2.models
1414
import geoip2.records
15+
from geoip2.mixins import SimpleEquality
1516

1617

1718
class _Serializable(SimpleEquality):
@@ -112,7 +113,7 @@ class GeoIP2Location(geoip2.records.Location):
112113
local_time: Optional[str]
113114

114115
def __init__(self, *args, **kwargs) -> None:
115-
self.local_time = kwargs.get("local_time", None)
116+
self.local_time = kwargs.get("local_time")
116117
super().__init__(*args, **kwargs)
117118

118119

@@ -1174,7 +1175,11 @@ class Reason(_Serializable):
11741175
reason: Optional[str]
11751176

11761177
def __init__(
1177-
self, *, code: Optional[str] = None, reason: Optional[str] = None, **_
1178+
self,
1179+
*,
1180+
code: Optional[str] = None,
1181+
reason: Optional[str] = None,
1182+
**_,
11781183
):
11791184
self.code = code
11801185
self.reason = reason

minfraud/request.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
66
"""
77

8-
import re
9-
import warnings
108
import hashlib
9+
import re
1110
import unicodedata
11+
import warnings
1212
from typing import Any, Dict
13+
1314
from voluptuous import MultipleInvalid
1415

1516
from .errors import InvalidRequestError
@@ -345,7 +346,7 @@ def _clean_domain(domain):
345346

346347
idx = domain.rfind(".")
347348
if idx != -1:
348-
tld = domain[idx + 1 :] # noqa
349+
tld = domain[idx + 1 :]
349350
if tld in _TYPO_TLDS:
350351
domain = domain[:idx] + "." + _TYPO_TLDS.get(tld)
351352

@@ -362,7 +363,7 @@ def _clean_email(address):
362363
if at_idx == -1:
363364
return None, None
364365

365-
domain = _clean_domain(address[at_idx + 1 :]) # noqa
366+
domain = _clean_domain(address[at_idx + 1 :])
366367
local_part = address[:at_idx]
367368

368369
local_part = unicodedata.normalize("NFC", local_part)

minfraud/validation.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
import ipaddress
1111
import re
12-
import uuid
1312
import urllib.parse
13+
import uuid
1414
from decimal import Decimal
1515
from typing import Optional
1616

@@ -31,7 +31,8 @@
3131
_custom_input_value = Any(
3232
All(str, Match(r"^[^\n]{1,255}\Z")),
3333
All(
34-
_any_number, Range(min=-1e13, max=1e13, min_included=False, max_included=False)
34+
_any_number,
35+
Range(min=-1e13, max=1e13, min_included=False, max_included=False),
3536
),
3637
bool,
3738
)
@@ -41,7 +42,8 @@
4142
_country_code = All(str, Match(r"^[A-Z]{2}$"))
4243

4344
_telephone_country_code = Any(
44-
All(str, Match("^[0-9]{1,4}$")), All(int, Range(min=1, max=9999))
45+
All(str, Match("^[0-9]{1,4}$")),
46+
All(int, Range(min=1, max=9999)),
4547
)
4648

4749
_subdivision_iso_code = All(str, Match(r"^[0-9A-Z]{1,4}$"))
@@ -251,7 +253,7 @@ def _hostname(hostname: str) -> str:
251253
"windcave",
252254
"wirecard",
253255
"worldpay",
254-
]
256+
],
255257
)
256258

257259
_single_char = Match("^[A-Za-z0-9]$")
@@ -268,7 +270,7 @@ def _credit_card_token(s: str) -> str:
268270

269271

270272
_rfc3339_datetime = Match(
271-
r"(?a)\A\d{4}-\d{2}-\d{2}[Tt]\d{2}:\d{2}:\d{2}(\.\d+)?(?:[Zz]|[+-]\d{2}:\d{2})\Z"
273+
r"(?a)\A\d{4}-\d{2}-\d{2}[Tt]\d{2}:\d{2}:\d{2}(\.\d+)?(?:[Zz]|[+-]\d{2}:\d{2})\Z",
272274
)
273275

274276

@@ -283,7 +285,7 @@ def _credit_card_token(s: str) -> str:
283285
"recurring_purchase",
284286
"referral",
285287
"survey",
286-
]
288+
],
287289
)
288290

289291
_currency_code = Match("^[A-Z]{3}$")
@@ -416,7 +418,7 @@ def _validate_at_least_one_identifier_field(report):
416418
# voluptuous returns.
417419
raise MultipleInvalid(
418420
"The report must contain at least one of the following fields: "
419-
"'ip_address', 'maxmind_id', 'minfraud_id', 'transaction_id'."
421+
"'ip_address', 'maxmind_id', 'minfraud_id', 'transaction_id'.",
420422
)
421423
return True
422424

minfraud/webservice.py

+37-14
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,27 @@
77
"""
88

99
import json
10+
from collections.abc import Sequence
1011
from functools import partial
11-
from typing import Any, Callable, cast, Dict, Optional, Sequence, Union
12+
from typing import Any, Callable, Dict, Optional, Union, cast
1213

1314
import aiohttp
1415
import aiohttp.http
1516
import requests
1617
import requests.utils
1718
from requests.models import Response
1819

19-
from .version import __version__
2020
from .errors import (
21-
MinFraudError,
22-
HTTPError,
2321
AuthenticationError,
22+
HTTPError,
2423
InsufficientFundsError,
2524
InvalidRequestError,
25+
MinFraudError,
2626
PermissionRequiredError,
2727
)
2828
from .models import Factors, Insights, Score
2929
from .request import prepare_report, prepare_transaction
30-
30+
from .version import __version__
3131

3232
_AIOHTTP_UA = f"minFraud-API/{__version__} {aiohttp.http.SERVER_SOFTWARE}"
3333

@@ -85,7 +85,11 @@ def _handle_success(
8585
return model_class(**decoded_body) # type: ignore
8686

8787
def _exception_for_error(
88-
self, status: int, content_type: Optional[str], raw_body: str, uri: str
88+
self,
89+
status: int,
90+
content_type: Optional[str],
91+
raw_body: str,
92+
uri: str,
8993
) -> Union[
9094
AuthenticationError,
9195
InsufficientFundsError,
@@ -94,15 +98,18 @@ def _exception_for_error(
9498
PermissionRequiredError,
9599
]:
96100
"""Returns the exception for the error responses."""
97-
98101
if 400 <= status < 500:
99102
return self._exception_for_4xx_status(status, content_type, raw_body, uri)
100103
if 500 <= status < 600:
101104
return self._exception_for_5xx_status(status, raw_body, uri)
102105
return self._exception_for_unexpected_status(status, raw_body, uri)
103106

104107
def _exception_for_4xx_status(
105-
self, status: int, content_type: Optional[str], raw_body: str, uri: str
108+
self,
109+
status: int,
110+
content_type: Optional[str],
111+
raw_body: str,
112+
uri: str,
106113
) -> Union[
107114
AuthenticationError,
108115
InsufficientFundsError,
@@ -113,7 +120,10 @@ def _exception_for_4xx_status(
113120
"""Returns exception for error responses with 4xx status codes."""
114121
if not raw_body:
115122
return HTTPError(
116-
f"Received a {status} error with no body", status, uri, raw_body
123+
f"Received a {status} error with no body",
124+
status,
125+
uri,
126+
raw_body,
117127
)
118128
if content_type is None or content_type.find("json") == -1:
119129
return HTTPError(
@@ -135,7 +145,10 @@ def _exception_for_4xx_status(
135145

136146
if "code" in decoded_body and "error" in decoded_body:
137147
return self._exception_for_web_service_error(
138-
decoded_body.get("error"), decoded_body.get("code"), status, uri
148+
decoded_body.get("error"),
149+
decoded_body.get("code"),
150+
status,
151+
uri,
139152
)
140153
return HTTPError(
141154
"Error response contains JSON but it does not "
@@ -147,7 +160,10 @@ def _exception_for_4xx_status(
147160

148161
@staticmethod
149162
def _exception_for_web_service_error(
150-
message: str, code: str, status: int, uri: str
163+
message: str,
164+
code: str,
165+
status: int,
166+
uri: str,
151167
) -> Union[
152168
InvalidRequestError,
153169
AuthenticationError,
@@ -354,7 +370,9 @@ async def score(
354370
)
355371

356372
async def report(
357-
self, report: Dict[str, Optional[str]], validate: bool = True
373+
self,
374+
report: Dict[str, Optional[str]],
375+
validate: bool = True,
358376
) -> None:
359377
"""Send a transaction report to the Report Transaction endpoint.
360378
@@ -403,7 +421,9 @@ async def _response_for(
403421
return self._handle_success(raw_body, uri, model_class)
404422

405423
async def _do_request(
406-
self, uri: str, data: Dict[str, Any]
424+
self,
425+
uri: str,
426+
data: Dict[str, Any],
407427
) -> aiohttp.ClientResponse:
408428
session = await self._session()
409429
return await session.post(uri, json=data, proxy=self._proxy)
@@ -651,7 +671,10 @@ def _response_for(
651671

652672
def _do_request(self, uri: str, data: Dict[str, Any]) -> Response:
653673
return self._session.post(
654-
uri, json=data, timeout=self._timeout, proxies=self._proxies
674+
uri,
675+
json=data,
676+
timeout=self._timeout,
677+
proxies=self._proxies,
655678
)
656679

657680
def close(self):

tests/test_models.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,8 @@ def test_factors(self):
291291
self.assertEqual(0.01, factors.subscores.avs_result)
292292
self.assertEqual(0.02, factors.subscores.billing_address)
293293
self.assertEqual(
294-
0.03, factors.subscores.billing_address_distance_to_ip_location
294+
0.03,
295+
factors.subscores.billing_address_distance_to_ip_location,
295296
)
296297
self.assertEqual(0.04, factors.subscores.browser)
297298
self.assertEqual(0.05, factors.subscores.chargeback)
@@ -309,7 +310,8 @@ def test_factors(self):
309310
self.assertEqual(0.15, factors.subscores.phone_number)
310311
self.assertEqual(0.2, factors.subscores.shipping_address)
311312
self.assertEqual(
312-
0.16, factors.subscores.shipping_address_distance_to_ip_location
313+
0.16,
314+
factors.subscores.shipping_address_distance_to_ip_location,
313315
)
314316
self.assertEqual(0.17, factors.subscores.time_of_day)
315317

@@ -365,9 +367,9 @@ def factors_response(self):
365367
{
366368
"code": "ANONYMOUS_IP",
367369
"reason": "Risk due to IP being an Anonymous IP",
368-
}
370+
},
369371
],
370-
}
372+
},
371373
],
372374
}
373375

@@ -399,5 +401,6 @@ def check_risk_score_reasons_data(self, reasons):
399401
self.assertEqual(1, len(reasons[0].reasons))
400402
self.assertEqual("ANONYMOUS_IP", reasons[0].reasons[0].code)
401403
self.assertEqual(
402-
"Risk due to IP being an Anonymous IP", reasons[0].reasons[0].reason
404+
"Risk due to IP being an Anonymous IP",
405+
reasons[0].reasons[0].reason,
403406
)

0 commit comments

Comments
 (0)