Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

furl doesn't type-hint itself as Text #146

Open
Kache opened this issue Jul 26, 2021 · 3 comments
Open

furl doesn't type-hint itself as Text #146

Kache opened this issue Jul 26, 2021 · 3 comments

Comments

@Kache
Copy link

Kache commented Jul 26, 2021

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
@gruns
Copy link
Owner

gruns commented Aug 16, 2021

this would be handy indeed. all for this improvement

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

@Kache
Copy link
Author

Kache commented Aug 17, 2021

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.

@gruns
Copy link
Owner

gruns commented Aug 17, 2021

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants