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

Provide to_dict method or make URL a mapping #989

Closed
1 task done
dhirschfeld opened this issue Feb 2, 2024 · 3 comments
Closed
1 task done

Provide to_dict method or make URL a mapping #989

dhirschfeld opened this issue Feb 2, 2024 · 3 comments

Comments

@dhirschfeld
Copy link

Is your feature request related to a problem?

It would be useful to be able to split a yarl.URL in a dictionary of it's constituent parts.

Describe the solution you'd like

I have a little helper function:

def to_dict(url: yarl.URL) -> dict:
    return dict(
        scheme=url.scheme,
        user=url.user,
        password=url.password,
        host=url.host,
        port=url.port,
        path=url.path,
        query=url.query,
        fragment=url.fragment,
    )
def test_to_dict(url: yarl.URL) -> None:
    assert yarl.URL.build(**to_dict(url)) == url

It would be convenient to either have this as a method or make the yarl.URL a mapping such that dict(url) would do the same thing as the to_dict function above.

Describe alternatives you've considered

Keep on defining my little helper whenever I use yarl 😢

Additional context

No response

Code of Conduct

  • I agree to follow the aio-libs Code of Conduct
@dhirschfeld
Copy link
Author

A mapping can be implemented by providing keys and __getitem__ methods - e.g.

class MappingURL(wrapt.ObjectProxy):

    def __init__(self, wrapped: yarl.URL):
        super().__init__(wrapped)

    def keys(self) -> list[str]:
        return [
            'scheme',
            'user',
            'password',
            'host',
            'port',
            'path',
            'query',
            'fragment',
        ]

    def __getitem__(self, key):
        return getattr(self, key)

image

@webknjaz
Copy link
Member

Hello, I see you jumped to the change you want in the project. Could you also explain your use case like I'm five? I'm struggling to understand why one would want to turn a well-defined type-checkable/lintable structure into a dict of arbitrary non-semantic keys and values... As things stand, I view this as a bad idea.

@webknjaz
Copy link
Member

webknjaz commented Nov 8, 2024

Closing per above.

@webknjaz webknjaz closed this as not planned Won't fix, can't repro, duplicate, stale Nov 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants