Skip to content

Feature request: make join() return self.__class__ to support subclassing #64

Description

@kjxlstad

Currently, the JsonPointer.join() method always returns a JsonPointer instance, even when called from a subclass:

class Pointer(jsonpointer.JsonPointer):
    ...

base = Pointer("/some/object")
end = Pointer("/actual/value")
full = base.join(end)

print(type(full))
# <class 'jsonpointer.JsonPointer'>

If join() (and probably other methods) instead returned instances of self.__class__ instead of JsonPointer normal behavior would stay the same while allowing subclasses to work as expected.

E.g. with join() as:

def join(self, suffix):
    """ Returns a new JsonPointer with the given suffix append to this ptr """
    if isinstance(suffix, self.__class__):
        suffix_parts = suffix.parts
    elif isinstance(suffix, str):
        suffix_parts = self.__class__(suffix).parts
    else:
        suffix_parts = suffix
    try:
        return self.__class__.from_parts(chain(self.parts, suffix_parts))
    except:  # noqa E722
        raise JsonPointerException("Invalid suffix")

You'd get:

print(type(JsonPointer("/some/object") / JsonPointer("/actual/value")))
# <class 'jsonpointer.JsonPointer'>

print(type(Pointer("/some/object") / Pointer("/actual/value")))
# <class '__main__.Pointer'>

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions