Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specifying return type for inherited function #389

Open
jat255 opened this issue Oct 2, 2024 · 2 comments
Open

Specifying return type for inherited function #389

jat255 opened this issue Oct 2, 2024 · 2 comments

Comments

@jat255
Copy link

jat255 commented Oct 2, 2024

I have a basic inherited class structure that looks more or less like this, with some methods in the base class that return objects of the same type:

from typing import Self

class A(SomeOtherBaseClass):
    ...
    
    def method_1(self) -> Self:
        modified_copy = self.deepcopy()
        return modified_copy
    
    def method_2(self) -> Self:
        modified_copy = self.deepcopy()
        return modified_copy
    ...

class B(A):
    def sub_class_method_b():
        ...

class C(A):
    def sub_class_method_c():
        ...

To type hint the methods in class A, I've specified the return type as typing.Self, which works fine in my editor. When building the docs using python-apigen, the entity pages for classes B and C look something like this:

image

I have also tried type-hinting with the name of the parent class, like this:

class A(SomeOtherBaseClass):
    ...
    
    def method_1(self) -> "A":
        modified_copy = self.deepcopy()
        return modified_copy
    
    def method_2(self) -> "A":
        modified_copy = self.deepcopy()
        return modified_copy
    ...

class B(A):
    def sub_class_method_b():
        ...

class C(A):
    def sub_class_method_c():
        ...

That also works in my editor, but results in incorrect documentation pages, that show the methods always return an object of class A, even on the pages for class B and C:

image

Ideally, the correct interpretation would be that those methods would return type B on the B page, and C on the C page. Is there anyway to configure things so this is the output?

@jbms
Copy link
Owner

jbms commented Oct 3, 2024

There isn't currently an option to do that, but it could potentially be implemented similar to how type parameters of inherited methods are also resolved.

Arguably Self is useful to display if the user might inherit from the class. If users aren't expected to inherit from the class then just showing the resolved return type could make sense.

@jat255
Copy link
Author

jat255 commented Oct 11, 2024

Ok, thanks for the context. Feel free to leave this open as a feature request, or close it if you wish. It would be nice if it worked the way I mentioned, but far from a deal-breaker on how useful this theme is!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants