-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
@no_type_check
support
#15122
@no_type_check
support
#15122
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! This overall seems like the way I would have gone about implementing this.
I think I understand the issue you point out with regard to cycles -- correct me if I'm wrong:
- This PR means that we need to evaluate the types for certain functions eagerly, in order to determine if they're decorated with
@no_type_check
, because we need to know if a function is decorated with@no_type_check
in order to determine whether we should be emitting diagnostics regarding that function - But we emit diagnostics during the process of type inference, so some types aren't actually ready yet at the point when we emit diagnostics
Examining the types of the decorators directly (without inferring the type of the function that's being decorated) makes sense to me... or potentially we could filter out the @no_type_check
-ignored diagnostics after the whole file has been inferred (rather than as part of context.report_lint()
), because at that point we know that all types are "ready"?
crates/red_knot_python_semantic/resources/mdtest/suppressions/no-type-check.md
Outdated
Show resolved
Hide resolved
crates/red_knot_python_semantic/resources/mdtest/suppressions/no-type-check.md
Outdated
Show resolved
Hide resolved
crates/red_knot_python_semantic/resources/mdtest/suppressions/no-type-check.md
Outdated
Show resolved
Hide resolved
This comment was marked as resolved.
This comment was marked as resolved.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great! Nice solution to the cycle issues.
crates/red_knot_python_semantic/resources/mdtest/suppressions/no-type-check.md
Outdated
Show resolved
Hide resolved
crates/red_knot_python_semantic/resources/mdtest/suppressions/no-type-check.md
Outdated
Show resolved
Hide resolved
crates/red_knot_python_semantic/resources/mdtest/suppressions/no-type-check.md
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!
crates/red_knot_python_semantic/resources/mdtest/suppressions/no-type-check.md
Outdated
Show resolved
Hide resolved
crates/red_knot_python_semantic/resources/mdtest/suppressions/no-type-check.md
Outdated
Show resolved
Hide resolved
crates/red_knot_python_semantic/resources/mdtest/suppressions/no-type-check.md
Outdated
Show resolved
Hide resolved
…no-type-check.md Co-authored-by: Carl Meyer <[email protected]>
Co-authored-by: Carl Meyer <[email protected]>
f880679
to
2c71d51
Compare
* main: Add all PEP-585 names to UP006 rule (#5454) [`flake8-simplify`] More precise inference for dictionaries (`SIM300`) (#15164) `@no_type_check` support (#15122) Visit PEP 764 inline `TypedDict`s' keys as non-type-expressions (#15073) [red-knot] Add diagnostic for invalid unpacking (#15086) [`flake8-use-pathlib`] Catch redundant joins in `PTH201` and avoid syntax errors (#15177) Update Rust crate glob to v0.3.2 (#15185) Update astral-sh/setup-uv action to v5 (#15193) Update dependency mdformat-mkdocs to v4.1.1 (#15192) Update Rust crate serde_with to v3.12.0 (#15191) Update NPM Development dependencies (#15190) Update pre-commit hook rhysd/actionlint to v1.7.5 (#15189) Update Rust crate syn to v2.0.93 (#15188) Update Rust crate serde to v1.0.217 (#15187) Update Rust crate quote to v1.0.38 (#15186) Update Rust crate compact_str to v0.8.1 (#15184) [`flake8-type-checking`] Disable TC006 & TC007 in stub files (#15179) Test explicit shadowing involving `def`s (#15174) Fix typo in `NameImport.qualified_name` docstring (#15170) [airflow]: extend names moved from core to provider (AIR303) (#15159)
Summary
This PR adds support for
@no_type_check
. Specifically, functions annotated with@no_type_check
.I think I solved it and the below no longer applies :)
The basics are working, but I'm running into cycles when the diagnostics are emitted from the annotated function's signature and I want to get a second opinion on what the best approach.
The current implementation infers the
FunctionType
and then checks if any decorator inFunctionLiteralType::decorators
is the knownNoTypeCheck
function. This works well, but cycles are more likely becauseFunctionLiteralType
infers the type of the return type and decorators, and any of those could create a cycle.An alternative is to do something closer to Pyright:
Instead of resolving the entire
FunctionLiteralType
, iterate over the function's decorators directly and infer each decorator expression. This reduces the possible cycles because it reduces the scope to the decorators only.Either approach requires a
recovery_fn
on the called query to returnType::Unknown
for the decorator's expression. So I think this PR mightbe blocked on fixpoint iteration.
Test Plan
Added tests, some of them panic :)