[ty] Support empty function bodies in if TYPE_CHECKING blocks#19372
[ty] Support empty function bodies in if TYPE_CHECKING blocks#19372carljm merged 12 commits intoastral-sh:mainfrom
if TYPE_CHECKING blocks#19372Conversation
|
| generator_functions: FxHashSet<FileScopeId>, | ||
|
|
||
| /// Set of all scopes that are inside `if TYPE_CHECKING` blocks. | ||
| function_scopes_in_type_checking: FxHashSet<FileScopeId>, |
There was a problem hiding this comment.
I think it's more memory efficient if we store this on Scope instead of having another hash map
There was a problem hiding this comment.
What do you mean by that, sorry
There was a problem hiding this comment.
ah, add an attribute to the Scope struct?
| let if_block_in_type_checking = | ||
| if let ast::Expr::Name(ast::ExprName { id, .. }) = &*node.test { | ||
| id == "TYPE_CHECKING" | ||
| } else { | ||
| false | ||
| }; | ||
|
|
||
| let else_elif_block_in_type_checking = | ||
| if let ast::Expr::UnaryOp(ast::ExprUnaryOp { op, operand, .. }) = &*node.test { | ||
| *op == ruff_python_ast::UnaryOp::Not | ||
| && if let ast::Expr::Name(ast::ExprName { id, .. }) = &**operand { | ||
| id == "TYPE_CHECKING" | ||
| } else { | ||
| false | ||
| } | ||
| } else { | ||
| false | ||
| }; |
There was a problem hiding this comment.
I'm aware this pollutes this arm a bit, should I move this to a different function outwith the SemanticIndexBuilder struct?
if TYPE_CHECKINGif TYPE_CHECKING blocks
|
| Lint rule | Added | Removed | Changed |
|---|---|---|---|
invalid-return-type |
0 | 208 | 0 |
unused-ignore-comment |
18 | 0 | 0 |
| Total | 18 | 208 | 0 |
| if self.scope().scope(self.db()).in_type_checking_block() { | ||
| return; | ||
| } |
There was a problem hiding this comment.
It's unfortunate that we need to pre-compute this information in semantic index builder considering that empty function bodies in type checking blocks are rare. Ideally, we'd compute it lazily whether the function is inside a TYPE_CHECKING block but I don't see how we can do this easily because our AST doesn't allow upward traversal.
Co-authored-by: Carl Meyer <carl@oddbird.net>
|
Ah sorry let me revert that |
|
It's all taken care of and merged already, thanks! |
|
Ah, didn't see that on my phone, thanks! |
Summary
Resolves astral-sh/ty#339
Supports having a blank function body inside
if TYPE_CHECKINGblock or in the elif or else of aif not TYPE_CHECKINGblock.Test Plan
Update
function/return_type.md