Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@dcramer
Last active December 11, 2015 21:28
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dcramer/4662375 to your computer and use it in GitHub Desktop.
Save dcramer/4662375 to your computer and use it in GitHub Desktop.
Because you should maintain API compatibility when you tell everyone to use your shit.
from requests.models import Response
class fixedjson(object):
def __init__(self, func):
self.func = func
def __get__(self, inst, cls):
result = self.func(inst)
class proxy(type(result)):
def __call__(self):
return self
if isinstance(result, (dict, int, float, unicode, str, tuple, list)):
return proxy(result)
elif result is None:
return proxy()
else:
raise NotImplementedError
return proxy
Response.json = fixedjson(Response.json)
import requests
x = requests.get('http://httpbin.org/ip')
print repr(x.json)
print repr(x.json())
@bboe
Copy link

bboe commented Jan 29, 2013

Because you should maintain API compatibility when you tell everyone to use your shit.

That's true until major version numbers change. The better option is to update your dependencies such that only versions within the same major version are acceptable. Doing so requires a little bit of forethought, but you should be thinking about that anyway when you "tell everyone to use your shit".

@mattspitz
Copy link

Out of curiosity, not nitpicking, isn't line 21 unreachable?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment