7
7
"""
8
8
9
9
# 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 Optional
11
12
12
- from geoip2 .mixins import SimpleEquality
13
13
import geoip2 .models
14
14
import geoip2 .records
15
15
16
16
17
- class _Serializable (SimpleEquality ):
18
- def to_dict (self ):
19
- """Returns a dict of the object suitable for serialization"""
17
+ class _Serializable :
18
+ def __eq__ (self , other : object ) -> bool :
19
+ return isinstance (other , self .__class__ ) and self .to_dict () == other .to_dict ()
20
+
21
+ def __ne__ (self , other : object ) -> bool :
22
+ return not self .__eq__ (other )
23
+
24
+ def to_dict (self ) -> dict :
25
+ """Returns a dict of the object suitable for serialization."""
20
26
result = {}
21
27
for key , value in self .__dict__ .items ():
22
28
if hasattr (value , "to_dict" ) and callable (value .to_dict ):
23
29
if d := value .to_dict ():
24
30
result [key ] = d
25
- elif hasattr (value , "raw" ):
26
- # geoip2 uses "raw" for historical reasons
27
- if d := value .raw :
28
- result [key ] = d
29
31
elif isinstance (value , list ):
30
32
ls = []
31
33
for e in value :
@@ -82,7 +84,9 @@ class IPRiskReason(_Serializable):
82
84
code : Optional [str ]
83
85
reason : Optional [str ]
84
86
85
- def __init__ (self , code : Optional [str ] = None , reason : Optional [str ] = None ):
87
+ def __init__ (
88
+ self , code : Optional [str ] = None , reason : Optional [str ] = None
89
+ ) -> None :
86
90
self .code = code
87
91
self .reason = reason
88
92
@@ -112,7 +116,7 @@ class GeoIP2Location(geoip2.records.Location):
112
116
local_time : Optional [str ]
113
117
114
118
def __init__ (self , * args , ** kwargs ) -> None :
115
- self .local_time = kwargs .get ("local_time" , None )
119
+ self .local_time = kwargs .get ("local_time" )
116
120
super ().__init__ (* args , ** kwargs )
117
121
118
122
@@ -196,16 +200,16 @@ class IPAddress(geoip2.models.Insights):
196
200
197
201
location : GeoIP2Location
198
202
risk : Optional [float ]
199
- risk_reasons : List [IPRiskReason ]
203
+ risk_reasons : list [IPRiskReason ]
200
204
201
205
def __init__ (
202
206
self ,
203
207
locales : Optional [Sequence [str ]],
204
208
* ,
205
- country : Optional [Dict ] = None ,
206
- location : Optional [Dict ] = None ,
209
+ country : Optional [dict ] = None ,
210
+ location : Optional [dict ] = None ,
207
211
risk : Optional [float ] = None ,
208
- risk_reasons : Optional [List [ Dict ]] = None ,
212
+ risk_reasons : Optional [list [ dict ]] = None ,
209
213
** kwargs ,
210
214
) -> None :
211
215
# For raw attribute
@@ -218,7 +222,7 @@ def __init__(
218
222
if risk_reasons is not None :
219
223
kwargs ["risk_reasons" ] = risk_reasons
220
224
221
- super ().__init__ (kwargs , locales = list ( locales or []) )
225
+ super ().__init__ (locales , ** kwargs )
222
226
self .location = GeoIP2Location (** (location or {}))
223
227
self .risk = risk
224
228
self .risk_reasons = [IPRiskReason (** x ) for x in risk_reasons or []]
@@ -237,7 +241,7 @@ class ScoreIPAddress(_Serializable):
237
241
238
242
risk : Optional [float ]
239
243
240
- def __init__ (self , * , risk : Optional [float ] = None , ** _ ):
244
+ def __init__ (self , * , risk : Optional [float ] = None , ** _ ) -> None :
241
245
self .risk = risk
242
246
243
247
@@ -292,7 +296,7 @@ def __init__(
292
296
phone_number : Optional [str ] = None ,
293
297
matches_provided_phone_number : Optional [bool ] = None ,
294
298
** _ ,
295
- ):
299
+ ) -> None :
296
300
self .name = name
297
301
self .matches_provided_name = matches_provided_name
298
302
self .phone_number = phone_number
@@ -352,7 +356,7 @@ def __init__(
352
356
last_seen : Optional [str ] = None ,
353
357
local_time : Optional [str ] = None ,
354
358
** _ ,
355
- ):
359
+ ) -> None :
356
360
self .confidence = confidence
357
361
self .id = id
358
362
self .last_seen = last_seen
@@ -402,7 +406,7 @@ def __init__(
402
406
reason : Optional [str ] = None ,
403
407
rule_label : Optional [str ] = None ,
404
408
** _ ,
405
- ):
409
+ ) -> None :
406
410
self .action = action
407
411
self .reason = reason
408
412
self .rule_label = rule_label
@@ -423,7 +427,7 @@ class EmailDomain(_Serializable):
423
427
424
428
first_seen : Optional [str ]
425
429
426
- def __init__ (self , * , first_seen : Optional [str ] = None , ** _ ):
430
+ def __init__ (self , * , first_seen : Optional [str ] = None , ** _ ) -> None :
427
431
self .first_seen = first_seen
428
432
429
433
@@ -477,12 +481,12 @@ class Email(_Serializable):
477
481
478
482
def __init__ (
479
483
self ,
480
- domain : Optional [Dict ] = None ,
484
+ domain : Optional [dict ] = None ,
481
485
first_seen : Optional [str ] = None ,
482
486
is_disposable : Optional [bool ] = None ,
483
487
is_free : Optional [bool ] = None ,
484
488
is_high_risk : Optional [bool ] = None ,
485
- ):
489
+ ) -> None :
486
490
self .domain = EmailDomain (** (domain or {}))
487
491
self .first_seen = first_seen
488
492
self .is_disposable = is_disposable
@@ -564,7 +568,7 @@ class CreditCard(_Serializable):
564
568
565
569
def __init__ (
566
570
self ,
567
- issuer : Optional [Dict ] = None ,
571
+ issuer : Optional [dict ] = None ,
568
572
country : Optional [str ] = None ,
569
573
brand : Optional [str ] = None ,
570
574
is_business : Optional [bool ] = None ,
@@ -573,7 +577,7 @@ def __init__(
573
577
is_virtual : Optional [bool ] = None ,
574
578
# pylint:disable=redefined-builtin
575
579
type : Optional [str ] = None ,
576
- ):
580
+ ) -> None :
577
581
self .issuer = Issuer (** (issuer or {}))
578
582
self .country = country
579
583
self .brand = brand
@@ -642,7 +646,7 @@ def __init__(
642
646
distance_to_ip_location : Optional [int ] = None ,
643
647
is_in_ip_country : Optional [bool ] = None ,
644
648
** _ ,
645
- ):
649
+ ) -> None :
646
650
self .is_postal_in_city = is_postal_in_city
647
651
self .latitude = latitude
648
652
self .longitude = longitude
@@ -729,7 +733,7 @@ def __init__(
729
733
is_high_risk : Optional [bool ] = None ,
730
734
distance_to_billing_address : Optional [int ] = None ,
731
735
** _ ,
732
- ):
736
+ ) -> None :
733
737
self .is_postal_in_city = is_postal_in_city
734
738
self .latitude = latitude
735
739
self .longitude = longitude
@@ -789,7 +793,7 @@ def __init__(
789
793
network_operator : Optional [str ] = None ,
790
794
number_type : Optional [str ] = None ,
791
795
** _ ,
792
- ):
796
+ ) -> None :
793
797
self .country = country
794
798
self .is_voip = is_voip
795
799
self .network_operator = network_operator
@@ -837,7 +841,7 @@ def __init__(
837
841
warning : Optional [str ] = None ,
838
842
input_pointer : Optional [str ] = None ,
839
843
** _ ,
840
- ):
844
+ ) -> None :
841
845
self .code = code
842
846
self .warning = warning
843
847
self .input_pointer = input_pointer
@@ -1060,7 +1064,7 @@ def __init__(
1060
1064
shipping_address_distance_to_ip_location : Optional [float ] = None ,
1061
1065
time_of_day : Optional [float ] = None ,
1062
1066
** _ ,
1063
- ):
1067
+ ) -> None :
1064
1068
self .avs_result = avs_result
1065
1069
self .billing_address = billing_address
1066
1070
self .billing_address_distance_to_ip_location = (
@@ -1174,8 +1178,12 @@ class Reason(_Serializable):
1174
1178
reason : Optional [str ]
1175
1179
1176
1180
def __init__ (
1177
- self , * , code : Optional [str ] = None , reason : Optional [str ] = None , ** _
1178
- ):
1181
+ self ,
1182
+ * ,
1183
+ code : Optional [str ] = None ,
1184
+ reason : Optional [str ] = None ,
1185
+ ** _ ,
1186
+ ) -> None :
1179
1187
self .code = code
1180
1188
self .reason = reason
1181
1189
@@ -1202,15 +1210,15 @@ class RiskScoreReason(_Serializable):
1202
1210
"""
1203
1211
1204
1212
multiplier : float
1205
- reasons : List [Reason ]
1213
+ reasons : list [Reason ]
1206
1214
1207
1215
def __init__ (
1208
1216
self ,
1209
1217
* ,
1210
1218
multiplier : float ,
1211
- reasons : Optional [List ] = None ,
1219
+ reasons : Optional [list ] = None ,
1212
1220
** _ ,
1213
- ):
1221
+ ) -> None :
1214
1222
self .multiplier = multiplier
1215
1223
self .reasons = [Reason (** x ) for x in reasons or []]
1216
1224
@@ -1357,32 +1365,32 @@ class Factors(_Serializable):
1357
1365
shipping_address : ShippingAddress
1358
1366
shipping_phone : Phone
1359
1367
subscores : Subscores
1360
- warnings : List [ServiceWarning ]
1361
- risk_score_reasons : List [RiskScoreReason ]
1368
+ warnings : list [ServiceWarning ]
1369
+ risk_score_reasons : list [RiskScoreReason ]
1362
1370
1363
1371
def __init__ (
1364
1372
self ,
1365
1373
locales : Sequence [str ],
1366
1374
* ,
1367
- billing_address : Optional [Dict ] = None ,
1368
- billing_phone : Optional [Dict ] = None ,
1369
- credit_card : Optional [Dict ] = None ,
1370
- disposition : Optional [Dict ] = None ,
1375
+ billing_address : Optional [dict ] = None ,
1376
+ billing_phone : Optional [dict ] = None ,
1377
+ credit_card : Optional [dict ] = None ,
1378
+ disposition : Optional [dict ] = None ,
1371
1379
funds_remaining : float ,
1372
- device : Optional [Dict ] = None ,
1373
- email : Optional [Dict ] = None ,
1380
+ device : Optional [dict ] = None ,
1381
+ email : Optional [dict ] = None ,
1374
1382
# pylint:disable=redefined-builtin
1375
1383
id : str ,
1376
- ip_address : Optional [Dict ] = None ,
1384
+ ip_address : Optional [dict ] = None ,
1377
1385
queries_remaining : int ,
1378
1386
risk_score : float ,
1379
- shipping_address : Optional [Dict ] = None ,
1380
- shipping_phone : Optional [Dict ] = None ,
1381
- subscores : Optional [Dict ] = None ,
1382
- warnings : Optional [List [ Dict ]] = None ,
1383
- risk_score_reasons : Optional [List [ Dict ]] = None ,
1387
+ shipping_address : Optional [dict ] = None ,
1388
+ shipping_phone : Optional [dict ] = None ,
1389
+ subscores : Optional [dict ] = None ,
1390
+ warnings : Optional [list [ dict ]] = None ,
1391
+ risk_score_reasons : Optional [list [ dict ]] = None ,
1384
1392
** _ ,
1385
- ):
1393
+ ) -> None :
1386
1394
self .billing_address = BillingAddress (** (billing_address or {}))
1387
1395
self .billing_phone = Phone (** (billing_phone or {}))
1388
1396
self .credit_card = CreditCard (** (credit_card or {}))
@@ -1524,29 +1532,29 @@ class Insights(_Serializable):
1524
1532
risk_score : float
1525
1533
shipping_address : ShippingAddress
1526
1534
shipping_phone : Phone
1527
- warnings : List [ServiceWarning ]
1535
+ warnings : list [ServiceWarning ]
1528
1536
1529
1537
def __init__ (
1530
1538
self ,
1531
1539
locales : Sequence [str ],
1532
1540
* ,
1533
- billing_address : Optional [Dict ] = None ,
1534
- billing_phone : Optional [Dict ] = None ,
1535
- credit_card : Optional [Dict ] = None ,
1536
- device : Optional [Dict ] = None ,
1537
- disposition : Optional [Dict ] = None ,
1538
- email : Optional [Dict ] = None ,
1541
+ billing_address : Optional [dict ] = None ,
1542
+ billing_phone : Optional [dict ] = None ,
1543
+ credit_card : Optional [dict ] = None ,
1544
+ device : Optional [dict ] = None ,
1545
+ disposition : Optional [dict ] = None ,
1546
+ email : Optional [dict ] = None ,
1539
1547
funds_remaining : float ,
1540
1548
# pylint:disable=redefined-builtin
1541
1549
id : str ,
1542
- ip_address : Optional [Dict ] = None ,
1550
+ ip_address : Optional [dict ] = None ,
1543
1551
queries_remaining : int ,
1544
1552
risk_score : float ,
1545
- shipping_address : Optional [Dict ] = None ,
1546
- shipping_phone : Optional [Dict ] = None ,
1547
- warnings : Optional [List [ Dict ]] = None ,
1553
+ shipping_address : Optional [dict ] = None ,
1554
+ shipping_phone : Optional [dict ] = None ,
1555
+ warnings : Optional [list [ dict ]] = None ,
1548
1556
** _ ,
1549
- ):
1557
+ ) -> None :
1550
1558
self .billing_address = BillingAddress (** (billing_address or {}))
1551
1559
self .billing_phone = Phone (** (billing_phone or {}))
1552
1560
self .credit_card = CreditCard (** (credit_card or {}))
@@ -1627,21 +1635,21 @@ class Score(_Serializable):
1627
1635
ip_address : ScoreIPAddress
1628
1636
queries_remaining : int
1629
1637
risk_score : float
1630
- warnings : List [ServiceWarning ]
1638
+ warnings : list [ServiceWarning ]
1631
1639
1632
1640
def __init__ (
1633
1641
self ,
1634
1642
* ,
1635
- disposition : Optional [Dict ] = None ,
1643
+ disposition : Optional [dict ] = None ,
1636
1644
funds_remaining : float ,
1637
1645
# pylint:disable=redefined-builtin
1638
1646
id : str ,
1639
- ip_address : Optional [Dict ] = None ,
1647
+ ip_address : Optional [dict ] = None ,
1640
1648
queries_remaining : int ,
1641
1649
risk_score : float ,
1642
- warnings : Optional [List [ Dict ]] = None ,
1650
+ warnings : Optional [list [ dict ]] = None ,
1643
1651
** _ ,
1644
- ):
1652
+ ) -> None :
1645
1653
self .disposition = Disposition (** (disposition or {}))
1646
1654
self .funds_remaining = funds_remaining
1647
1655
self .id = id
0 commit comments