python-json-pointer icon indicating copy to clipboard operation
python-json-pointer copied to clipboard

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

Open kjxlstad opened this issue 9 months ago • 0 comments

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'>

kjxlstad avatar May 14 '25 11:05 kjxlstad