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

Support External Classes' __rtruediv__ #832

Closed
1 task done
michaeljpeters opened this issue Mar 10, 2023 · 0 comments
Closed
1 task done

Support External Classes' __rtruediv__ #832

michaeljpeters opened this issue Mar 10, 2023 · 0 comments

Comments

@michaeljpeters
Copy link
Contributor

michaeljpeters commented Mar 10, 2023

Is your feature request related to a problem?

No

Describe the solution you'd like

Return NotImplemented in URL.__truediv__ when called with an improper type. This will allow external classes to extend the functionality.

Here's my dream example:

class CustomPath(PurePosixPath):
    @tp.overload
    def __rtruediv__(self, key: PurePosixPath) -> PurePosixPath:
        pass

    @tp.overload
    def __rtruediv__(self, key: URL) -> URL:
        pass

    def __rtruediv__(
        self, key: tp.Union[PurePosixPath, URL]
    ) -> tp.Union[PurePosixPath, URL]:
        if isinstance(key, URL):
            breakpoint()  # michael
            return key / str(self)
        return super().__rtruediv__(key)


if __name__ == "__main__":
    p = CustomPath("abc/file.txt")
    u = URL("http://beefslab.com/yes")
    print(f"{u=}")
    print(f"{p=}")
    print(f"{u / p=}")

Describe alternatives you've considered

Subclassing yarl.URL is not supported :(

Additional context

    def __truediv__(self, name):
        if not type(name) is str:
            return NotImplemented
        return self._make_child((name,))

Code of Conduct

  • I agree to follow the aio-libs Code of Conduct
@michaeljpeters michaeljpeters changed the title Support for External Classes' __rtruediv__ Support External Classes' __rtruediv__ Mar 10, 2023
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