Skip to content

__new__ shouldn't hardcode the return types #3107

@multimeric

Description

@multimeric

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:

def __new__(cls, value: str, base: str | None = None) -> URIRef:

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"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions