Skip to content

Support External Classes' __rtruediv__ #832

@michaeljpeters

Description

@michaeljpeters

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions