[DRAFT] Remove query_builder from mypy typing-issues list#119028
Draft
cursor[bot] wants to merge 3 commits into
Draft
[DRAFT] Remove query_builder from mypy typing-issues list#119028cursor[bot] wants to merge 3 commits into
cursor[bot] wants to merge 3 commits into
Conversation
Co-authored-by: Armen Zambrano G. <armenzg@users.noreply.github.com>
Co-authored-by: Armen Zambrano G. <armenzg@users.noreply.github.com>
Co-authored-by: Armen Zambrano G. <armenzg@users.noreply.github.com>
Comment on lines
+130
to
+134
| operation, metric_name = parse_expression(field) | ||
| if operation is not None and operation not in OPERATIONS: | ||
| raise InvalidParams(f"Invalid operation '{operation}'. Must be one of {', '.join(OPERATIONS)}") | ||
|
|
||
| if matches is not None: | ||
| operation = matches[1] | ||
| metric_name = matches[2] | ||
| else: | ||
| operation = None | ||
| metric_name = field | ||
| return MetricField(cast(MetricOperationType | None, operation), get_mri(metric_name)) |
Contributor
There was a problem hiding this comment.
parse_public_field uses MRI regex instead of public-name regex, breaking all public metric field parsing
Replacing PUBLIC_EXPRESSION_REGEX.match(field) with parse_expression(field) is incorrect: parse_expression uses MRI_EXPRESSION_REGEX which requires MRI format (entity:namespace/name@unit), so public fields like sum(session.duration) never match, causing operation=None and metric_name="sum(session.duration)", which then fails inside get_mri() with InvalidParams("Failed to parse 'sum(session.duration)'") for every API call with a public metric field.
Evidence
parse_expressioninnaming_layer/mapping.py:116usesMRI_EXPRESSION_REGEX, whose pattern is^{OP_REGEX}\(({MRI_SCHEMA_REGEX_STRING})\)-parse_expressioninnaming_layer/mapping.py:116usesMRI_EXPRESSION_REGEX, whose pattern is whereMRI_SCHEMA_REGEX_STRING=entity:namespace/name@unit` — requires colons and at-sign.- A public field like
sum(session.duration)does not matchMRI_EXPRESSION_REGEX;parse_expressionreturns(None, "sum(session.duration)"). operationisNone, so theInvalidParamsguard on line 131 is skipped;get_mri("sum(session.duration)")raisesInvalidParams("Failed to parse 'sum(session.duration)'…")viaKeyErrorinNAME_TO_MRIlookup.- The old code used
PUBLIC_EXPRESSION_REGEX(^{OP_REGEX}\({PUBLIC_NAME_REGEX}\)-parse_expressioninnaming_layer/mapping.py:116usesMRI_EXPRESSION_REGEX, whose pattern is^{OP_REGEX}(({MRI_SCHEMA_REGEX_STRING}))-parse_expressioninnaming_layer/mapping.py:116usesMRI_EXPRESSION_REGEX, whose pattern is whereMRI_SCHEMA_REGEX_STRING=entity:namespace/name@unit— requires colons and at-sign. - A public field like
sum(session.duration)does not matchMRI_EXPRESSION_REGEX;parse_expressionreturns(None, "sum(session.duration)"). operationisNone, so theInvalidParamsguard on line 131 is skipped;get_mri("sum(session.duration)")raisesInvalidParams("Failed to parse 'sum(session.duration)'…")viaKeyErrorinNAME_TO_MRIlookup.- The old code used
PUBLIC_EXPRESSION_REGEX() which correctly matchessession.durationas a plain dotted public name, extractingoperation="sum"andmetric_name="session.duration". parse_fieldis called directly from user API query params (query_params.getlist("field", [])) at line 530, so every metrics API request using a public field with an operation hits this path.
Identified by Warden sentry-backend-bugs · 788-XYR
Contributor
Backend Test FailuresFailures on
|
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.
Summary
sentry.snuba.metrics.query_builderfrom thetool.mypy.overrides"sentry modules with typing issues" list inpyproject.tomlsrc/sentry/snuba/metrics/query_builder.py(type narrowing, optional handling, and typed parameter casting)Testing
.venv/bin/python -m mypy src/sentry/snuba/metrics/query_builder.py.venv/bin/prek run -qmake test-selective(fails in this environment:gcloudunavailable)SENTRY_SKIP_SERVICE_VALIDATION=1 .venv/bin/pytest -n3 -svv --reuse-db tests/sentry/snuba/metrics/test_query_builder.py(fails in this environment due unavailable Redis/Postgres services)Reviewers
Legal Boilerplate
Look, I get it. The entity doing business as "Sentry" was incorporated in the State of Delaware in 2015 as Functional Software, Inc. and is gonna need some rights from me in order to utilize my contributions in this here PR. So here's the deal: I retain all rights, title and interest in and to my contributions, and by keeping this boilerplate intact I confirm that Sentry can use, modify, copy, and redistribute my contributions, under Sentry's choice of terms.