Skip to content

Comments

[red-knot] handle cycles in MRO/bases resolution#16693

Merged
carljm merged 2 commits intomainfrom
cjm/mrocycle
Mar 13, 2025
Merged

[red-knot] handle cycles in MRO/bases resolution#16693
carljm merged 2 commits intomainfrom
cjm/mrocycle

Conversation

@carljm
Copy link
Contributor

@carljm carljm commented Mar 13, 2025

There can be semi-cyclic inheritance patterns (e.g. recursive generics) that are not technically inheritance cycles, but that can cause us to hit Salsa query cycles in evaluating a type's MRO. Add fixed-point handling to these MRO-related queries so we don't panic on these cycles.

The details of what queries we hit in what order in this case will change as we implement support for generics, but ultimately we will probably need cycle handling for all queries that can re-enter type inference, otherwise we are susceptible to small changes in query execution order causing panics.

Fixes #14333
Further reduces the panicking set of seeds in astral-sh/ty#228

@carljm carljm added the ty Multi-file analysis & type inference label Mar 13, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Mar 13, 2025

mypy_primer results

No ecosystem changes detected ✅

@carljm
Copy link
Contributor Author

carljm commented Mar 13, 2025

Actually, after looking at this a bit more, I'm not yet convinced this is the right fix. Investigating a bit more.

@carljm carljm marked this pull request as draft March 13, 2025 01:22
@carljm carljm marked this pull request as ready for review March 13, 2025 02:38
@carljm
Copy link
Contributor Author

carljm commented Mar 13, 2025

Ok, I do think this is right, re-submitting for review.

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 don't mind all the added tracing calls but I'm curious how verbose it becomes. But we can always remove them in the future if needed. I can definetely see how they're useful and using KNOT_LOG can help to narrow down the scope of logs

@carljm
Copy link
Contributor Author

carljm commented Mar 13, 2025

The particular reason these tracing calls are important is that Salsa's behavior with queries that are methods is currently pretty unfriendly: the queries are all named inner_fn_name_ and are very difficult to distinguish from each other in Salsa traces. This is probably something that can be improved in Salsa, and then these traces in red-knot would be less necessary.

@carljm carljm merged commit abaa189 into main Mar 13, 2025
22 checks passed
@carljm carljm deleted the cjm/mrocycle branch March 13, 2025 15:16
dcreager added a commit that referenced this pull request Mar 14, 2025
* main: (53 commits)
  [syntax-errors] Tuple unpacking in `for` statement iterator clause before Python 3.9 (#16558)
  Ruff v0.10 Release (#16708)
  Add new `noqa` specification to the docs (#16703)
  describe requires-python fallback in docs (#16704)
  [red-knot] handle cycles in MRO/bases resolution (#16693)
  [red-knot] Auto generate statement nodes (#16645)
  [`pylint`] Better inference for `str.strip` (`PLE310`) (#16671)
  [`pylint`] Improve `repeated-equality-comparison` fix to use a `set` when all elements are hashable (`PLR1714`) (#16685)
  [`pylint`/`pep8-naming`] Check `__new__` argument name in `bad-staticmethod-argument` and not `invalid-first-argument-name-for-class-method` (`PLW0211`/`N804`) (#16676)
  [`flake8-pyi`] Stabilize fix for `unused-private-type-var` (`PYI018`) (#16682)
  [`flake8-bandit`] Deprecate `suspicious-xmle-tree-usage` (`S320`) (#16680)
  [`flake8-simplify`] Avoid double negation in fixes (`SIM103`) (#16684)
  [`pyupgrade`]: Improve diagnostic range for `redundant-open-mode` (`UP015`) (#16672)
  Consider all `TYPE_CHECKING` symbols for type-checking blocks (#16669)
  [`pep8-naming`]: Ignore methods decorated with `@typing.override` (`invalid-argument-name`) (#16667)
  Stabilize FURB169 preview behavior (#16666)
  [`pylint`] Detect invalid default value type for `os.environ.get` (`PLW1508`) (#16674)
  [`flake8-pytest-style`] Allow for loops with empty bodies (`PT012`, `PT031`) (#16678)
  [`pyupgrade`]: Deprecate `non-pep604-isinstance` (`UP038`) (#16681)
  [`flake8-type-checking`] Stabilize `runtime-cast-value` (`TC006`) (#16637)
  ...
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] Panics on self-referential generic class definition

2 participants