Skip to content

Commit

Permalink
Check with mypy
Browse files Browse the repository at this point in the history
  • Loading branch information
barseghyanartur committed May 19, 2020
1 parent a380f4a commit d422286
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 36 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -40,3 +40,4 @@ pycallgraph.*
mprof.svg
mprofile_*.dat
profiler.py.prof
.mypy_cache/
2 changes: 2 additions & 0 deletions mypy.ini
@@ -0,0 +1,2 @@
[mypy]
;disallow_untyped_defs = False
Empty file added src/tld/py.typed
Empty file.
30 changes: 15 additions & 15 deletions src/tld/registry.py
@@ -1,4 +1,4 @@
from typing import Type, Dict
from typing import Dict

__author__ = 'Artur Barseghyan'
__copyright__ = '2013-2020 Artur Barseghyan'
Expand All @@ -10,36 +10,36 @@

class Registry(type):

REGISTRY = {}
REGISTRY = {} # type: Dict[str, Registry]

def __new__(cls, name, bases, attrs):
new_cls = type.__new__(cls, name, bases, attrs)
def __new__(mcs, name, bases, attrs):
new_cls = type.__new__(mcs, name, bases, attrs)
# Here the name of the class is used as key but it could be any class
# parameter.
if getattr(new_cls, '_uid', None):
cls.REGISTRY[new_cls._uid] = new_cls
mcs.REGISTRY[new_cls._uid] = new_cls
return new_cls

@property
def _uid(cls) -> str:
return getattr(cls, 'uid', cls.__name__)

@classmethod
def reset(cls) -> None:
cls.REGISTRY = {}
def reset(mcs) -> None:
mcs.REGISTRY = {}

@classmethod
def get(cls, key, default=None):
return cls.REGISTRY.get(key, default)
def get(mcs, key, default=None):
return mcs.REGISTRY.get(key, default)

@classmethod
def items(cls):
return cls.REGISTRY.items()
def items(mcs):
return mcs.REGISTRY.items()

# @classmethod
# def get_registry(cls) -> Dict[str, Type]:
# return dict(cls.REGISTRY)
# def get_registry(mcs) -> Dict[str, Type]:
# return dict(mcs.REGISTRY)
#
# @classmethod
# def pop(cls, uid) -> None:
# cls.REGISTRY.pop(uid)
# def pop(mcs, uid) -> None:
# mcs.REGISTRY.pop(uid)
10 changes: 5 additions & 5 deletions src/tld/res/effective_tld_names.dat.txt
Expand Up @@ -7092,7 +7092,7 @@ org.zw

// newGTLDs

// List of new gTLDs imported from https://www.icann.org/resources/registries/gtlds/v2/gtlds.json on 2020-04-02T18:20:31Z
// List of new gTLDs imported from https://www.icann.org/resources/registries/gtlds/v2/gtlds.json on 2020-05-06T16:23:34Z
// This list is auto-generated, don't edit it manually.
// aaa : 2015-02-26 American Automobile Association, Inc.
aaa
Expand Down Expand Up @@ -7241,7 +7241,7 @@ analytics
// android : 2014-08-07 Charleston Road Registry Inc.
android

// anquan : 2015-01-08 QIHOO 360 TECHNOLOGY CO. LTD.
// anquan : 2015-01-08 Beijing Qihu Keji Co., Ltd.
anquan

// anz : 2015-07-31 Australia and New Zealand Banking Group Limited
Expand Down Expand Up @@ -9749,7 +9749,7 @@ shop
// shopping : 2016-03-31 Binky Moon, LLC
shopping

// shouji : 2015-01-08 QIHOO 360 TECHNOLOGY CO. LTD.
// shouji : 2015-01-08 Beijing Qihu Keji Co., Ltd.
shouji

// show : 2015-03-05 Binky Moon, LLC
Expand Down Expand Up @@ -10313,7 +10313,7 @@ xerox
// xfinity : 2015-07-09 Comcast IP Holdings I, LLC
xfinity

