[ty] Improve spans of references to submodules imported in an __init__.py#21795
Merged
charliermarsh merged 2 commits intomainfrom Feb 11, 2026
Merged
[ty] Improve spans of references to submodules imported in an __init__.py#21795charliermarsh merged 2 commits intomainfrom
__init__.py#21795charliermarsh merged 2 commits intomainfrom
Conversation
__init__.py__init__.py
Typing conformance resultsNo changes detected ✅ |
|
Gankra
commented
Dec 4, 2025
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); |
Contributor
Author
There was a problem hiding this comment.
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
Member
There was a problem hiding this comment.
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.
MichaReiser
reviewed
Dec 4, 2025
| // 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); |
Member
There was a problem hiding this comment.
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
MichaReiser
approved these changes
Dec 4, 2025
a374408 to
77bfe2c
Compare
Memory usage reportMemory usage unchanged ✅ |
fdd62b7 to
65c7efc
Compare
65c7efc to
5a6ef6c
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Good ol'
DefinitionKind::ImportFromSubmoduleDefinitionKind::ImportFromSubmodulety#1759