Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type hints #132

Merged
merged 1 commit into from
Oct 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,9 @@ commands =
black urlextract --check --skip-string-normalization
black tests --check --skip-string-normalization
black setup.py --check --skip-string-normalization

[testenv:mypy]
deps =
mypy
commands =
mypy --install-types --non-interactive --namespace-packages urlextract
22 changes: 12 additions & 10 deletions urlextract/cachefile.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
import os
import tempfile
import urllib.request
from typing import Set, Iterable, Tuple, List, Union, NoReturn

from datetime import datetime
from urllib.error import URLError, HTTPError

import idna
import idna # type: ignore
import filelock
from platformdirs import user_cache_dir

Expand Down Expand Up @@ -61,7 +63,7 @@ def __init__(self, cache_dir=None):
self._tld_list_path = self._get_default_cache_file_path()
self._default_cache_file = True

def _get_default_cache_dir(self):
def _get_default_cache_dir(self) -> str:
"""
Returns default cache directory (data directory)

Expand All @@ -72,7 +74,7 @@ def _get_default_cache_dir(self):

return os.path.join(os.path.dirname(__file__), self._DATA_DIR)

def _get_default_cache_file_path(self):
def _get_default_cache_file_path(self) -> str:
"""
Returns default cache file path

Expand All @@ -91,7 +93,7 @@ def _get_default_cache_file_path(self):

return default_list_path

def _get_writable_cache_dir(self):
def _get_writable_cache_dir(self) -> str:
"""
Get writable cache directory with fallback to user's cache directory
and global temp directory
Expand Down Expand Up @@ -124,7 +126,7 @@ def _get_writable_cache_dir(self):

raise CacheFileError("Cache directories are not writable.")

def _get_cache_file_path(self):
def _get_cache_file_path(self) -> str:
"""
Get path for cache file

Expand All @@ -148,7 +150,7 @@ def _get_cache_file_path(self):
# get path for cached file
return os.path.join(cache_dir, self._CACHE_FILE_NAME)

def _get_cache_lock_file_path(self):
def _get_cache_lock_file_path(self) -> str:
"""
Get path for cache file lock

Expand All @@ -158,7 +160,7 @@ def _get_cache_lock_file_path(self):
"""
return self._get_cache_file_path() + ".lock"

def _download_tlds_list(self):
def _download_tlds_list(self) -> bool:
"""
Function downloads list of TLDs from IANA.
LINK: https://data.iana.org/TLD/tlds-alpha-by-domain.txt
Expand Down Expand Up @@ -215,7 +217,7 @@ def _download_tlds_list(self):

return True

def _load_cached_tlds(self):
def _load_cached_tlds(self) -> Set[str]:
"""
Loads TLDs from cached file to set.

Expand All @@ -231,7 +233,7 @@ def _load_cached_tlds(self):
)
raise CacheFileError("Cached file is not readable for current user.")

set_of_tlds = set()
set_of_tlds : Set[str] = set()

with filelock.FileLock(self._get_cache_lock_file_path()):
with open(self._tld_list_path, "r") as f_cache_tld:
Expand All @@ -249,7 +251,7 @@ def _load_cached_tlds(self):

return set_of_tlds

def _get_last_cachefile_modification(self):
def _get_last_cachefile_modification(self) -> Union[datetime, None]:
"""
Get last modification of cache file with TLDs.

Expand Down
Loading