// xihuan : 2015-01-08 QIHOO 360 TECHNOLOGY CO. LTD.
// xihuan : 2015-01-08 Beijing Qihu Keji Co., Ltd.
xihuan

// xin : 2014-12-11 Elegant Leader Limited
Expand Down Expand Up @@ -10634,7 +10634,7 @@ you
// youtube : 2014-05-01 Charleston Road Registry Inc.
youtube

// yun : 2015-01-08 QIHOO 360 TECHNOLOGY CO. LTD.
// yun : 2015-01-08 Beijing Qihu Keji Co., Ltd.
yun

// zappos : 2015-06-25 Amazon Registry Services, Inc.
Expand Down
4 changes: 2 additions & 2 deletions src/tld/result.py
@@ -1,4 +1,4 @@
from typing import Dict
from typing import Any, Dict
from urllib.parse import SplitResult

__author__ = 'Artur Barseghyan'
Expand Down Expand Up @@ -52,7 +52,7 @@ def __str__(self) -> str:
__repr__ = __str__

@property
def __dict__(self) -> Dict[str, str]:
def __dict__(self) -> Dict[str, Any]: # type: ignore
"""Mimic __dict__ functionality.
:return:
Expand Down
2 changes: 1 addition & 1 deletion src/tld/tests/test_core.py
Expand Up @@ -9,7 +9,7 @@

from urllib.parse import urlsplit

from faker import Faker
from faker import Faker # type: ignore

from .. import defaults
from ..base import BaseTLDSourceParser
Expand Down
25 changes: 12 additions & 13 deletions src/tld/utils.py
Expand Up @@ -5,7 +5,7 @@
# codecs_open = open
from os.path import isabs
import sys
from typing import Dict, Type, Union, Tuple, List
from typing import Dict, Type, Union, Tuple, List, Optional
from urllib.parse import urlsplit, SplitResult

from .base import BaseTLDSourceParser
Expand Down Expand Up @@ -89,7 +89,7 @@ def update_tld_names(
:param parser_uid:
:return:
"""
results = []
results: List[bool] = []
results_append = results.append
if parser_uid:
parser_cls = Registry.get(parser_uid, None)
Expand Down Expand Up @@ -176,7 +176,7 @@ def get_tld_names(
cls,
fail_silently: bool = False,
retry_count: int = 0
) -> Union[Dict[str, Trie], None]:
) -> Optional[Dict[str, Trie]]:
"""Parse.
:param fail_silently:
Expand Down Expand Up @@ -397,7 +397,7 @@ def get_fld(
search_private: bool = True,
parser_class: Type[BaseTLDSourceParser] = MozillaTLDSourceParser,
**kwargs
) -> Union[str, None]:
) -> Optional[str]:
"""Extract the first level domain.
Extract the top level domain based on the mozilla's effective TLD names
Expand Down Expand Up @@ -456,7 +456,7 @@ def get_tld(
search_public: bool = True,
search_private: bool = True,
parser_class: Type[BaseTLDSourceParser] = MozillaTLDSourceParser
) -> Union[None, str, Result]:
) -> Optional[Union[str, Result]]:
"""Extract the top level domain.
Extract the top level domain based on the mozilla's effective TLD names
Expand Down Expand Up @@ -539,7 +539,7 @@ def parse_tld(
:param search_public:
:param search_private:
:param parser_class:
:return:
:return: Tuple (tld, domain, subdomain)
:rtype: tuple
"""
try:
Expand All @@ -552,21 +552,20 @@ def parse_tld(
search_private=search_private,
parser_class=parser_class
)
_tld = obj.tld
domain = obj.domain
subdomain = obj.subdomain
if obj is None:
return None, None, None

return obj.tld, obj.domain, obj.subdomain # type: ignore

except (
TldBadUrl,
TldDomainNotFound,
TldImproperlyConfigured,
TldIOError
):
_tld = None
domain = None
subdomain = None
pass

return _tld, domain, subdomain
return None, None, None


def is_tld(
Expand Down

0 comments on commit d422286

Please sign in to comment.