Python: Add metrics query for type annotations#18448
Merged
Conversation
Adds a query that counts the number of type annotations of various kinds. Intended to be used with something like MRVA to inform our modelling decisions. Currently the query counts the following "interesting" types in addition to the total number of types: - Built-in types (which are less likely to be interesting from a modelling perspective) - Forward declarations (i.e. annotations inside strings) which will require a fair bit of QL machinery to interpret. - Simple types (stuff like `foo` or `foo.bar.baz`) - Optional types (stuff like `Optional[foo]` which from a modelling perspective should likely be treated the same as `foo`) - Complex types (anything that contains more complex type constructions such as instantiations of generic types)
Contributor
There was a problem hiding this comment.
Copilot wasn't able to review any files in this pull request.
Files not reviewed (1)
- python/ql/src/Metrics/Internal/TypeAnnotations.ql: Language not supported
Tip: Copilot code review supports C#, Go, Java, JavaScript, Markdown, Python, Ruby and TypeScript, with more languages coming soon. Learn more
RasmusWL
approved these changes
Jan 17, 2025
Member
RasmusWL
left a comment
There was a problem hiding this comment.
Looks fine to me 👍
I ran it on flask, and one thing i immediately did afterwards was to look into the complex type annotations... but it required that I added a any(Annotatable a).getAnnotation() = e. We could consider having those in by default 🤷
Predicates are not following QL style guide with camcelCase, so it seems like you have been too deep in Python land 😅
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.
Adds a query that counts the number of type annotations of various kinds. Intended to be used with something like MRVA to inform our modelling decisions.
Currently the query counts the following "interesting" types in addition to the total number of types:
fooorfoo.bar.baz)Optional[foo]which from a modelling perspective should likely be treated the same asfoo)Pull Request checklist
All query authors
.qhelp. See the documentation in this repository.Internal query authors only
.ql,.qll, or.qhelpfiles. See the documentation (internal access required).