From d3a54f796a276a4ce2843a032f2e548df636a807 Mon Sep 17 00:00:00 2001 From: "sentry-junior[bot]" <264270552+sentry-junior[bot]@users.noreply.github.com> Date: Mon, 16 Nov 2026 10:38:54 +0000 Subject: [PATCH 1/3] test: Fix tests that could not fail Remove try/except blocks that swallowed assertion failures, assert on the collected results instead of inside loops that may not run, skip instead of silently passing on unsupported versions, and drop duplicate parametrize cases. Found with falsegreen. Co-Authored-By: Ivana Kellyer <131587164+sentrivana@users.noreply.github.com> --- tests/integrations/beam/test_beam.py | 16 +++++--------- tests/integrations/grpc/test_grpc.py | 3 +-- tests/integrations/grpc/test_grpc_aio.py | 3 +-- tests/integrations/mcp/test_mcp.py | 22 +++++++++---------- .../pydantic_ai/test_pydantic_ai.py | 11 ++-------- tests/test_client.py | 16 ++------------ tests/test_envelope.py | 5 ++--- tests/test_utils.py | 1 - 8 files changed, 25 insertions(+), 52 deletions(-) diff --git a/tests/integrations/beam/test_beam.py b/tests/integrations/beam/test_beam.py index e80a67a107..ae93d0a672 100644 --- a/tests/integrations/beam/test_beam.py +++ b/tests/integrations/beam/test_beam.py @@ -140,17 +140,13 @@ def test_monkey_patch_signature(f, args, kwargs): except Exception: print("Failed on {} with parameters {}, {}".format(f_temp, args, kwargs)) raise - try: - expected_signature = inspect.signature(f) - test_signature = inspect.signature(f_temp) - assert expected_signature == test_signature, ( - "Failed on {}, signature {} does not match {}".format( - f, expected_signature, test_signature - ) + expected_signature = inspect.signature(f) + test_signature = inspect.signature(f_temp) + assert expected_signature == test_signature, ( + "Failed on {}, signature {} does not match {}".format( + f, expected_signature, test_signature ) - except Exception: - # expected to pass for py2.7 - pass + ) class _OutputHandler(OutputHandler): diff --git a/tests/integrations/grpc/test_grpc.py b/tests/integrations/grpc/test_grpc.py index f50a2f62d1..a878f1c6cf 100644 --- a/tests/integrations/grpc/test_grpc.py +++ b/tests/integrations/grpc/test_grpc.py @@ -666,8 +666,7 @@ def test_stream_stream(sentry_init): # Use the provided channel stub = gRPCTestServiceStub(channel) response_iterator = stub.TestStreamStream(iter((gRPCTestMessage(text="test"),))) - for response in response_iterator: - assert response.text == "test" + assert [response.text for response in response_iterator] == ["test"] _tear_down(server=server) diff --git a/tests/integrations/grpc/test_grpc_aio.py b/tests/integrations/grpc/test_grpc_aio.py index 217dc110d2..190812f56b 100644 --- a/tests/integrations/grpc/test_grpc_aio.py +++ b/tests/integrations/grpc/test_grpc_aio.py @@ -457,8 +457,7 @@ async def test_stream_stream(grpc_server_and_channel): # Use the provided channel stub = gRPCTestServiceStub(channel) response = stub.TestStreamStream((gRPCTestMessage(text="test"),)) - async for r in response: - assert r.text == "test" + assert [r.text async for r in response] == ["test"] @pytest.mark.asyncio diff --git a/tests/integrations/mcp/test_mcp.py b/tests/integrations/mcp/test_mcp.py index 15432d8d84..ef6c04bbdb 100644 --- a/tests/integrations/mcp/test_mcp.py +++ b/tests/integrations/mcp/test_mcp.py @@ -142,21 +142,21 @@ def __init__(self, text): self.text = text +@pytest.mark.skipif(IS_MCP_V2, reason="Server decorator patching is MCP v1 only") def test_integration_patches_server(sentry_init): """Test that MCPIntegration patches the Server class""" - if not IS_MCP_V2: - original_call_tool = Server.call_tool - original_get_prompt = Server.get_prompt - original_read_resource = Server.read_resource + original_call_tool = Server.call_tool + original_get_prompt = Server.get_prompt + original_read_resource = Server.read_resource - sentry_init( - integrations=[MCPIntegration()], - traces_sample_rate=1.0, - ) + sentry_init( + integrations=[MCPIntegration()], + traces_sample_rate=1.0, + ) - assert Server.call_tool is not original_call_tool - assert Server.get_prompt is not original_get_prompt - assert Server.read_resource is not original_read_resource + assert Server.call_tool is not original_call_tool + assert Server.get_prompt is not original_get_prompt + assert Server.read_resource is not original_read_resource @pytest.mark.asyncio diff --git a/tests/integrations/pydantic_ai/test_pydantic_ai.py b/tests/integrations/pydantic_ai/test_pydantic_ai.py index e100e62671..c67a399d40 100644 --- a/tests/integrations/pydantic_ai/test_pydantic_ai.py +++ b/tests/integrations/pydantic_ai/test_pydantic_ai.py @@ -3591,15 +3591,8 @@ async def test_tool_execution_without_span_context(sentry_init, capture_items): # Call without span context (no transaction active) # The patches should handle this gracefully - try: - # This will fail because we're not in a transaction, but it should not crash - await agent.run("test") - except Exception: - # Expected to fail, that's okay - pass - - # Should not crash - assert True + result = await agent.run("test") + assert result.output is not None @pytest.mark.asyncio diff --git a/tests/test_client.py b/tests/test_client.py index 7dc3922285..207d6b7590 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -1038,20 +1038,8 @@ def custom_repr(value): assert "Fail object" in my_vars["fail"] -@pytest.mark.parametrize( - "dsn", - [ - "http://894b7d594095440f8dfea9b300e6f572@localhost:8000/2", - "http://894b7d594095440f8dfea9b300e6f572@localhost:8000/2", - ], -) -def test_init_string_types(dsn, sentry_init): - # Allow unicode strings on Python 3 and both on Python 2 (due to - # unicode_literals) - # - # Supporting bytes on Python 3 is not really wrong but probably would be - # extra code - sentry_init(dsn) +def test_init_string_types(sentry_init): + sentry_init("http://894b7d594095440f8dfea9b300e6f572@localhost:8000/2") assert ( sentry_sdk.get_client().dsn == "http://894b7d594095440f8dfea9b300e6f572@localhost:8000/2" diff --git a/tests/test_envelope.py b/tests/test_envelope.py index 5549cc6e36..3fd5a9fbfa 100644 --- a/tests/test_envelope.py +++ b/tests/test_envelope.py @@ -73,9 +73,8 @@ def test_add_and_get_session(): expected = Session() envelope.add_session(expected) - for item in envelope: - if item.type == "session": - assert item.payload.json == expected.to_json() + (item,) = [item for item in envelope if item.type == "session"] + assert item.payload.json == expected.to_json() def test_envelope_headers(sentry_init, capture_envelopes, monkeypatch): diff --git a/tests/test_utils.py b/tests/test_utils.py index d5b9713cce..1230921eac 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -835,7 +835,6 @@ def test_parse_version(version, expected_result): ), False, ), - ("1.0.1", (2, 0, 0), False), ("2.0", (1,), True), ( "2.0.1", From 188f0c0612df6ce5fa91afc6d494a48ca493eb1a Mon Sep 17 00:00:00 2001 From: "sentry-junior[bot]" <264270552+sentry-junior[bot]@users.noreply.github.com> Date: Mon, 16 Nov 2026 10:50:25 +0000 Subject: [PATCH 2/3] ci: Run falsegreen on changed test lines in PRs Fails on high-confidence findings on lines a PR adds or changes and reports low-confidence ones as warnings. --- .github/workflows/falsegreen.yml | 32 ++++++++ pyproject.toml | 9 +++ scripts/falsegreen_changed_tests.py | 111 ++++++++++++++++++++++++++++ uv.lock | 10 +++ 4 files changed, 162 insertions(+) create mode 100644 .github/workflows/falsegreen.yml create mode 100644 scripts/falsegreen_changed_tests.py diff --git a/.github/workflows/falsegreen.yml b/.github/workflows/falsegreen.yml new file mode 100644 index 0000000000..747a7df927 --- /dev/null +++ b/.github/workflows/falsegreen.yml @@ -0,0 +1,32 @@ +name: falsegreen + +on: + pull_request: + paths: + - "tests/**.py" + +concurrency: + group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }} + cancel-in-progress: true + +permissions: + contents: read + +jobs: + falsegreen: + name: Check changed tests for false greens + runs-on: ubuntu-latest + timeout-minutes: 10 + + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + # The merge commit and its first parent, the base branch + fetch-depth: 2 + - name: Install uv + uses: astral-sh/setup-uv@68559e99cc807c1eb7e9238fd43a15bba460eec2 # v10.3.3 + with: + python-version: 3.14 + + - name: Run falsegreen + run: uv run --group falsegreen python scripts/falsegreen_changed_tests.py diff --git a/pyproject.toml b/pyproject.toml index e0cef5f340..09e2ca2575 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -14,6 +14,10 @@ dev = [ "ruff", ] +falsegreen = [ + "falsegreen==0.9.2", +] + toxgen = [ "jinja2", "packaging", @@ -323,6 +327,11 @@ ignore = [ "scripts/**" = ["T20"] "tests/**" = ["T20"] +[tool.falsegreen] +# C1 (assert under if/for) and C13b (mock without autospec) flag patterns these +# tests use on purpose, like branching on span_streaming +disable = ["C1", "C13b"] + [tool.ruff.format] # ruff format already excludes the same files as specified in extend-exclude # Ensure Python 3.7 compatibility - avoid using Python 3.9+ syntax features diff --git a/scripts/falsegreen_changed_tests.py b/scripts/falsegreen_changed_tests.py new file mode 100644 index 0000000000..33e3626c48 --- /dev/null +++ b/scripts/falsegreen_changed_tests.py @@ -0,0 +1,111 @@ +""" +Run falsegreen on the test lines that a pull request adds or changes. + +High-confidence findings fail the run, low-confidence findings are reported as +warnings. Findings on lines the pull request didn't touch are ignored. + +In CI, HEAD is the pull request merge commit, so its first parent is the base +branch. Locally, commit your changes and pass the commit to compare against: + + uv run --group falsegreen python scripts/falsegreen_changed_tests.py \ + "$(git merge-base origin/master HEAD)" +""" + +import json +import re +import subprocess +import sys +from collections import defaultdict + +HUNK_RE = re.compile(r"^@@ -\d+(?:,\d+)? \+(\d+)(?:,(\d+))? @@") + +# falsegreen exit codes: 0 clean, 10 low-confidence findings only, +# 20 at least one high-confidence finding +FALSEGREEN_EXIT_CODES = (0, 10, 20) + + +def changed_test_lines(base_ref): + diff = subprocess.run( + [ + "git", + "diff", + "--unified=0", + "--diff-filter=AM", + "--no-color", + base_ref, + "HEAD", + "--", + "tests/*.py", + ], + check=True, + capture_output=True, + text=True, + ).stdout + + lines = defaultdict(set) + path = None + for line in diff.splitlines(): + if line.startswith("+++ b/"): + path = line[len("+++ b/") :] + continue + + match = HUNK_RE.match(line) + if match and path is not None: + start = int(match.group(1)) + count = int(match.group(2) or 1) + lines[path].update(range(start, start + count)) + + return lines + + +def main(): + base_ref = sys.argv[1] if len(sys.argv) > 1 else "HEAD^1" + lines = changed_test_lines(base_ref) + if not lines: + print("No changed test files.") + return 0 + + result = subprocess.run( + [sys.executable, "-m", "falsegreen", "--json", *sorted(lines)], + capture_output=True, + text=True, + ) + if result.returncode not in FALSEGREEN_EXIT_CODES: + sys.stderr.write(result.stderr) + return result.returncode + + findings = [ + finding + for finding in json.loads(result.stdout) + if finding["line"] in lines[finding["file"]] + ] + + for finding in findings: + level = "error" if finding["confidence"] == "high" else "warning" + print( + "::{} file={},line={},title=falsegreen {}::{}. {}".format( + level, + finding["file"], + finding["line"], + finding["code"], + finding["title"], + finding.get("fix", ""), + ) + ) + + high = sum(finding["confidence"] == "high" for finding in findings) + print( + "falsegreen: {} high, {} low finding(s) on changed lines.".format( + high, len(findings) - high + ) + ) + if high: + print( + "If a finding is a false positive, silence it with " + "`# falsegreen: ignore[CODE]` on that line." + ) + return 1 if high else 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/uv.lock b/uv.lock index 4d031deed2..88b5bced0f 100644 --- a/uv.lock +++ b/uv.lock @@ -26,6 +26,7 @@ docs = [ { name = "sphinx-autodoc-typehints", extras = ["type-comments"], specifier = ">=2.3.0" }, { name = "typing-extensions" }, ] +falsegreen = [{ name = "falsegreen", specifier = "==0.9.2" }] test = [ { name = "dataclasses", marker = "python_full_version < '3.7'" }, { name = "pytest", specifier = ">=6.0.0" }, @@ -809,6 +810,15 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/50/a7/bb99bf5e6f78736ddb53480f2c3ff3702ffe2196a7c5e1661c03081d398e/eval_type_backport-0.4.0-py3-none-any.whl", hash = "sha256:ad5e2a8db71b6696a56eafb938b0f5a337d3217f256b8e158b469422b4772b20", size = 6432, upload-time = "2026-06-02T13:22:04.827Z" }, ] +[[package]] +name = "falsegreen" +version = "0.9.2" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/32/39/5ba28aff8cebc5dc0227856bdb8bdb81e37deea617016bbcc5a3b022acb1/falsegreen-0.9.2.tar.gz", hash = "sha256:c4ea886c2e88f8abcdaed0f468c03159f87a320c321dec4eaab0b611c0f5e02d", size = 177191, upload-time = "2026-07-16T14:16:37.265Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/7c/84/edced8d6ad5a7d98128ca87c874997ced39251deb8444459a11b07ba367c/falsegreen-0.9.2-py3-none-any.whl", hash = "sha256:fa0f98cd8c38728da4a731fc4f649e222e668e93b34c99c7d5da2f5792464d6a", size = 74313, upload-time = "2026-07-16T14:16:35.883Z" }, +] + [[package]] name = "fcache" version = "0.5.0" From dc8aaabb218f34aeebdfecd1700b8bf4b37ba1b5 Mon Sep 17 00:00:00 2001 From: "sentry-junior[bot]" <264270552+sentry-junior[bot]@users.noreply.github.com> Date: Mon, 16 Nov 2026 10:52:57 +0000 Subject: [PATCH 3/3] Revert "ci: Run falsegreen on changed test lines in PRs" This reverts commit 188f0c0612df6ce5fa91afc6d494a48ca493eb1a. --- .github/workflows/falsegreen.yml | 32 -------- pyproject.toml | 9 --- scripts/falsegreen_changed_tests.py | 111 ---------------------------- uv.lock | 10 --- 4 files changed, 162 deletions(-) delete mode 100644 .github/workflows/falsegreen.yml delete mode 100644 scripts/falsegreen_changed_tests.py diff --git a/.github/workflows/falsegreen.yml b/.github/workflows/falsegreen.yml deleted file mode 100644 index 747a7df927..0000000000 --- a/.github/workflows/falsegreen.yml +++ /dev/null @@ -1,32 +0,0 @@ -name: falsegreen - -on: - pull_request: - paths: - - "tests/**.py" - -concurrency: - group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }} - cancel-in-progress: true - -permissions: - contents: read - -jobs: - falsegreen: - name: Check changed tests for false greens - runs-on: ubuntu-latest - timeout-minutes: 10 - - steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - with: - # The merge commit and its first parent, the base branch - fetch-depth: 2 - - name: Install uv - uses: astral-sh/setup-uv@68559e99cc807c1eb7e9238fd43a15bba460eec2 # v10.3.3 - with: - python-version: 3.14 - - - name: Run falsegreen - run: uv run --group falsegreen python scripts/falsegreen_changed_tests.py diff --git a/pyproject.toml b/pyproject.toml index bf1f0f202c..c98f3d62cc 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -14,10 +14,6 @@ dev = [ "ruff", ] -falsegreen = [ - "falsegreen==0.9.2", -] - toxgen = [ "jinja2", "packaging", @@ -324,11 +320,6 @@ ignore = [ "scripts/**" = ["T20"] "tests/**" = ["T20"] -[tool.falsegreen] -# C1 (assert under if/for) and C13b (mock without autospec) flag patterns these -# tests use on purpose, like branching on span_streaming -disable = ["C1", "C13b"] - [tool.ruff.format] # ruff format already excludes the same files as specified in extend-exclude # Ensure Python 3.7 compatibility - avoid using Python 3.9+ syntax features diff --git a/scripts/falsegreen_changed_tests.py b/scripts/falsegreen_changed_tests.py deleted file mode 100644 index 33e3626c48..0000000000 --- a/scripts/falsegreen_changed_tests.py +++ /dev/null @@ -1,111 +0,0 @@ -""" -Run falsegreen on the test lines that a pull request adds or changes. - -High-confidence findings fail the run, low-confidence findings are reported as -warnings. Findings on lines the pull request didn't touch are ignored. - -In CI, HEAD is the pull request merge commit, so its first parent is the base -branch. Locally, commit your changes and pass the commit to compare against: - - uv run --group falsegreen python scripts/falsegreen_changed_tests.py \ - "$(git merge-base origin/master HEAD)" -""" - -import json -import re -import subprocess -import sys -from collections import defaultdict - -HUNK_RE = re.compile(r"^@@ -\d+(?:,\d+)? \+(\d+)(?:,(\d+))? @@") - -# falsegreen exit codes: 0 clean, 10 low-confidence findings only, -# 20 at least one high-confidence finding -FALSEGREEN_EXIT_CODES = (0, 10, 20) - - -def changed_test_lines(base_ref): - diff = subprocess.run( - [ - "git", - "diff", - "--unified=0", - "--diff-filter=AM", - "--no-color", - base_ref, - "HEAD", - "--", - "tests/*.py", - ], - check=True, - capture_output=True, - text=True, - ).stdout - - lines = defaultdict(set) - path = None - for line in diff.splitlines(): - if line.startswith("+++ b/"): - path = line[len("+++ b/") :] - continue - - match = HUNK_RE.match(line) - if match and path is not None: - start = int(match.group(1)) - count = int(match.group(2) or 1) - lines[path].update(range(start, start + count)) - - return lines - - -def main(): - base_ref = sys.argv[1] if len(sys.argv) > 1 else "HEAD^1" - lines = changed_test_lines(base_ref) - if not lines: - print("No changed test files.") - return 0 - - result = subprocess.run( - [sys.executable, "-m", "falsegreen", "--json", *sorted(lines)], - capture_output=True, - text=True, - ) - if result.returncode not in FALSEGREEN_EXIT_CODES: - sys.stderr.write(result.stderr) - return result.returncode - - findings = [ - finding - for finding in json.loads(result.stdout) - if finding["line"] in lines[finding["file"]] - ] - - for finding in findings: - level = "error" if finding["confidence"] == "high" else "warning" - print( - "::{} file={},line={},title=falsegreen {}::{}. {}".format( - level, - finding["file"], - finding["line"], - finding["code"], - finding["title"], - finding.get("fix", ""), - ) - ) - - high = sum(finding["confidence"] == "high" for finding in findings) - print( - "falsegreen: {} high, {} low finding(s) on changed lines.".format( - high, len(findings) - high - ) - ) - if high: - print( - "If a finding is a false positive, silence it with " - "`# falsegreen: ignore[CODE]` on that line." - ) - return 1 if high else 0 - - -if __name__ == "__main__": - sys.exit(main()) diff --git a/uv.lock b/uv.lock index 88b5bced0f..4d031deed2 100644 --- a/uv.lock +++ b/uv.lock @@ -26,7 +26,6 @@ docs = [ { name = "sphinx-autodoc-typehints", extras = ["type-comments"], specifier = ">=2.3.0" }, { name = "typing-extensions" }, ] -falsegreen = [{ name = "falsegreen", specifier = "==0.9.2" }] test = [ { name = "dataclasses", marker = "python_full_version < '3.7'" }, { name = "pytest", specifier = ">=6.0.0" }, @@ -810,15 +809,6 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/50/a7/bb99bf5e6f78736ddb53480f2c3ff3702ffe2196a7c5e1661c03081d398e/eval_type_backport-0.4.0-py3-none-any.whl", hash = "sha256:ad5e2a8db71b6696a56eafb938b0f5a337d3217f256b8e158b469422b4772b20", size = 6432, upload-time = "2026-06-02T13:22:04.827Z" }, ] -[[package]] -name = "falsegreen" -version = "0.9.2" -source = { registry = "https://pypi.org/simple" } -sdist = { url = "https://files.pythonhosted.org/packages/32/39/5ba28aff8cebc5dc0227856bdb8bdb81e37deea617016bbcc5a3b022acb1/falsegreen-0.9.2.tar.gz", hash = "sha256:c4ea886c2e88f8abcdaed0f468c03159f87a320c321dec4eaab0b611c0f5e02d", size = 177191, upload-time = "2026-07-16T14:16:37.265Z" } -wheels = [ - { url = "https://files.pythonhosted.org/packages/7c/84/edced8d6ad5a7d98128ca87c874997ced39251deb8444459a11b07ba367c/falsegreen-0.9.2-py3-none-any.whl", hash = "sha256:fa0f98cd8c38728da4a731fc4f649e222e668e93b34c99c7d5da2f5792464d6a", size = 74313, upload-time = "2026-07-16T14:16:35.883Z" }, -] - [[package]] name = "fcache" version = "0.5.0"