If Python had macros this library would have a very different implementation indeed.
I think 200LOC and a complex registration/namespace/cache system is a bit much to avoid an if statement around a def for a handful of functions. This is especially true since this just turns the if into a decorator, and imports will still need to be done the old way for compatibility. The cognitive overhead of this is not a good tradeoff.
You're absolutely right! I built this mainly to see if it could be done. I noticed uvicorn's main function uses an if-statement like you described, and I wondered what it would look like as a decorator.
My next goal is to see if I can make the resolution "static", i.e. Once all the functions are loaded, avoid the dictionary lookup at runtime, and just assign the appropriate function version as the bare module attribute.
It's fun to do things like this in python, simply because you can. But it's also important to point out that there's a lot of things you _shouldn't_ (this may be one of them!). I'm glad to see this sparked some discussion!
If Python simply parsed and evaluated top-level expressions one by one, you could test the value of some version variable and act accordingly. Like for this range of versions, load this file, otherwise load that one.
If I think of this problem in a Lisp frame, it does not scream "use macros" at me.
I think 200LOC and a complex registration/namespace/cache system is a bit much to avoid an if statement around a def for a handful of functions. This is especially true since this just turns the if into a decorator, and imports will still need to be done the old way for compatibility. The cognitive overhead of this is not a good tradeoff.