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

[F821] Improve error message for anext/aiter #13287

Closed
JP-Ellis opened this issue Sep 9, 2024 · 6 comments · Fixed by #13293
Closed

[F821] Improve error message for anext/aiter #13287

JP-Ellis opened this issue Sep 9, 2024 · 6 comments · Fixed by #13293
Assignees
Labels
rule Implementing or modifying a lint rule

Comments

@JP-Ellis
Copy link

JP-Ellis commented Sep 9, 2024

Summary

The lint F821 has a false positive with anext.

As pointed out by @MichaReiser, this ultimately is due to having either project.requires-python or tools.ruff.target-version set to a too lower version.

In this instances, it was discovered that Ruff was targeting Python 3.8 (from an erroneous config).

Example

async def foo(iter: AsyncGenerator[str, None]):
    assert await anext(iter)

Desired Behaviour

Where a symbol is introduced in a more recent version of Python, Ruff should:

  1. Still raise F821 as it is strictly correct; but also,
  2. Inform the use Ruff is assuming Python 3.x, and the symbol in question was only introduced in Python 3.y, and nudge the user to set reures-python or target-version.

Environment

ruff --version
ruff 0.6.4python --version
Python 3.12.5
@evanarlian
Copy link

False positive on aiter as well.

@MichaReiser MichaReiser added the question Asking for support or clarification label Sep 9, 2024
@MichaReiser
Copy link
Member

Have you specified requires-python in your pyproject.toml or a target-version in your ruff.toml? I'm asking because anext is only available in Python 3.10 and newer (and the default is Python 3.8)

@JP-Ellis
Copy link
Author

JP-Ellis commented Sep 9, 2024

Good catch. I have a monorepo and the root project has

[project]
# ...
requires-python = "~=3.12.0"

[tool.ruff]
target-version = "py312"

and one of the sub-packages has

[project]
# ...
requires-python = "~=3.12.0"

[tool.ruff]
target-version = "py38"

I can confirm that changing from py38 to py312 fixes this.

In cases like there where there is a new symbol introduced in a more recent version of Python, should ruff instead suggest/direct the end-user to update the settings?

@MichaReiser
Copy link
Member

In cases like there where there is a new symbol introduced in a more recent version of Python, should ruff instead suggest/direct the end-user to update the settings?

That's a neat idea. I think the error message could possibly be improved by saying that the symbol is only defined in Python XX or newer.

@JP-Ellis JP-Ellis changed the title [F821] False positive: anext [F821] Improve error message for anext/aiter Sep 9, 2024
@JP-Ellis
Copy link
Author

JP-Ellis commented Sep 9, 2024

I have updated the original issue to incorporate the suggestion that Ruff provide a better suggestion in this case.

@MichaReiser MichaReiser added rule Implementing or modifying a lint rule help wanted Contributions especially welcome and removed question Asking for support or clarification labels Sep 9, 2024
@AlexWaygood AlexWaygood self-assigned this Sep 9, 2024
@AlexWaygood AlexWaygood removed the help wanted Contributions especially welcome label Sep 9, 2024
jaraco added a commit to jaraco/zipp that referenced this issue Sep 9, 2024
@MichaReiser
Copy link
Member

Thanks @AlexWaygood !

VigdisIS added a commit to volur-ai/python-volur-sdk that referenced this issue Sep 19, 2024
VigdisIS added a commit to volur-ai/python-volur-sdk that referenced this issue Sep 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rule Implementing or modifying a lint rule
Projects
None yet
4 participants