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

[pyflakes] Avoid false positives in @no_type_check contexts (F821, F722) #14615

Merged
merged 13 commits into from
Nov 26, 2024

Conversation

ntBre
Copy link
Contributor

@ntBre ntBre commented Nov 26, 2024

Summary

These changes avoid the false positives reported in #13824, where both F821 (undefined name) and F722 (syntax error in forward annotation) were triggered in function signatures decorated with typing.no_type_check. In line with the discussion on the issue, the code now skips visiting type definitions when in a context where this decorator was found. While the initial report only covered the function decorator, the docs indicate that classes can also be decorated, so this context is also checked when visiting class definitions, and the new tests reflect that.

Test Plan

These changes were tested by including the code snippets triggering the issue as new snapshot tests. As mentioned above, these snapshots are also augmented with class variants. I also added invalid annotations to the function and method bodies to make sure the no_type_check context applied to the bodies too.

if decorator
.expression
.as_name_expr()
.is_some_and(|name| name.id.as_str() == "no_type_check")
Copy link
Member

Choose a reason for hiding this comment

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

I think this should use match_typing_expr so that it works for @typing.no_type_check too.

Copy link
Member

Choose a reason for hiding this comment

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

So, will this apply to type annotations in the body of the function? And should it, per the spec? (I don't know off-hand.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah thanks, I missed match_typing_expr.

Yes, this should apply to annotations in the body too, based on my reading of the no_type_check docs:

This works as a class or function decorator. With a class, it applies recursively to all methods and classes defined in that class (but not to methods defined in its superclasses or subclasses). Type checkers will ignore all annotations in a function or class with this decorator.

Similarly in the typing docs linked in what Micha linked:

If a type checker supports the no_type_check decorator for functions, it should suppress all type errors for the def statement and its body including any nested functions or classes. It should also ignore all parameter and return type annotations and treat the function as if it were unannotated.

I added annotated variables to the test fixtures in my last commit to check this.

Copy link
Contributor

github-actions bot commented Nov 26, 2024

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Copy link
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

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

I looked into the @no_type_check for red knot and I think we should ignore it for classes, the same as pyright. See https://discuss.python.org/t/no-type-check-decorator/43119.

Edit: The relevant typing spec change https://github.com/python/typing/pull/1615/files

@MichaReiser MichaReiser added the rule Implementing or modifying a lint rule label Nov 26, 2024
@ntBre
Copy link
Contributor Author

ntBre commented Nov 26, 2024

Ah okay, happy to revert the class part. That's what I had initially anyway since that's all that was reported in #13824.

Copy link
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

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

Nice. This looks good to me

@MichaReiser
Copy link
Member

Thanks

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
Development

Successfully merging this pull request may close these issues.

3 participants