Recently I had the opportunity to contribute to the aiohttp library. This is one of the libraries that we, the Hotels backend team at Skyscanner, make use of on a daily basis to create a new backend service.
In this post I will briefly explain the main learnings from this contribution.
The pull request can be found here: https://github.com/KeepSafe/aiohttp/pull/1020/files
Key learnings:
Living on the edge:
Working in the master branch of a project means that sometimes it may be unhealthy. It is always good advice to check whether master is healthy or not when you submit your MR to the CI system (Travis CI in this case) before pulling your hair searching for a non existing bug in your code.
Namedtuples can be used to define a class through inheritance and by implementing the bool method,
class WebSocketReady(namedtuple('WebSocketReady', 'ok protocol')): def __bool__(self): return self.ok
it is really easy to check for a condition:
if WebSocketReady: ...
Write Unit Tests: This might be obvious, but the point is not just to write them but to write them for every piece of code created. In this case I had written tests that checked the actual functionality on a high level like:
def test_websocket_not_ready(): websocket_ready = WebSocketReady(False, None) assert websocket_ready.ok is False assert websocket_ready.protocol is None
But not the tests that checked the low level functionality like:
def test_bool_websocket_not_ready(): websocket_ready = WebSocketReady(False, None) assert bool(websocket_ready) is False
Always good to bear in mind.