You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When visiting, count the number of return Nones visited in the body. There are then two scenarios under which the diagnostic is skipped:
If the return is not annotated and if the number of return None to the total number of returns (that is to say, all the paths return None), skip the diagnostic.
If the return is annotated, and that annotation is None, skip the diagnostic.
Return annotations
I saw in the ecosystem check that functions that only return None but annotated otherwise were now being ignored. For example,
deffoo(s: str) ->str|None:
"""A very helpful docstring. Args: s (str): A string. """returnNone
would no longer report a diagnostic. I changed this back by always checking the rule if the return annotation is not None: if the return annotation is anything other than None, run the diagnostic.
I think this is more likely to be the expected behaviour, especially due to the functional overlap between docstrings and type annotations. You could argue that checking against the return type annotation should be all this patch does, but that would make the rule quite unhelpful in untyped contexts. For example,
deffoo(obj):
"""A very helpful docstring. Args: obj (object): An object. """ifobjisNone:
returnNoneprint(obj)
tjkuson
changed the title
[DOC201] Permit explicit None early returns
[DOC201] Permit explicit None in functions that only return None
Aug 22, 2024
tjkuson
changed the title
[DOC201] Permit explicit None in functions that only return None
[DOC201] Permit explicit None in functions that only return NoneAug 22, 2024
There's some situations in the ecosystem check here where the diagnostic is going away and it looks like a false negative is introduced, such as https://github.com/apache/airflow/blob/6c878866dedc182c55e18e490bff3f3a9e9c5747/airflow/providers/airbyte/operators/airbyte.py#L126. The reason there for the error going away is that the function is typed as always returning None -- looking at the function, I think it's pretty clear that it doesn't always return None, but I guess that's a bug in their type annotations rather than a bug in the logic of this PR. I think it's correct for us to believe people's type annotations in this rule.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bugSomething isn't workingdocstringRelated to docstring linting or formattingpreviewRelated to preview mode features
2 participants
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
No longer reports docstring-missing-returns (DOC201) on explicit returns in functions that only return
None.For example,
no longer reports DOC201.
Closes #13062
Methodology
When visiting, count the number of
return Nones visited in the body. There are then two scenarios under which the diagnostic is skipped:return Noneto the total number of returns (that is to say, all the paths returnNone), skip the diagnostic.None, skip the diagnostic.Return annotations
I saw in the ecosystem check that functions that only return
Nonebut annotated otherwise were now being ignored. For example,would no longer report a diagnostic. I changed this back by always checking the rule if the return annotation is not
None: if the return annotation is anything other thanNone, run the diagnostic.I think this is more likely to be the expected behaviour, especially due to the functional overlap between docstrings and type annotations. You could argue that checking against the return type annotation should be all this patch does, but that would make the rule quite unhelpful in untyped contexts. For example,
would still report DOC201.
Test Plan
cargo nextest run