Skip to content

Conversation

@sharkdp
Copy link
Contributor

@sharkdp sharkdp commented May 2, 2025

Summary

closes #17775

Test Plan

Added corpus regression test

@sharkdp sharkdp added the ty Multi-file analysis & type inference label May 2, 2025
@MichaReiser
Copy link
Member

MichaReiser commented May 2, 2025

Nice! I've been waiting for this panic to "go away" :)

@sharkdp
Copy link
Contributor Author

sharkdp commented May 2, 2025

So this is a bit unfortunate… the mypy_primer run fails because I'm adding projects that are still panicking in the old state on main. Ideally, we'd only fail that run if we're introducing new panics.

I can run mypy_primer locally with the new projects --project-selector '/alerta|aiohttp|meson|sphinx|trio$'

@sharkdp sharkdp force-pushed the david/fix-17775 branch from 9dcf314 to 9d8ed64 Compare May 2, 2025 07:43
@sharkdp sharkdp marked this pull request as ready for review May 2, 2025 07:44
@sharkdp sharkdp force-pushed the david/fix-17775 branch from 9d8ed64 to f3bdf34 Compare May 2, 2025 09:56
@sharkdp
Copy link
Contributor Author

sharkdp commented May 2, 2025

Going to merge this, as it seems relatively uncontroversial — the whole fix happens in a function that will eventually go away.

@sharkdp sharkdp merged commit 6d2c10c into main May 2, 2025
34 of 35 checks passed
@sharkdp sharkdp deleted the david/fix-17775 branch May 2, 2025 10:11
Comment on lines +7627 to +7633
let value_ty = if builder.deferred_state.in_string_annotation() {
// Using `.expression_type` does not work in string annotations, because
// we do not store types for sub-expressions. Re-infer the type here.
builder.infer_expression(value)
} else {
builder.expression_type(value)
};
Copy link
Member

Choose a reason for hiding this comment

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

oof, that seems a bit unfortunate. Does that mean we should just get rid of the expression_type() API altogether? Or have expression_type() itself check whether we're in a deferred string annotation (and fallback to infer_expression() if so)?

It feels like a bit of a footgun if we have to always make sure we're not in a deferred string annotation before calling expression_type()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good questions. It's certainly possible that we have other hidden problems with calling expression_type for something that might be inside a string annotation. But note that it's not common to re-enter a sub-AST and match on inner elements (and then use expression_type) after the type has already been inferred. Usually, in functions like infer_expression, we return types "back up" to the caller, i.e. to outer elements. Admittedly, I did not think too much about this exact callsite because this whole function is basically a large "To do" and will disappear eventually.

dcreager added a commit that referenced this pull request May 2, 2025
* main:
  [red-knot] Refactor: no mutability in call APIs (#17788)
  [red-knot] Fix panic for `tuple[x[y]]` string annotation (#17787)
  [red-knot] Implicit instance attributes in generic methods (#17769)
  doc: Add link to `check-typed-exception` from `S110` and `S112` (#17786)
  Fix module name in ASYNC110, 115, and 116 fixes (#17774)
  [red-knot] More informative hover-types for assignments (#17762)
  [syntax-errors] Use consistent message for bad starred expression usage. (#17772)
  red_knot_server: add auto-completion MVP
  Allow passing a virtual environment to `ruff analyze graph` (#17743)
  Bump 0.11.8 (#17766)
  [`flake8-use-pathlib`] Fix `PTH104`false positive when `rename` is passed a file descriptor (#17712)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[red-knot] Panic: Could not find missing expression ID

4 participants