Token Bucket context manager for rate limiting async calls
Parent-less class

class TokenBucket():


can drop the parens.

lower_snake_case

These variables:


tokensPerS
refillSleepS
sleepDuration


etc., and all of your methods, should be in lower_snake_case instead of camelCase, by convention.

Type hints

PEP484 allows you to turn this

def hasTokens(self):


into this

def hasTokens(self) -&gt; bool:


and likewise for your other method parameters and returns. Among other things, it will allow you to drop your types from comments like this:

"""
    tokens (collections.deque): The tokens (which manifest as timestamps)
                                in reverse chronological order.
    last (float):               The most recent token consumed.
    rate (float):               See __init__ arg `tokensPerS`.
    capacity (int):             See __init__ arg `capacity`.
    sleepDuration (float):      See __init__ arg `refillSleepS`.
"""


because they'll already be in the signature.