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

PT022 doesnt correct type annotations #7610

Closed
RonnyPfannschmidt opened this issue Sep 22, 2023 · 2 comments · Fixed by #7613
Closed

PT022 doesnt correct type annotations #7610

RonnyPfannschmidt opened this issue Sep 22, 2023 · 2 comments · Fixed by #7613
Assignees
Labels
bug Something isn't working fixes Related to suggested fixes for violations

Comments

@RonnyPfannschmidt
Copy link

given

import pytest
from typing import Generator


@pytest.fixture()
def testme() -> Generator[int, None, None]:
    a = 1
    yield a

after running

ruff --isolated --select PT ruff_check.py --fix

im left with

import pytest
from typing import Generator


@pytest.fixture()
def testme() -> Generator[int, None, None]:
    a = 1
    return a

instead of

import pytest
from typing import Generator


@pytest.fixture()
def testme() -> int:
    a = 1
    return a
@charliermarsh charliermarsh added the bug Something isn't working label Sep 22, 2023
@charliermarsh
Copy link
Member

Thanks for filing. Just confirming, the issue is that the type annotation should be changed to reflect that the method is no longer a generator, right?

@RonnyPfannschmidt
Copy link
Author

exactly

@dhruvmanila dhruvmanila added the fixes Related to suggested fixes for violations label Sep 23, 2023
@dhruvmanila dhruvmanila self-assigned this Sep 23, 2023
dhruvmanila added a commit that referenced this issue Sep 24, 2023
## Summary

This PR fixes the autofix behavior for `PT022` to create an additional
edit for the return type if it's present. The edit will update the
return type from `Generator[T, ...]` to `T`. As per the [official
documentation](https://docs.python.org/3/library/typing.html?highlight=typing%20generator#typing.Generator),
the first position is the yield type, so we can ignore other positions.

```python
typing.Generator[YieldType, SendType, ReturnType]
```

## Test Plan

Add new test cases, `cargo test` and review the snapshots.

fixes: #7610
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixes Related to suggested fixes for violations
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants