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

False positive DOC405 #126

Closed
ddelange opened this issue Feb 21, 2024 · 7 comments · Fixed by #205
Closed

False positive DOC405 #126

ddelange opened this issue Feb 21, 2024 · 7 comments · Fixed by #205

Comments

@ddelange
Copy link

Hi 👋

The following throws a

164: DOC405: Function `yields` has both "return" and "yield" statements. Please use Generator[YieldType, SendType, ReturnType] as the return type annotation, and put your yield type in YieldType and return type in ReturnType. More details in https://jsh9.github.io/pydoclint/notes_generator_vs_iterator.html

false positive. Interestingly, when I remove the Args section from the docstring, the false positive is no longer thrown.

from contextlib import contextmanager


@contextmanager
def yields(db: Optional[int]) -> Iterator[int]:
    """Test a function.

    Args:
        db: the database

    Yields:
        Some stuff.
    """
    if db is not None:
        yield db
        return

    db = ...
    yield db
@jsh9
Copy link
Owner

jsh9 commented Mar 1, 2024

Hi @ddelange , did you check out the page that's included at the end of the error message: https://jsh9.github.io/pydoclint/notes_generator_vs_iterator.html

Do you think what the page says makes sense?

@ddelange
Copy link
Author

ddelange commented Mar 1, 2024

Hi @jsh9 👋

It should definitely be legal to annotate the above function as Iterator[int]. The return statement is only present for (early) exit, not to actually return a value (which would need to be mentioned in the docstring). This example only ever yields an int.

From python docs:

In a generator function, the return statement indicates that the generator is done and will cause StopIteration to be raised. The returned value (if any) is used as an argument to construct StopIteration and becomes the StopIteration.value attribute.

@jabbera
Copy link

jabbera commented May 28, 2024

I just ran into this as well and without a way to disable errors line by line I've had to remove pydoclint from my pre-commit

@jsh9
Copy link
Owner

jsh9 commented May 29, 2024

Hi @jabbera and @ddelange ,

What do you think of the following logic?

  • If there is a bare return (i.e., just return without anything behind it, not even return None), suppress DOC405
  • As long as there is anything after return, raise DOC405

The purpose of DOC405 is to persuade users to use a Generator when they are actually returning something. And I guess this use case of yours is an edge case.

@ddelange
Copy link
Author

ddelange commented May 29, 2024

sounds good:) typing docs corroborate this approach: Generator[int, None, None] may be typed as Iterator[int].

since return None and return are equivalent statements, both fall under this category imo, and some linters might force either of both for style.

@jabbera
Copy link

jabbera commented May 29, 2024

@jsh9 I think that sounds good. I actually went back and baselined with the error to get it suppressed so I'm actually good, but would love to get rid of the baseline!

@jsh9
Copy link
Owner

jsh9 commented Jan 13, 2025

The fix is published in version 0.5.19.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants