elements-of-python-style icon indicating copy to clipboard operation
elements-of-python-style copied to clipboard

Try to use type hints

Open Zeroto521 opened this issue 4 years ago • 1 comments

Since Python3.5 supports for type hints. It could be better than writing docstring at sometimes.

Such as follow example. We can learn from code a lot directly rather than docstring.

Before

def get(url, qsargs=None, timeout=5.0):
    """Send an HTTP GET request.

    :param url: URL for the new request.
    :type url: str
    :param qsargs: Converted to query string arguments.
    :type qsargs: dict
    :param timeout: In seconds.
    :rtype: mymodule.Response
    """
    return request('get', url, qsargs=qsargs, timeout=timeout)

Later

from typing import Dict, Optional

def get(url: str, qsargs: Optional[Dict] = None, timeout: float = 5.0) -> mymodule.Response:
    """Send an HTTP GET request.

    :param url: URL for the new request.
    :param qsargs: Converted to query string arguments.
    :param timeout: In seconds.
    """
    return request('get', url, qsargs=qsargs, timeout=timeout)

Zeroto521 avatar May 20 '20 15:05 Zeroto521

Love this suggestion! Thanks @Zeroto521.

This is also related to #45 which I opened a couple weeks ago, and to PR #4 (which has been open way too long, I realize).

I also recently discovered myst-parser (as I tweeted here) which seems like it provides a Markdown-ish way to provide docstrings and have them still be compatible with Sphinx. The question of how & whether to mix standard reST, Markdown, typing, and sphinx is definitely a question of style and deserves good coverage here!

amontalenti avatar May 25 '20 18:05 amontalenti