[ty] Avoid accessing class literal with incorrect AST by ibraheemdev · Pull Request #18670 · astral-sh/ruff (original) (raw)
This looks good to me; thanks for the fix! There is only one possible ParsedModule that is correct to use here, and we already have the information to know which it is, so there's no reason to leave open the possibility of a caller passing in the wrong one. Especially given that this results in a hard-to-understand crash, and the bug clearly can go undetected until we have a very particular cross-module scenario, so it may not be caught by tests.
It's true that this means calling header_range or header_span on a class implicitly means adopting a direct dependency on the AST of the module in which the class is defined, but that's the unavoidable nature of asking for a range/span, and it seems clear enough in the method names.
I don't mind waiting to land this until @MichaReiser has a chance to review it, though.