-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Should we support type annotations in docstrings for Python 2 code? #612
Comments
I don't really like this. It feels conflicting with the Python philosophy, "There should be one way to do it." (I might have slightly misquoted it...) |
@kirbyfan64 Which syntax would you prefer for Python 2 code? |
@JukkaL I thought mypy was going to use a decorator? Maybe something like this could be used:
|
The main use case for decorators was function overloading, as runtime dispatch needs access to the type objects, and decorators could easily support this. But PEP 484 only supports overloading in stubs, so this use case no longer exists. Thus we could have Python 2 annotations in comments without issues. I don't have much enthusiasm for decorators mainly for aesthetic reasons: they make my code look "noisy" compared to normal Python code (including code with Python 3 function annotations). I think that the syntax alternatives are closer to the look of normal Python code. However, using a comment together with a docstring looks pretty bad to me: def join_name(first_name, last_name):
# type: (str, str) -> str
"""Join first and last name into a full name.
... more discussion ...
"""
return first_name + ' ' + last_name This isn't much better, and the type annotation is easy to miss: def join_name(first_name, last_name):
"""Join first and last name into a full name.
... more discussion ...
"""
# type: (str, str) -> str
return first_name + ' ' + last_name |
@JukkaL Yeah...this is probably a lose-lose situation... |
Another ideas from PyCharm IDE. Using def first_name(name):
"""Return the first name from a name with both a first and a last name.
:type name: str
:rtype: str
"""
return name.split()[0] Using def first_name(name):
"""Return the first name from a name with both a first and a last name.
@type name: str
@rtype: str
"""
return name.split()[0] Using the def first_name(name):
"""Return the first name from a name with both a first and a last name.
:param str name: The full name, such as 'John Smith'.
:rtype: str
"""
return name.split()[0] The |
FWIW we decided against this, at least in the short term -- instead we're using a comment-based notation that is now added to PEP 484 (and also to PEP 8). |
We experimented with it using some Dropbox code but weren't very successful and the main problems were these:
|
I'm tagging this as postponed. |
Should we just close this? We can always reopen this if we change our minds. |
Yeah, we're not going to do this.
|
We could support an alternative, docstring-based syntax for types in programs that need to be Python 2 compatible. The idea is from http://sphinxcontrib-napoleon.readthedocs.org/en/latest/example_google.html
Example:
Mypy would threat this as equivalent to this:
A bunch of details would have to be fleshed out before implementing this. I'm adding this issue to get feedback on whether people would consider this useful. I've started seeing code using the above convention (though with slightly ad-hoc types).
Note that we can support
# type:
annotations as an alternative. They could be pretty useful for small, internal functions that may not have a docstring at all. The docstring syntax would be pretty verbose if used for every function. Example:Maybe we should extend the docstring syntax to have optional shorter forms:
Another shorter form:
Another alternative for Python 2 code is to use the mypy codec that supports Python 3 type annotations in Python 2 code by automatically stripping them away. This has a few problems:
The text was updated successfully, but these errors were encountered: