New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make ImportFrom level just a u32 #11170
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excellent, thank you.
(This has bothered me for a while.) |
Oh, looks like I need to learn to run doc-tests locally, too. |
By the way, have you seen |
Oh yeah, Now I just have to decide if I like that enough better to do the work to switch... |
Going to leave this unmerged for a bit while I think about that :) |
Nah I think I'll just merge this. |
|
Agreed, this looks good to me. But |
I definitely prefer this over Option. The thing that bothered me most is that the AST allows for both level and module to be None which should never happen in practice. The new design doesn't prevent this from happening, but at least it's no longer required to handle the case explicitly (you can test if module is set and otherwise take the default case) |
This could be useful for error recovery to represent |
We can represent this as |
Summary
Currently we represent the
level
of anImportFrom
(the leading dots) as anOption<u32>
, but the parser always parses a non-relative import (from foo import bar
) as havinglevel: Some(0)
. But various other representations of imports throughout the codebase also useOption<u32>
, and inconsistently sometimes useSome(0)
and sometimes useNone
to represent the same situation: an absolute import. And then there's tons of code checking for absolute vs relative import that jumps through various hoops to handleNone
andSome(0)
the same way. And of course it's easy to get this wrong -- in particular, it would be easy to check forNone
and miss theSome(0)
case.There is no useful distinction between what
None
andSome(0)
represent here; they are exactly the same import. It's just two different inconsistent representations for the same thing.This PR changes our representation of
level
everywhere to be justu32
, so an absolute import always has level0
, and simplifying a bunch of code that checkslevel
. This is also consistent with CPython AST, which useslevel=0
, notlevel=None
.It would be possible to go the other way and try to eliminate the use of
Some(0)
and always useNone
, but it will be harder to stay consistent with that, sinceSome(0)
will always be possible. And since it's possible, every usage site should technically still check for it.Option<u32>
is twice as big asu32
, so this change saves a bit of memory, too. And some CPU instructions, from all the usage sites doing various forms of double checks forNone
and0
.Test Plan
Compiles and test suite passes. Updated some
insta
tests.