-
Notifications
You must be signed in to change notification settings - Fork 10.8k
downloadermiddlewares.retry.BackwardsCompatibilityMetaclass
does not provide backward compatibility for middleware instances
#6049
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
Comments
The fix is actually very tricky. Here is a sample snippet: DEPRECATED_ATTRIBUTE = 'A'
def __getattr__(self, item):
if item == DEPRECATED_ATTRIBUTE:
return 'A does not exist'
raise AttributeError(f'{self.__class__.__name__!r} object has no attribute {item!r}')
class Meta(type):
__getattr__ = __getattr__
class Data(metaclass=Meta):
def __init__(self):
try:
self.a = self.__getattribute__(DEPRECATED_ATTRIBUTE)
except AttributeError:
self.a = 'a here'
__getattr__ = __getattr__
class DataDefineA(Data):
A = 1
class DataNoA(Data): pass
print(Data.A)
print(Data().A)
print(Data().a)
print(DataDefineA.A)
print(DataDefineA().A)
print(DataDefineA().a)
print(DataNoA.A)
print(DataNoA().A)
print(DataNoA().a) Output:
Such solution achieves both compatibility issues. In addition, if a subclass or its instance defines attribute Rewritten DEPRECATED_ATTRIBUTE = 'EXCEPTIONS_TO_RETRY'
def backwards_compatibility_getattr(self, item):
if item == DEPRECATED_ATTRIBUTE:
warnings.warn(
f"Attribute RetryMiddleware.{DEPRECATED_ATTRIBUTE} is deprecated. "
"Use the RETRY_EXCEPTIONS setting instead.",
ScrapyDeprecationWarning,
stacklevel=2,
)
return tuple(
load_object(x) if isinstance(x, str) else x
for x in Settings().getlist("RETRY_EXCEPTIONS")
)
raise AttributeError(f'{self.__class__.__name__!r} object has no attribute {item!r}')
class BackwardsCompatibilityMetaclass(type):
__getattr__ = backwards_compatibility_getattr
class RetryMiddleware(metaclass=BackwardsCompatibilityMetaclass):
def __init__(self, settings):
if not settings.getbool("RETRY_ENABLED"):
raise NotConfigured
self.max_retry_times = settings.getint("RETRY_TIMES")
self.retry_http_codes = set(
int(x) for x in settings.getlist("RETRY_HTTP_CODES")
)
self.priority_adjust = settings.getint("RETRY_PRIORITY_ADJUST")
try:
self.exceptions_to_retry = self.__getattribute__(DEPRECATED_ATTRIBUTE)
except AttributeError:
# If EXCEPTIONS_TO_RETRY is not "overridden"
self.exceptions_to_retry = tuple(
load_object(x) if isinstance(x, str) else x
for x in settings.getlist("RETRY_EXCEPTIONS")
)
__getattr__ = backwards_compatibility_getattr |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Description
Previously,
EXCEPTIONS_TO_RETRY
was an attribute ofRetryMiddleware
. This allows:RetryMiddleware
subclasses could accessEXCEPTIONS_TO_RETRY
viacls.EXCEPTIONS_TO_RETRY
.RetryMiddleware
instances and instances of its subclasses could accessEXCEPTIONS_TO_RETRY
viaself.EXCEPTIONS_TO_RETRY
.In 2.10
EXCEPTIONS_TO_RETRY
was removed and added as a property toBackwardsCompatibilityMetaclass
. This added compatibility only for the first point.Steps to Reproduce
Expected behavior
A warning about
EXCEPTIONS_TO_RETRY
deprecation.Actual behavior
AttributeError: 'MyRetryMiddleware' object has no attribute 'EXCEPTIONS_TO_RETRY'
Versions
The text was updated successfully, but these errors were encountered: