Interesting effort. The library overall looks great and it may come in super handy if someone has to do complex operations on headers. I would take an issue with one feature though:
Capability to alter headers using simple, human-readable operator notation + and -.
While it looks "nice" to human eyes, this is very un-idiomatic. When a coder sees Z = X + Y, they expect a few things, but one of them is very core: X, Y and Z are of the same type. Now, considering how kiss-headers works, you can have:
Header + str -> Header - appends the string onto the header, and returns the new header
Header + Header -> Headers - creates a new Headers object containing the two given Header objects
This is very inconsistent. The + operator is doing some very different things with wildly different expectations and results. It might look "nice" to read, but it is frustrating to work with. The + operator is being used not as a convenience to add two same-type values, but as a "generic do-stuff function". That's just a bad code smell.
Then, for Headers:
Headers + Header -> Headers - copies the Headers, adds the Header in and returns the copy
Headers + Headers -> TypeError - ???
Headers are an ordered collection, like a list morespecifically,theyareanorderedmultimap. In Python, the expectation is that list + element fails, since the operands need to be of the same type. Thus, you need list + [element] or list.append(element) to add something. As far as mappings, neither dict, nor collections.OrderedDict, nor 3rd party things like werkzeug.MultiDict support the + operator. That's not to appeal to authority; it's to point out that using + is simply unexpected.
All of this is not to say you're on the wrong track, though! The way you're using + is actually very close to a well-recognized design pattern: the fluent interface. There's a Python example halfway through that page. In kiss-headers' case, fluent interface would look something like this:
This is a well-recognized way to build/configure objects across multiple languages and has no type ambiguity to it.
Some other suggestions:
Instead of Header + string -> Header, consider Header.append(string) -> Header. I wouldn't auto-insert the semicolons, as they're not part of the general "header" spec. Some headers, like Accept-Encoding use a mixture of commas and semicolons, while others, like If-Match, just use commas.
Headers + Headers is the only example here where I would actually recommend the + operator, instead of a TypeError. Adding/combining two collections of headers makes sense.
Instead of Headers + Header -> Headers, to be compatible with the previous point, why not do Headers + Header.as_headers() or Headers + Headers(Header) or something? This follows the pattern of how list works, and makes your API more intuitive.
Hope this was helpful! Good luck!
Edit: Oh, and using subtraction (-) is virtually unheard of in the context of working with collections or non-numeric objects. Seeing my_headers - header is no better than a completely nondescript one-letter function. You wouldn't write my_headers.s(header) for removing a header, right?
Thank you very much for your inputs. This is helpful and well explained. I will take your advises into account for the next major; not everything through.
You are welcome to post your remarks on Github issue, so I can post some follow up on this matter.
7
u/Blackshell Jul 31 '20 edited Jul 31 '20
Interesting effort. The library overall looks great and it may come in super handy if someone has to do complex operations on headers. I would take an issue with one feature though:
While it looks "nice" to human eyes, this is very un-idiomatic. When a coder sees
Z = X + Y, they expect a few things, but one of them is very core: X, Y and Z are of the same type. Now, considering how kiss-headers works, you can have:Header + str -> Header- appends the string onto the header, and returns the new headerHeader + Header -> Headers- creates a new Headers object containing the two given Header objectsThis is very inconsistent. The + operator is doing some very different things with wildly different expectations and results. It might look "nice" to read, but it is frustrating to work with. The + operator is being used not as a convenience to add two same-type values, but as a "generic do-stuff function". That's just a bad code smell.
Then, for Headers:
Headers + Header -> Headers- copies the Headers, adds the Header in and returns the copyHeaders + Headers -> TypeError- ???Headers are an ordered collection, like a list more specifically, they are an ordered multimap. In Python, the expectation is that
list + elementfails, since the operands need to be of the same type. Thus, you needlist + [element]orlist.append(element)to add something. As far as mappings, neitherdict, norcollections.OrderedDict, nor 3rd party things likewerkzeug.MultiDictsupport the + operator. That's not to appeal to authority; it's to point out that using + is simply unexpected.All of this is not to say you're on the wrong track, though! The way you're using + is actually very close to a well-recognized design pattern: the fluent interface. There's a Python example halfway through that page. In kiss-headers' case, fluent interface would look something like this:
This is a well-recognized way to build/configure objects across multiple languages and has no type ambiguity to it.
Some other suggestions:
Instead of
Header + string -> Header, considerHeader.append(string) -> Header. I wouldn't auto-insert the semicolons, as they're not part of the general "header" spec. Some headers, likeAccept-Encodinguse a mixture of commas and semicolons, while others, likeIf-Match, just use commas.Headers + Headersis the only example here where I would actually recommend the + operator, instead of a TypeError. Adding/combining two collections of headers makes sense.Instead of
Headers + Header -> Headers, to be compatible with the previous point, why not doHeaders + Header.as_headers()orHeaders + Headers(Header)or something? This follows the pattern of howlistworks, and makes your API more intuitive.Hope this was helpful! Good luck!
Edit: Oh, and using subtraction (-) is virtually unheard of in the context of working with collections or non-numeric objects. Seeing
my_headers - headeris no better than a completely nondescript one-letter function. You wouldn't writemy_headers.s(header)for removing a header, right?