From 6998e1c905ef6e5fa737b32ac0b6e7f1b7701c14 Mon Sep 17 00:00:00 2001 From: Andrey Rakhmatullin Date: Wed, 10 May 2023 14:21:18 +0400 Subject: [PATCH] Fix typing-related issued on Python < 3.9. --- scrapy/utils/datatypes.py | 7 +++---- scrapy/utils/misc.py | 4 +--- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/scrapy/utils/datatypes.py b/scrapy/utils/datatypes.py index 599b201ea..0f6bdc5ab 100644 --- a/scrapy/utils/datatypes.py +++ b/scrapy/utils/datatypes.py @@ -5,10 +5,9 @@ Python Standard Library. This module must not depend on any module outside the Standard Library. """ -import collections import weakref from collections.abc import Mapping -from typing import Any, Optional, Sequence, TypeVar +from typing import Any, Optional, OrderedDict, Sequence, TypeVar _KT = TypeVar("_KT") _VT = TypeVar("_VT") @@ -68,7 +67,7 @@ class CaselessDict(dict): return dict.pop(self, self.normkey(key), *args) -class LocalCache(collections.OrderedDict[_KT, _VT]): +class LocalCache(OrderedDict[_KT, _VT]): """Dictionary with a finite number of keys. Older items expires first. @@ -85,7 +84,7 @@ class LocalCache(collections.OrderedDict[_KT, _VT]): super().__setitem__(key, value) -class LocalWeakReferencedCache(weakref.WeakKeyDictionary[_KT, _VT]): +class LocalWeakReferencedCache(weakref.WeakKeyDictionary): """ A weakref.WeakKeyDictionary implementation that uses LocalCache as its underlying data structure, making it ordered and capable of being size-limited. diff --git a/scrapy/utils/misc.py b/scrapy/utils/misc.py index defc8663d..70187ba74 100644 --- a/scrapy/utils/misc.py +++ b/scrapy/utils/misc.py @@ -232,9 +232,7 @@ def walk_callable(node: ast.AST) -> Generator[ast.AST, Any, None]: yield node -_generator_callbacks_cache: LocalWeakReferencedCache[ - Callable, bool -] = LocalWeakReferencedCache(limit=128) +_generator_callbacks_cache = LocalWeakReferencedCache(limit=128) def is_generator_with_return_value(callable: Callable) -> bool: