Contextualize scheme registration API #28
Description
Activity
I'm generally down for this kind of contextualization, but I must say that I would not want it to happen this way. This kind of invokes a JavaScript-like, prototype-driven approach. The explicit context approach I prefer is typified in ashes, where there's a default context implicit.
This is especially true in this case, where
register_scheme(and the future context object) are very very rarely touched. They're mostly there as escape valves for working with older versions of the library in the far flung future when many more schemes have been registered, or in case I've got a bug in the data in the default registry and they can't upgrade immediately.Anyways, in short, yes, but I definitely wouldn't want
schemeshanging out on theURLtype's API.- changed the title
[-]Localize registration API[/-][+]Contextualize scheme registration API[/+]on Jul 3, 2017 (renamed bc "localize" makes me think l10n ;) )
I'd be happy if we can avoid an import-time side effect. I'm always up for more dependency injection, and while a context object that encapsulates the scheme registry and that users can definitely avoids mutating global state, it presents its own design challenges:
register_schemeadds schemes tohyperlink, whereas ashes' contexts share nothing with default behavior, except perhaps through inheritance. Ahyperlinkcontext object would have to either chain itself to existing context objects or allow itself to be cloned and then mutated. That's no less like prototypes than returning aURLfactory that knows about new schemes, so if one is unidiomatic so is the other.- The various methods that create
hyperlink.URLinstances --URL.__init__,URL.from_text, andURL.replace-- would have to take a new argument. They already take a lot of arguments. This isn't a huge deal and will be less of a deal if closing remove "uses_netloc" parameter and attribute to .__init__ and .replace #24 leads toSchemeobjects.
hyperlink.register_schememutates global state. That's convenient but amounts to an import time side effect. If I do this in modulea:I have to do this in module
b:Scheme registration should be localized, returning a new
URL-like object that knows about the registered schemes. That would letalook like this:So that
bcould do this:A context manager might be useful, too: