-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Type safe option value getter #14561
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
base: master
Are you sure you want to change the base?
Type safe option value getter #14561
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.
There is a typo in the fourth commit message: envrionment
.
I don't see the value of using separate commits for each file. Personally, I would rather squash all of them.
Yeah, I didn't plan to merge them that way, just have smaller chunks to review |
85f0d3b
to
6431122
Compare
6431122
to
245c47f
Compare
245c47f
to
c62617e
Compare
@@ -889,7 +890,19 @@ def get_value_object_and_value_for(self, key: OptionKey) -> T.Tuple[AnyOptionTyp | |||
computed_value = vobject.validate_value(self.augments[key]) | |||
return (vobject, computed_value) | |||
|
|||
def get_value_for(self, name: 'T.Union[OptionKey, str]', subproject: T.Optional[str] = None) -> ElementaryOptionValues: | |||
def get_value_for(self, key: OptionKey, type_: T.Type[ElementaryOptionTypes], | |||
*, fallback: T.Optional[ElementaryOptionTypes] = None) -> ElementaryOptionTypes: |
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.
Maybe default
would be more in line to how the same concept is named in the Pyrthon APIs than fallback
This provides a variant of `get_value_for` that does run-time type checking, but in a way that allows static type checkers like mypy to understand. This gives us the two fold advantage of runtime checking of types that can't be statically known and not having to write a lot `assert isinstance(...), 'for mypy'` checks.
It belongs here, rather than in the CoreData.
Be more consistent
We have a fallback parameter now, so we can avoid the exception handling by providing a sane or sentinel default value instead
The `target` parameter is actually `None | BuildTarget`, and we rely on this.
It's really a core part of Meson, not something specific to the Interpreter, and this can be seen by where it is imported.
Because that is what they actually should have
By using overloads we create two valid signatures: (BuildTarget, Environment) (None, Envionment, str) This means that mypy will *force* a user to either pass a valid target or a subproject, but not both. This in turn means we can rely on only having one subproject definition. The drawback to having clean public interfaces, is that we have to convince mypy we're doing the right thing inside the Compiler class (or add a bunch of extra code that isn't actually necessary).
This pattern repeats a lot in the Compiler, and the compiler even has it's own implementation of this (which will be removed shortly).
This was an unsafe version of the other function, but without proper subproject handling. In most cases (except unittests) the safe variant was actually better anyway, especially with the fallback parameter
c62617e
to
91db35c
Compare
This (ultimately) makes the
OptionStore.get_option_value()
type safe, but using a generic with a bound type. This allows us to both have runtime type checking, and allows mypy (and other type checkers) to see that at static inspection time and treat the return as safe. This, in turn, allows us to remove a lot ofassert isinstance(...), 'for mypy'
checks, with a properMesonBugException
with a more helpful error message.The addition of a fallback parameter also allows us to remove a bunch of
if key in options and options.get_value_for(key)
style checks, through the use of the fallback.This series has been split into a bunch of patches to attempt to make review easier, but I'm perfectly happy to squash any number of them into less patches before merging.