# Safe composition in python

def json_response(response):
assert response.code == 200, 'bad http response'

def custom_json_response(response):
response = json_response(response)
assert 'answer' in response, 'invalid custom json'


The code looks straightforward but I wonder if there any chance to rewrite it to achieve the following:

1. Replace json_response direct call with composition.
2. Code should not become a message from Sirius: everybody should still be able to understand it just without any efforts.
3. It should be robust (composition type safety) yet easy extensible.

Any ideas?

I would not rely on assert. If Python is started with the -O option, then assertions will be stripped out and not evaluated. Better to raise an Exception here.
• asserts shouldn't be used for these types of checks (I personally don't use them at all, but if you are going to use them, they're for checking invariants)
• As for what I think you were going for with your composition point, I would just have your second function take an already json.loadsed object (or the return value of your first function) passed in.