Refactor module-level deprecation code

This commit is contained in:
JustAnotherArchivist
2023-02-21 21:23:12 +00:00
parent 880a0a7f55
commit 7327a01397
2 changed files with 15 additions and 13 deletions

View File

@@ -14,8 +14,18 @@ import warnings
logger = logging.getLogger(__name__)
def _module_deprecation_helper(all, **names):
def __getattr__(name):
if name in names:
warnings.warn(f'{name} is deprecated, use {names[name].__name__} instead', DeprecatedFeatureWarning, stacklevel = 2)
return names[name]
raise AttributeError(f'module {__name__!r} has no attribute {name!r}')
def __dir__():
return sorted(all + list(names.keys()))
return __getattr__, __dir__
class DeprecatedPropertyAccessWarning(FutureWarning):
class DeprecatedFeatureWarning(FutureWarning):
pass
@@ -28,7 +38,7 @@ class _DeprecatedProperty:
def __get__(self, obj, objType):
if obj is None: # if the access is through the class using _DeprecatedProperty rather than an instance of the class:
return self
warnings.warn(f'{self.name} is deprecated, use {self.replStr} instead', DeprecatedPropertyAccessWarning, stacklevel = 2)
warnings.warn(f'{self.name} is deprecated, use {self.replStr} instead', DeprecatedFeatureWarning, stacklevel = 2)
return self.repl(obj)

View File

@@ -38,17 +38,6 @@ import urllib3.util.ssl_
import warnings
# DescriptionURL deprecation
_DEPRECATED_NAMES = {'DescriptionURL': 'TextLink'}
def __getattr__(name):
if name in _DEPRECATED_NAMES:
warnings.warn(f'{name} is deprecated, use {_DEPRECATED_NAMES[name]} instead', FutureWarning, stacklevel = 2)
return globals()[_DEPRECATED_NAMES[name]]
raise AttributeError(f'module {__name__!r} has no attribute {name!r}')
def __dir__():
return sorted(__all__ + list(_DEPRECATED_NAMES.keys()))
_logger = logging.getLogger(__name__)
_API_AUTHORIZATION_HEADER = 'Bearer AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs=1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA'
_globalGuestTokenManager = None
@@ -2082,3 +2071,6 @@ class TwitterTrendsScraper(_TwitterAPIScraper):
for item in entry['content']['timelineModule']['items']:
trend = item['item']['content']['trend']
yield Trend(name = trend['name'], metaDescription = trend['trendMetadata'].get('metaDescription'), domainContext = trend['trendMetadata']['domainContext'])
__getattr__, __dir__ = snscrape.base._module_deprecation_helper(__all__, DescriptionURL = TextLink)