55import ipaddress
66import sys
77import unittest
8+ from abc import ABC , abstractmethod
89from collections import defaultdict
9- from typing import cast
10+ from typing import cast , Callable , Union
1011
1112import pytest
1213import pytest_httpserver
2627from geoip2 .webservice import AsyncClient , Client
2728
2829
29- class TestBaseClient (unittest .TestCase ):
30+ class TestBaseClient (unittest .TestCase , ABC ):
31+ client : Union [AsyncClient , Client ]
32+ client_class : Callable [[int , str ], Union [AsyncClient , Client ]]
33+
3034 country = {
3135 "continent" : {"code" : "NA" , "geoname_id" : 42 , "names" : {"en" : "North America" }},
3236 "country" : {
@@ -54,6 +58,9 @@ class TestBaseClient(unittest.TestCase):
5458 insights ["traits" ]["user_count" ] = 2
5559 insights ["traits" ]["static_ip_score" ] = 1.3
5660
61+ @abstractmethod
62+ def run_client (self , v ): ...
63+
5764 def _content_type (self , endpoint ):
5865 return (
5966 "application/vnd.maxmind.com-"
@@ -319,7 +326,7 @@ def user_agent_compare(actual: str, expected: str) -> bool:
319326 header_value_matcher = HeaderValueMatcher (
320327 defaultdict (
321328 lambda : HeaderValueMatcher .default_header_value_matcher ,
322- {"User-Agent" : user_agent_compare },
329+ {"User-Agent" : user_agent_compare }, # type: ignore[dict-item]
323330 ),
324331 ),
325332 ).respond_with_json (
@@ -374,19 +381,22 @@ def test_insights_ok(self) -> None:
374381 def test_named_constructor_args (self ) -> None :
375382 id = 47
376383 key = "1234567890ab"
377- client = self .client_class (account_id = id , license_key = key )
384+ client = self .client_class (id , key )
378385 self .assertEqual (client ._account_id , str (id ))
379386 self .assertEqual (client ._license_key , key )
380387
381388 def test_missing_constructor_args (self ) -> None :
382389 with self .assertRaises (TypeError ):
383- self .client_class (license_key = "1234567890ab" )
390+
391+ self .client_class (license_key = "1234567890ab" ) # type: ignore[call-arg]
384392
385393 with self .assertRaises (TypeError ):
386- self .client_class ("47" )
394+ self .client_class ("47" ) # type: ignore
387395
388396
389397class TestClient (TestBaseClient ):
398+ client : Client
399+
390400 def setUp (self ) -> None :
391401 self .client_class = Client
392402 self .client = Client (42 , "abcdef123456" )
@@ -398,6 +408,8 @@ def run_client(self, v):
398408
399409
400410class TestAsyncClient (TestBaseClient ):
411+ client : AsyncClient
412+
401413 def setUp (self ) -> None :
402414 self ._loop = asyncio .new_event_loop ()
403415 self .client_class = AsyncClient
0 commit comments