furl icon indicating copy to clipboard operation
furl copied to clipboard

furl doesn't type-hint itself as Text

Open Kache opened this issue 4 years ago • 3 comments

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

Kache avatar Jul 26 '21 22:07 Kache

this would be handy indeed. all for this improvement

digging in, what other side effects might subclassing Text have?

gruns avatar Aug 16 '21 22:08 gruns

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.

Kache avatar Aug 17 '21 00:08 Kache

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?

gruns avatar Aug 17 '21 19:08 gruns