Implement isinstance() in Starlark interpreter#29474
Draft
Steffeeen wants to merge 1 commit into
Draft
Conversation
Previously, `isinstance()` was only implemented in a basic form to reserve the keyword for a future actual implementation. This commit now actually implements `isinstance()`. Type aliases don't work yet in `isinstance()`, as they are not implemented properly themselves.
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.
Description
This PR actually implements
isinstance()in Starlark. Until now,isinstance()was only reserved in the lexer and parser but could not actually be used.Motivation
Closes #27848.
Build API Changes
No
Checklist
Release Notes
RELNOTES: None
This should probably only be released together with all the other type system changes.
Open questions
These are questions that are still open from my side that I would like to get input on.
Should
isinstance()require enabling type checking?Currently, for this implementation work correctly it requires that the user either uses static or dynamic type checking as otherwise the
TypeTaggeris not run. TheTypeTaggeris responsible for resolving theExpressionin theIsInstanceExpressionto aStarlarkType(similar toCastExpressions). If theTypeTaggeris never run the code currently throws aNullPointerExceptioninEval#evalIsInstance()because it expects theIsInstanceExpressionto have aStarlarkType. This should probably be caught by theResolverbut it currently does not have access to the information of whether the user has enabled type checking. Another option would be to add an explicit check inevalIsInstance()and throw an error there. Alternatively, to avoid this problem entirely, we could just always run theTypeTagger. But I'm not sure if this may cause other issues.More complex type narrowing logic?
I only implemented a basic matching against a few (in my opinion) common patterns of using
isinstance()instead of a full evaluation of all possible combinations ofand,orandnottogether withisinstance(). I don't think the more advanced cases warrant the additional complexity.The code for this pattern matching is currently not really nice as it uses long conditional chains. The conditions cannot really be factored out because the
IsInstanceExpressionandIdentifierare both needed in the body of the if block. IntelliJ suggests using a switch statement instead of the if-else chain, however that makes the code even more unreadable in my opinion.Type aliases
As far as I could tell, type aliases are not yet resolved in the
TypeTaggerand as such they don't work inisinstance(). I added a unit test that tests type aliases inisinstance()but it is annotated with@Ignorefor now.