Add __contains__ to State

There seems to be no way to check whether state has a key, apart from accessing the internal _state property.

By adding the __contains__ to State, you would be able to use the in keyword.

class State(object):
    --snip--

    def __contains__(self, key: typing.Any) -> bool:
        return key in self._state

This would let you use something like the following snippet

# Initialize DB
if db not in app.state:
    app.state.db = DB()

Wouldn’t it be be as effective to catch AttributeError?

It would be possible to do it that way, yeah. But as effective, I disagree. The exception has to be constructed, and that includes gathering the stack trace and such.

Also for reading comprehension, compare the two following snippets:

try:
    return request.app.state.db
except AttribugeError:
    request.app.state.db = DB()
    return request.app.state.db
if "db" not in request.app.state:
    request.app.state.db = DB()

return request.app.state.db

I find the second one much more readable.