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

Update Ruff and enable F821 in stubs #11771

Merged
merged 1 commit into from Apr 17, 2024
Merged

Conversation

Avasam
Copy link
Sponsor Collaborator

@Avasam Avasam commented Apr 16, 2024

Thanks to @AlexWaygood 's work on astral-sh/ruff#3011 , we no longer need to ignore F821 in Ruff configs, whilst still preferring to avoid forward-references.

I've left in F821 from Flake8 as per my in-code comment: typeshed being a testing ground for F821, it's great to catch changes to the monkeypatching of F821

Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I actually wouldn't be opposed to ignoring flake8's version of F821 now, but we can leave that discussion for another time/it's a separate thing that should probably be considered separately

Copy link
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@Avasam
Copy link
Sponsor Collaborator Author

Avasam commented Apr 16, 2024

I actually wouldn't be opposed to ignoring flake8's version of F821 now, [...]

Me neither, but I figured some of the reasoning about testing / feedback loop in #11734 (comment) could apply here as well. Although Flake8-pyi's unit tests should essentially cover changes and regressions.

Happy to make a follow-up PR to see what others think (mainly from flake8-pyi maintainers).

@@ -132,9 +132,6 @@ if sys.version_info >= (3, 12):
ContextManager = AbstractContextManager
AsyncContextManager = AbstractAsyncContextManager

# This itself is only available during type checking
def type_check_only(func_or_cls: _F) -> _F: ...
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why were these moved? This is legal in stubs, even if it might be better style to move the function after the definition of _F.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ruff does not currently support generalised forward references in stub files in the same way that mypy and pyright do. We considered implementing that support, but when we looked, we couldn't actually find a reference anywhere in any PEP or typing spec that specified that all forward references should be allowed in stub files. We therefore decided to only support forward references in situations where it could plausibly be useful to have forward references, such as class bases. See astral-sh/ruff#10779 (comment).

This is similar in concept to the limited way flake8-pyi monkeypatches flake8. pyflakes-monkeypatched-by-flake8-pyi will allow this:

class Foo(list[Bar]): ...
class Bar: ...

But it won't allow this, because there's no known use case for it:

class Foo(Bar): ...
class Bar: ...

(Ruff will allow ^both of those, but it's the same idea of "allowing pragmatic forward references, but not arbitrary forward references")

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(If it does get added to a spec somewhere or we think there is a good reason to support arbitrary forward references in stubs, I'm personally very open to adding support for it at Ruff.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense in general; the second change in this PR (connect = Connection before Connection is defined) is definitely some very odd code.

I'm not sure why Ruff has an issue with the definition of def type_check_only, though. _F is used only in an annotation; why does the definition of type_check_only need to be after the definition of _F?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue in typing.pyi is that the TypeVar _F is bound to Callable before Callable is defined

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I guess that's the kind of case we only encounter in typeshed :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup! 😄

@JelleZijlstra JelleZijlstra merged commit 12b9e48 into python:main Apr 17, 2024
92 checks passed
@Avasam Avasam deleted the ruff-F821 branch April 17, 2024 17:10
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 this pull request may close these issues.

None yet

3 participants