-
Notifications
You must be signed in to change notification settings - Fork 15
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
Add shortcuts for long typing.Annotated type hints #103
Comments
Yes, sure! Contributions are very welcome. |
Great, I will fork the repository and send a merge request |
While working on the new feature, I came along the There is, however, a more pressing issue with the def annotate(name: str) -> str:
"""A field.
Args:
name (str): The name of the field.
Returns:
str: Returns the name of the field.
"""
return name
def test(a: Annotated[str, annotate("This is a parameter.")]) -> str:
"""Performs a test.
Args:
a (Annotated[str, annotate("This is a parameter.")]): The parameter a.
Returns:
str: Returns the value of the parameter a.
"""
return a This is obviously wrong, since both type hints are the same to the dot. "Annotated" could be added to the regular expression in I was thinking that the quotes could be stripped after the unparsing, i.e., two type hints are considered equal if they only differ in quotes. In this case all quotes could be removed without having to deal with special cases like Also, this is definitely an edge case that will probably never happen, but the stripping of the single backticks in the T = TypeVar('T')
def test2(t: T) -> None:
"""Performs a test.
Args:
t (`T`): The parameter.
"""
pass Finally, I have noticed that unlike the def do_work() -> Literal["Success", "Error"]:
"""Performs work.
Returns:
Literal["Success", "Error"]: Returns "Success" or "Error".
"""
return "Success" The reason is that the return type in the function declaration was parsed, which results in the double quotes being normalized to single quotes, while return type in the docstring does not get parsed, so the double quotes remain. |
Some frameworks, such as FastAPI make heavy use of
typing.Annotated
to add metadata to function parameters. This becomes unwieldy very fast, if the entire annotation has to be added in the docstring. For example, consider the following pseudo-code for a FastAPI function that gets invoked for an HTTP GET request to "/{id}":The metadata can become arbitrarily large and I would bet there are more frameworks out there that also use
typing.Annotated
in this way.I want to propose to add an exception for
typing.Annotated
. I have read Cases that pydoclint is not designed to handle and Notes on writing type hints and I understand that using the AST module does not allow us to derive any semantic meaning about type annotations, but I think it would still be beneficial to make an exception on matching names alone, i.e., when the name of the type starts withAnnotated[
ortyping.Annotated[
, then the user can substitute the actual type hint with a short hand.Since the actual type that results from the annotation is the type specified as the first argument to
typing.Annotated
, I would propose that the actual type should be allowed as a short hand. So instead of adding the entire type to the docstring, it would suffice to specify the first argument ofAnnotated
. For example, for the function definition above, PyDocLint should not raise a validation error, when the following docstring would be added:I have had a look at PyDocLint's code and I believe (besides updating tests and docs), the only thing that needs to be changed is in the
Arg._typeHintsEq
method in thepydoclint/utils/arg.py
file. I believe the following heuristic would be enough to implement this:Before submitting a pull request, I wanted to get feedback from you. Would you be willing to include an exception for
typing.Annotated
?The text was updated successfully, but these errors were encountered: