Skip to content

Commit

Permalink
Add deprecation warning to instance methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ob-stripe committed Apr 26, 2019
1 parent e90ba4e commit b76c96a
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions stripe/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import sys
import os
import re
import warnings

import stripe
from stripe import six
Expand Down Expand Up @@ -303,9 +304,42 @@ def __get__(self, obj=None, objtype=None):
@functools.wraps(self.method)
def _wrapper(*args, **kwargs):
if obj is not None:
self._deprecation_warning(obj)
return self.method(obj, *args, **kwargs)
else:
class_method = getattr(objtype, self.class_method_name)
return class_method(*args, **kwargs)

return _wrapper

def _deprecation_warning(self, obj):
if (
not isinstance(obj, stripe.api_resources.abstract.APIResource)
or "id" not in obj
):
return

message = "This instance method is deprecated. Use the class method {module_name}.{class_name}.{method_name}({sid}, ...) instead.".format(
module_name=self._public_module_name(obj.__class__.__module__),
class_name=obj.__class__.__name__,
method_name=self.method.__name__,
sid=repr(obj["id"]),
)
warnings.simplefilter("always", DeprecationWarning)
warnings.warn(message, category=DeprecationWarning, stacklevel=3)
warnings.simplefilter("default", DeprecationWarning)

@staticmethod
def _public_module_name(module_name):
"""Converts a fully qualified module name to a public short name.
Examples:
- `stripe.api_resources.customer` -> `stripe`
- `stripe.api_resources.issuing.card` -> `stripe.issuing`
"""
parts = module_name.split(".")
# remove 'api_resources'
del parts[1]
# remove resource package
del parts[-1]
return ".".join(parts)

0 comments on commit b76c96a

Please sign in to comment.