rdflib icon indicating copy to clipboard operation
rdflib copied to clipboard

`__new__` shouldn't hardcode the return types

Open multimeric opened this issue 7 months ago • 1 comments

When __new__ is overridden, the return type should be Self, or just left empty for the type checker to infer. Currently some of these are hardcoded to the class name:

https://github.com/RDFLib/rdflib/blob/9f76b0fc7b9d6a01cc2e08cad29ac54acb60c026/rdflib/term.py#L306

The issue with this is that any child class UriSubclass that tries to inherit from it and call super().__new__() will think it has an instance of UriSubclass and not Child:

class UriSubclass(URIRef):
    def __new__(cls, value: str, base: str | None = None) -> Self:
        return super().__new__(cls, value, base)

Gives:

Type "URIRef" is not assignable to return type "Self@UriSubclass"

multimeric avatar Apr 01 '25 03:04 multimeric