python-hyper/hyperlink

rooted flag exposes serialization ambiguity

glyph opened this issue · 3 comments

glyph commented

This should not be possible:

>>> import hyperlink
>>> a = hyperlink.URL(path=['', 'foo'], rooted=False)
>>> b = hyperlink.URL(path=['foo'], rooted=True)
>>> a == b
False
>>> a
URL.from_text('/foo')
>>> b
URL.from_text('/foo')
>>> a.normalize() == b.normalize()
False

I think that if path ever starts with a '' when rooted=False, we ought to flip the flag and remove the path segment.

I would think that raising an exception would be more appropriate when the caller provides conflicting inputs, no?

glyph commented

the input isn't exactly conflicting; if you're prepending or appending something to the path, you could be doing it blindly and expecting hyperlink to "do the right thing" which indeed it could do, at least in this case; the initial '' segment is semantically equivalent to making it be rooted, and understanding that rooted is a separate flag is a bit of a finicky implementation detail.

Then again, check out this hot nonsense:

>>> a = hyperlink.URL.from_text("https://www.example.com/foo")
>>> b = a.replace(rooted=False)
>>> a.rooted
True
>>> b.rooted
False
>>> a
URL.from_text('https://www.example.com/foo')
>>> b
URL.from_text('https://www.example.com/foo')
>>> a == b
True

There seems to be another bug that makes == work correctly in this case, but rooted should absolutely not be possible to set to False on an URL object with an authority present.

glyph commented

Related: #24