Typing for smaller scrapy/utils/ modules.

This commit is contained in:
Andrey Rakhmatullin 2023-04-16 01:12:21 +04:00
parent 92f86fab06
commit 4596a58a13
4 changed files with 14 additions and 10 deletions

View File

@ -1,7 +1,7 @@
"""Boto/botocore helpers"""
def is_botocore_available():
def is_botocore_available() -> bool:
try:
import botocore # noqa: F401

View File

@ -1,19 +1,21 @@
import warnings
from functools import wraps
from typing import Any, Callable
from twisted.internet import defer, threads
from twisted.internet.defer import Deferred
from scrapy.exceptions import ScrapyDeprecationWarning
def deprecated(use_instead=None):
def deprecated(use_instead: Any = None) -> Callable:
"""This is a decorator which can be used to mark functions
as deprecated. It will result in a warning being emitted
when the function is used."""
def deco(func):
def deco(func: Callable) -> Callable:
@wraps(func)
def wrapped(*args, **kwargs):
def wrapped(*args: Any, **kwargs: Any) -> Any:
message = f"Call to deprecated function {func.__name__}."
if use_instead:
message += f" Use {use_instead} instead."
@ -28,23 +30,23 @@ def deprecated(use_instead=None):
return deco
def defers(func):
def defers(func: Callable) -> Callable[..., Deferred]:
"""Decorator to make sure a function always returns a deferred"""
@wraps(func)
def wrapped(*a, **kw):
def wrapped(*a: Any, **kw: Any) -> Deferred:
return defer.maybeDeferred(func, *a, **kw)
return wrapped
def inthread(func):
def inthread(func: Callable) -> Callable[..., Deferred]:
"""Decorator to call a function in a thread and return a deferred with the
result
"""
@wraps(func)
def wrapped(*a, **kw):
def wrapped(*a: Any, **kw: Any) -> Deferred:
return threads.deferToThread(func, *a, **kw)
return wrapped

View File

@ -1,6 +1,7 @@
import datetime
import decimal
import json
from typing import Any
from itemadapter import ItemAdapter, is_item
from twisted.internet import defer
@ -12,7 +13,7 @@ class ScrapyJSONEncoder(json.JSONEncoder):
DATE_FORMAT = "%Y-%m-%d"
TIME_FORMAT = "%H:%M:%S"
def default(self, o):
def default(self, o: Any) -> Any:
if isinstance(o, set):
return list(o)
if isinstance(o, datetime.datetime):

View File

@ -1,5 +1,6 @@
import platform
import sys
from typing import List, Tuple
import cryptography
import cssselect
@ -12,7 +13,7 @@ import scrapy
from scrapy.utils.ssl import get_openssl_version
def scrapy_components_versions():
def scrapy_components_versions() -> List[Tuple[str, str]]:
lxml_version = ".".join(map(str, lxml.etree.LXML_VERSION))
libxml2_version = ".".join(map(str, lxml.etree.LIBXML_VERSION))