You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Not necessarily intentional, no. Any idea how we could return such a keyview object? I don't know how we can construct one from something that's not a dict.
Okay I found it — we can use typing.KeysView(<dict>), typing.ValuesView(<dict>), etc.
Thinking about this more, I think this was an intentional choice, basically to keep the scope down a bit.
Eg one reason why we cannot very easily implement these interfaces (eg building a dict and returning the result of calling .keys() on it) is because duplicate headers are not forbidden by the HTTP spec. Set-Cookie is a prime example of this.
That doesn't mean we can't implement the KeysView/ValuesView/ItemsView protocols, though, but we'd need to implement our own subclasses, and basically end up with what multidict is doing. Making that work with type hints etc would be a significant chunk of code and work.
Don't think we're ready to make that move yet… But I'd personally be interested in seeing what making this work would take.
I think the easiest way would likely be something similar to how aiolibs multidict does it, yeah; using the default KeysView, ValuesView wouldn't work well for the reasons you mention although it's worth pointing out that the XView constructors accept general mappings (and using stale dict snapshots to implement the views would violate the protocol anyway).
So, switching to multidict is not as simple unfortunately, and probably not possible, because we already have some subtle behavior differences between our QueryParams and Headers models, and how multidict behaves.
Eg:
Currently params[key] returns the first value in the list of items for key, while multidict returns the last.
multidict doesn't allow efficiently iterating over key-unique items (like a dict), so we have to do dict(md).keys() to get unique keys for queryparams (while multikeys is what headers expect).
Activity
florimondmanca commentedon Apr 9, 2020
Not necessarily intentional, no. Any idea how we could return such a keyview object? I don't know how we can construct one from something that's not a dict.
florimondmanca commentedon Apr 9, 2020
Okay I found it — we can use
typing.KeysView(<dict>),typing.ValuesView(<dict>), etc.Thinking about this more, I think this was an intentional choice, basically to keep the scope down a bit.
Eg one reason why we cannot very easily implement these interfaces (eg building a dict and returning the result of calling
.keys()on it) is because duplicate headers are not forbidden by the HTTP spec.Set-Cookieis a prime example of this.That doesn't mean we can't implement the
KeysView/ValuesView/ItemsViewprotocols, though, but we'd need to implement our own subclasses, and basically end up with whatmultidictis doing. Making that work with type hints etc would be a significant chunk of code and work.Don't think we're ready to make that move yet… But I'd personally be interested in seeing what making this work would take.
obataku commentedon Apr 9, 2020
I think the easiest way would likely be something similar to how aiolibs
multidictdoes it, yeah; using the defaultKeysView,ValuesViewwouldn't work well for the reasons you mention although it's worth pointing out that theXViewconstructors accept general mappings (and using staledictsnapshots to implement the views would violate the protocol anyway).florimondmanca commentedon Apr 11, 2020
So, switching to
multidictis not as simple unfortunately, and probably not possible, because we already have some subtle behavior differences between ourQueryParamsandHeadersmodels, and how multidict behaves.Eg:
params[key]returns the first value in the list of items forkey, whilemultidictreturns the last.multidictdoesn't allow efficiently iterating over key-unique items (like a dict), so we have to dodict(md).keys()to get unique keys for queryparams (while multikeys is what headers expect).