furl
furl copied to clipboard
furl doesn't type-hint itself as Text
If furl were a typing.Text, it would work with static type checkers/hinters.
I like that the following works:
url_obj = furl('http://www.google.com')
response = requests.get(url_obj)
However, the static checker (I'm using PyRight) complains that furl.furl.furl does not match request.post's signature for Text | bytes.
I'm newish to Python still, but I think it would require adding the superclass:
from typing import Text
# at https://github.com/gruns/furl/blob/d0bee9a27d7f432b047194a94f64cd4ff0319f6a/furl/furl.py#L1337-L1338
class furl(URLPathCompositionInterface, QueryCompositionInterface,
FragmentCompositionInterface, UnicodeMixin, Text):
Then using a factory function named furl.
In the meantime, this is my workaround to satisfy the checker:
# in my utils/__init__.py
from typing import Text
from furl import furl as FurlOrig
class Furl(FurlOrig, Text): # bonus: capitalized class name less surprising
pass
def furl(*args, **kwargs) -> Furl:
return Furl(*args, **kwargs)
# usage in other files
from utils import furl
# use `furl` as normal
this would be handy indeed. all for this improvement
digging in, what other side effects might subclassing Text have?
For one, 'asdf'.join and furl('asdf').join are completely different things.
And also maybe typing covariance/contravariance issues?
I've since taken out my "workaround" above and posited changes from the other side, in typeshed/requests.
For one, 'asdf'.join and furl('asdf').join are completely different things.
exactly as i was curious about 🙂
I've since taken out my "workaround" above and posited changes from the other side, in typeshed/requests.
sounds good. fwiw, furl does this, too, by testing if an object has __str__() support with
def attemptstr(o):
try:
return str(o)
except Exception:
return o
either way, shall we close this issue?