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

[red_knot] Crash on analysing stub file with explicitly derived class #13160

Closed
LordAro opened this issue Aug 30, 2024 · 2 comments · Fixed by #13204
Closed

[red_knot] Crash on analysing stub file with explicitly derived class #13160

LordAro opened this issue Aug 30, 2024 · 2 comments · Fixed by #13204
Labels
red-knot Multi-file analysis & type inference

Comments

@LordAro
Copy link

LordAro commented Aug 30, 2024

Found just while playing around with red_knot ( b4d9d26 )

test.py

class Foo(object):
    pass

works fine

Renaming it to test.pyi results in:

 $ ./../target/debug/red_knot && echo $?
0
 $ mv test.py test.pyi
 $ ./../target/debug/red_knot && echo $?
thread '<unnamed>' panicked at crates/red_knot_python_semantic/src/types/infer.rs:177:25:
no entry found for key
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Rayon: detected unexpected panic; aborting
Aborted (core dumped)

Seems to crash with anything as the parent.

relevant part of RUST_BACKTRACE=full:

  19:     0x561605928fe5 - core::option::Option<T>::expect::hffc771cc74f800e9
                               at /build/rustc-kAv1jW/rustc-1.75.0+dfsg0ubuntu1~bpo0/library/core/src/option.rs:894:21
  20:     0x561605928fe5 - <std::collections::hash::map::HashMap<K,V,S> as core::ops::index::Index<&Q>>::index::hf84fbd63e8ba27dd
                               at /build/rustc-kAv1jW/rustc-1.75.0+dfsg0ubuntu1~bpo0/library/std/src/collections/hash/map.rs:1341:23
  21:     0x56160595a8e4 - red_knot_python_semantic::types::infer::TypeInference::expression_ty::h45e30f4ed3623f78
                               at /home/cpigott/dev/ruff/crates/red_knot_python_semantic/src/types/infer.rs:177:25
  22:     0x56160579d5e4 - <ruff_python_ast::expression::ExpressionRef as red_knot_python_semantic::semantic_model::HasTy>::ty::h7574fdc81220df66
                               at /home/cpigott/dev/ruff/crates/red_knot_python_semantic/src/semantic_model.rs:62:9
  23:     0x5616056603b5 - <ruff_python_ast::nodes::ExprName as red_knot_python_semantic::semantic_model::HasTy>::ty::h0bf9800421b11df7
                               at /home/cpigott/dev/ruff/crates/red_knot_python_semantic/src/semantic_model.rs:72:17
  24:     0x561605600139 - red_knot_workspace::lint::lint_maybe_undefined::hbf56527a89120b0f
                               at /home/cpigott/dev/ruff/crates/red_knot_workspace/src/lint.rs:117:11
  25:     0x561605600d9e - <red_knot_workspace::lint::SemanticVisitor as ruff_python_ast::visitor::Visitor>::visit_expr::h4e5d2046d4543b66
                               at /home/cpigott/dev/ruff/crates/red_knot_workspace/src/lint.rs:244:17
  26:     0x56160561fbfd - ruff_python_ast::visitor::walk_arguments::hcf8638d59de7f088
                               at /home/cpigott/dev/ruff/crates/ruff_python_ast/src/visitor.rs:597:9
  27:     0x5616055f9674 - ruff_python_ast::visitor::Visitor::visit_arguments::he4e1af453a0b6b0f
                               at /home/cpigott/dev/ruff/crates/ruff_python_ast/src/visitor.rs:58:9
  28:     0x5616056238bc - ruff_python_ast::visitor::walk_stmt::h8c2b40d349b01a24
                               at /home/cpigott/dev/ruff/crates/ruff_python_ast/src/visitor.rs:165:17
  29:     0x561605600d2a - <red_knot_workspace::lint::SemanticVisitor as ruff_python_ast::visitor::Visitor>::visit_stmt::h2cea15d01c9fd284
                               at /home/cpigott/dev/ruff/crates/red_knot_workspace/src/lint.rs:238:9
  30:     0x561605620f36 - ruff_python_ast::visitor::walk_body::hd1184b90f5a26118
                               at /home/cpigott/dev/ruff/crates/ruff_python_ast/src/visitor.rs:115:9
  31:     0x5616055f93f8 - ruff_python_ast::visitor::Visitor::visit_body::h1b0a1fbe97acb4d0
                               at /home/cpigott/dev/ruff/crates/ruff_python_ast/src/visitor.rs:94:9
  32:     0x561605602c32 - <red_knot_workspace::lint::lint_semantic::Configuration_ as salsa::function::Configuration>::execute::inner_::h18d04db1014d8c80
                               at /home/cpigott/dev/ruff/crates/red_knot_workspace/src/lint.rs:93:5

Looking forward to seeing where this goes!

@AlexWaygood AlexWaygood added the red-knot Multi-file analysis & type inference label Aug 30, 2024
@carljm
Copy link
Contributor

carljm commented Aug 31, 2024

Thanks for the report!

This looks related to #13176, but I'd expect that issue to cause a resolution to Type::Unbound, not a panic. Will take a look.

@carljm
Copy link
Contributor

carljm commented Aug 31, 2024

Found the bug, fix PR in progress.

carljm added a commit that referenced this issue Sep 3, 2024
Test coverage for #13131 wasn't as good as I thought it was, because
although we infer a lot of types in stubs in typeshed, we don't check
typeshed, and therefore we don't do scope-level inference and pull all
types for a scope. So we didn't really have good test coverage for
scope-level inference in a stub. And because of this, I got the code for
supporting that wrong, meaning that if we did scope-level inference with
deferred types, we'd end up never populating the deferred types in the
scope's `TypeInference`, which causes panics like #13160.

Here I both add test coverage by running the corpus tests both as `.py`
and as `.pyi` (which reveals the panic), and I fix the code to support
deferred types in scope inference.

This also revealed a problem with deferred types in generic functions,
which effectively span two scopes. That problem will require a bit more
thought, and I don't want to block this PR on it, so for now I just
don't defer annotations on generic functions.

Fixes #13160.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
red-knot Multi-file analysis & type inference
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants