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

Allow overriding type annotations with docstrings #143

Closed
suned opened this issue Aug 3, 2020 · 6 comments
Closed

Allow overriding type annotations with docstrings #143

suned opened this issue Aug 3, 2020 · 6 comments
Labels
extractor: pytkdocs Related to pytkdocs feature New feature or request
Projects

Comments

@suned
Copy link

suned commented Aug 3, 2020

Is your feature request related to a problem? Please describe.
I'm often less than satisfied with the types produced by mkdocstrings. A couple of examples:

When using bound generics. Consider this decorator that preserves the signature of the decorated function:

from typing import Callable, TypeVar

F = TypeVar('F', bound=Callable)

def my_decorator(f: F) -> F:
   ...

When documenting my_decorator the type field in the documentation will simply say F for the argument and return type. This doesn't really give the reader any useful information, but there aren't any other precise ways to type this decorator with the python type system. You could document the type variable as well, but it quickly becomes alot of text for the reader to look at, when really id just like the type of f and the return type of my_decorator to be Callable[..., TypeVar('A')].

When using decorators that alter the signature of a function. Consider this:

def decorator(f: Callable[..., int]) -> Callable[..., Optional[int]]:
    ....

@decorator
def function() -> int:
    ....

The actual return type of function is Optional[int], but if I document it with mkdocstrings the return type will be int, which is simply wrong.

Describe the solution you'd like
I'd like to be able to use the Google docstring type format to manually override the type presented in the docs. To take the last example:

@decorator
def function() -> int:
    """
    Returns:
        Optional[int]: The result
    """
    ...

I'd like mkdocstrings to show Optional[int] as the return type in this case.

Describe alternatives you've considered
Use a type checker to analyze types, and show the analyzed/inferred types instead of the annotated types in the documentation. It would be necessary to allow for different type-checkers in the case where specialized types are used (or e.g a MyPy plugin). This would be a huge feature, and hence I think the suggested solution is better.

Additional context
Add any other context or screenshots about the feature request here.

@suned suned added the feature New feature or request label Aug 3, 2020
@pawamoy
Copy link
Member

pawamoy commented Aug 3, 2020

Hi @suned, thanks for the detailed feature request :)

Since pytkdocs already reads type annotations in docstrings, we could consider adding a configuration option like type_source_preference with possible values docstring and annotation.

About the issue in itself: I don't know the typing system enough to formulate a clear opinion. Making pytkdocs smarter about types would be nice, but to me, right now, it seems really complicated. Using a third-party tool like mypy could be more reasonable (I don't want to reinvent the wheel here and make it square :p).

If anyone wants to help here, I'd appreciate it :)

@suned
Copy link
Author

suned commented Aug 4, 2020

we could consider adding a configuration option like type_source_preference with possible values docstring and annotation

That would be cool, but a simple scheme where one takes presedence over the other would also work if thats easier. I can't think of a use-case for using the annotated type over the documented type in cases where both are present. In addition I can report that if you have both a type annotation and a type in the docstring the behaviour is also a little wonky since the documented type ends up in the description.

@suned
Copy link
Author

suned commented Aug 4, 2020

I suppose it would be possible to implement it entirely in a template?

@pawamoy
Copy link
Member

pawamoy commented Aug 4, 2020

I can't think of a use-case for using the annotated type over the documented type in cases where both are present.

Hmmm, you're right. It would make sense to always use the type written in the docstring, even if the parameter/attribute/else is annotated. I think we can safely implement this change.

I suppose it would be possible to implement it entirely in a template?

I don't think so, currently only one of the types (annotation or docstring) is picked up by pytkdocs. You're left with no choice but to use the one it picked in the templates.

@pawamoy pawamoy added the extractor: pytkdocs Related to pytkdocs label Aug 4, 2020
@pawamoy pawamoy added this to To do in Main board via automation Aug 4, 2020
@pawamoy
Copy link
Member

pawamoy commented Sep 21, 2021

Done in pytkdocs v0.12

@pawamoy pawamoy closed this as completed Sep 21, 2021
Main board automation moved this from To do to Done Sep 21, 2021
@posita
Copy link

posita commented Sep 22, 2021

For those playing along at home, there seems to be precious little documentation around this feature in pytkdocs that I could find, but perhaps the tests help? (See, e.g., this as well as the release notes.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
extractor: pytkdocs Related to pytkdocs feature New feature or request
Projects
Main board
  
Done
Development

No branches or pull requests

3 participants