Skip to content

[ty] Improve spans of references to submodules imported in an __init__.py#21795

Merged
charliermarsh merged 2 commits intomainfrom
gankra/fix-span
Feb 11, 2026
Merged

[ty] Improve spans of references to submodules imported in an __init__.py#21795
charliermarsh merged 2 commits intomainfrom
gankra/fix-span

Conversation

@Gankra
Copy link
Contributor

@Gankra Gankra commented Dec 4, 2025

Good ol' DefinitionKind::ImportFromSubmodule

@Gankra Gankra requested a review from carljm as a code owner December 4, 2025 17:05
@Gankra Gankra added the server Related to the LSP server label Dec 4, 2025
@Gankra Gankra added the ty Multi-file analysis & type inference label Dec 4, 2025
@Gankra Gankra changed the title [ty] Fix spans of references to submodules imported in an __init__.py [ty] Improve spans of references to submodules imported in an __init__.py Dec 4, 2025
@astral-sh-bot
Copy link

astral-sh-bot bot commented Dec 4, 2025

Typing conformance results

No changes detected ✅

@astral-sh-bot
Copy link

astral-sh-bot bot commented Dec 4, 2025

mypy_primer results

Changes were detected when running on open source projects
porcupine (https://github.com/Akuli/porcupine)
- porcupine/pluginmanager.py:133:49: error[invalid-argument-type] Argument to function `__new__` is incorrect: Expected `Iterable[Never]`, found `Unknown | str`
- Found 25 diagnostics
+ Found 24 diagnostics

Expression (https://github.com/cognitedata/Expression)
+ tests/test_compose.py:21:16: error[invalid-assignment] Object of type `(Never, /) -> Never` is not assignable to `(int, /) -> int`
- Found 204 diagnostics
+ Found 205 diagnostics

scikit-build-core (https://github.com/scikit-build/scikit-build-core)
- src/scikit_build_core/build/wheel.py:99:20: error[no-matching-overload] No overload of bound method `__init__` matches arguments
- Found 50 diagnostics
+ Found 49 diagnostics

prefect (https://github.com/PrefectHQ/prefect)
- src/prefect/input/run_input.py:672:20: error[invalid-return-type] Return type does not match returned value: expected `T@GetAutomaticInputHandler | AutomaticRunInput[T@GetAutomaticInputHandler]`, found `T@GetAutomaticInputHandler | AutomaticRunInput[T@GetAutomaticInputHandler] | Coroutine[Any, Any, T@GetAutomaticInputHandler | AutomaticRunInput[T@GetAutomaticInputHandler]]`
+ src/prefect/input/run_input.py:672:20: error[invalid-return-type] Return type does not match returned value: expected `T@GetAutomaticInputHandler | AutomaticRunInput[T@GetAutomaticInputHandler]`, found `Unknown | Coroutine[Any, Any, Unknown]`

materialize (https://github.com/MaterializeInc/materialize)
- misc/python/materialize/cli/mz_workload_anonymize.py:251:13: error[no-matching-overload] No overload of bound method `join` matches arguments
- Found 535 diagnostics
+ Found 534 diagnostics

Comment on lines 1064 to 1071
let base_addr = module_str.as_ptr().addr();
let component_addr = component_str.as_ptr().addr();
let offset = base_addr.saturating_sub(component_addr);
let Ok(offset_size) = TextSize::try_from(offset) else {
// This shouldn't happen but just in case, provide a safe default
return module_ident.range();
};
let start = module_ident.start().saturating_add(offset_size);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is probably Parser Crimes and there's probably a better API for doing this but this is my absolute favourite trick and I'll use it any time I can

Copy link
Member

Choose a reason for hiding this comment

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

Took me a while to understand what's happening here, especially the pointer stuff. This could be rewritten to:

        let module_ident = self.module(module);
        let module_str = module_ident.as_str();

        let Some((end_offset, _)) = module_str.match_indices('.').nth(self.module_index) else {
            // This shouldn't happen but just in case, provide a safe default
            return module_ident.range();
        };

        let (component_str, _) = module_str.split_at(end_offset);

        let Ok(end_offset) = TextSize::try_from(end_offset) else {
            // This shouldn't happen but just in case, provide a safe default
            return module_ident.range();
        };
        let start_offset = end_offset - component_str.text_len();
        TextRange::new(start_offset, end_offset) + module_ident.start()

Or we should add a short comment explaining what we're doing here.

// This shouldn't happen but just in case, provide a safe default
return module_ident.range();
};
let start = module_ident.start().saturating_add(offset_size);
Copy link
Member

Choose a reason for hiding this comment

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

I don't think the saturating_add gives you much here, as we are as likely to panic somewhere in the text range conversion if the new range ends up being between two char boundaries. In which case I'd prefer if it panicked in the add

@AlexWaygood AlexWaygood removed their request for review December 5, 2025 18:03
@charliermarsh charliermarsh self-assigned this Feb 11, 2026
@astral-sh-bot
Copy link

astral-sh-bot bot commented Feb 11, 2026

Memory usage report

Memory usage unchanged ✅

@charliermarsh charliermarsh merged commit 144b35f into main Feb 11, 2026
49 checks passed
@charliermarsh charliermarsh deleted the gankra/fix-span branch February 11, 2026 17:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

server Related to the LSP server ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants