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

Fix type-var error, in newer versions of mypy #275

Merged
merged 1 commit into from Oct 3, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 20 additions & 15 deletions tldextract/cache.py
Expand Up @@ -7,7 +7,16 @@
import os.path
import sys
from hashlib import md5
from typing import Callable, Dict, Hashable, Iterable, Optional, TypeVar, Union
from typing import (
Callable,
Dict,
Hashable,
Iterable,
Optional,
TypeVar,
Union,
cast,
)

from filelock import FileLock
import requests
Expand Down Expand Up @@ -87,7 +96,7 @@ def __init__(self, cache_dir: Optional[str], lock_timeout: int = 20):
# combined with a call to `.clear()` wont wipe someones hard drive
self.file_ext = ".tldextract.json"

def get(self, namespace: str, key: Union[str, Dict[str, Hashable]]) -> T:
def get(self, namespace: str, key: Union[str, Dict[str, Hashable]]) -> object:
"""Retrieve a value from the disk cache"""
if not self.enabled:
raise KeyError("Cache is disabled")
Expand Down Expand Up @@ -121,12 +130,10 @@ def set(
global _DID_LOG_UNABLE_TO_CACHE # pylint: disable=global-statement
if not _DID_LOG_UNABLE_TO_CACHE:
LOG.warning(
(
"unable to cache %s.%s in %s. This could refresh the "
"Public Suffix List over HTTP every app startup. "
"Construct your `TLDExtract` with a writable `cache_dir` or "
"set `cache_dir=None` to silence this warning. %s"
),
"unable to cache %s.%s in %s. This could refresh the "
"Public Suffix List over HTTP every app startup. "
"Construct your `TLDExtract` with a writable `cache_dir` or "
"set `cache_dir=None` to silence this warning. %s",
namespace,
key,
cache_filepath,
Expand Down Expand Up @@ -181,12 +188,10 @@ def run_and_cache(
global _DID_LOG_UNABLE_TO_CACHE # pylint: disable=global-statement
if not _DID_LOG_UNABLE_TO_CACHE:
LOG.warning(
(
"unable to cache %s.%s in %s. This could refresh the "
"Public Suffix List over HTTP every app startup. "
"Construct your `TLDExtract` with a writable `cache_dir` or "
"set `cache_dir=None` to silence this warning. %s"
),
"unable to cache %s.%s in %s. This could refresh the "
"Public Suffix List over HTTP every app startup. "
"Construct your `TLDExtract` with a writable `cache_dir` or "
"set `cache_dir=None` to silence this warning. %s",
namespace,
key_args,
cache_filepath,
Expand All @@ -200,7 +205,7 @@ def run_and_cache(
# pylint: disable-next=abstract-class-instantiated
with FileLock(lock_path, timeout=self.lock_timeout):
try:
result: T = self.get(namespace=namespace, key=key_args)
result = cast(T, self.get(namespace=namespace, key=key_args))
except KeyError:
result = func(**kwargs)
self.set(namespace=namespace, key=key_args, value=result)
Expand Down