Skip to content

chore: add utils CRAP score check#577

Merged
marandaneto merged 13 commits into
mainfrom
chore/add-utils-crap-score-check
May 26, 2026
Merged

chore: add utils CRAP score check#577
marandaneto merged 13 commits into
mainfrom
chore/add-utils-crap-score-check

Conversation

@marandaneto
Copy link
Copy Markdown
Member

@marandaneto marandaneto commented May 15, 2026

💡 Motivation and Context

follow up #576

Follow-up to the targeted mutation-testing PR: add a CRAP-score gate for posthog/utils.py using pytest-crap instead of a hardcoded CRAP formula. This keeps the complexity/coverage quality bar explicit in CI while avoiding custom reimplementation of CRAP scoring.

💚 How did you test it?

  • uv run --extra test --with pytest-crap --with pytest-cov pytest posthog/test/test_utils.py posthog/test/test_size_limited_dict.py --timeout=30 --cov=posthog.utils --crap --crap-threshold=10 -q
  • uv run --extra test --with pytest-crap --with pytest-cov python .github/scripts/check_crap_threshold.py posthog/utils.py --max-crap 10
  • rm -f .coverage && rm -rf mutants && uv run --extra test --with mutmut mutmut run --max-children 1 && uv run --extra test --with mutmut mutmut results
  • uv run --extra test pytest --timeout=30 -q
  • uvx ruff check .github/scripts/check_crap_threshold.py posthog/utils.py posthog/test/test_utils.py posthog/test/test_size_limited_dict.py
  • ruby -e 'require "yaml"; YAML.load_file(".github/workflows/ci.yml"); puts "ci yaml ok"'

📝 Checklist

  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • I updated the docs if needed.
  • No breaking change or entry added to the changelog.

If releasing new changes

  • Ran sampo add to generate a changeset file

@marandaneto marandaneto requested a review from a team as a code owner May 15, 2026 23:36
@marandaneto marandaneto marked this pull request as draft May 15, 2026 23:36
@socket-security
Copy link
Copy Markdown

socket-security Bot commented May 15, 2026

No dependency changes detected. Learn more about Socket for GitHub.

👍 No dependency changes detected in pull request

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented May 15, 2026

Prompt To Fix All With AI
Fix the following 3 code review issues. Work through them one at a time, proposing concise fixes.

---

### Issue 1 of 3
posthog/utils.py:415-424
**Hidden deletion side-effect in a predicate method**

`_key_has_version` reads as a pure boolean predicate, but it silently deletes the Redis key when JSON parsing fails. The caller `_delete_keys_with_version` already handles deletion for the `True` return path, so a reader expects `False` to mean "no action taken." The covert delete on the error path will fire even if a caller simply wanted to check the version without intending to clean up. Consider separating the concern — either rename to something like `_version_matches_or_delete_corrupt` to make the side-effect visible, or move the `self.redis.delete(key)` call back into `_delete_keys_with_version` after catching the exception there.

### Issue 2 of 3
posthog/test/test_utils.py:299-351
**New edge-case assertions embedded in the existing test rather than parameterised**

The team prefers parameterised tests. The six new `with` blocks (missing `win32_ver`, empty `mac_ver`, absent `mac_ver`, empty Linux distro, missing `release`) are appended inline inside `test_get_os_info` rather than expressed as `@pytest.mark.parametrize` cases. Inlining them increases the method's size and makes it harder to identify which scenario failed on a CI failure. Each private helper (`_get_windows_os_info`, `_get_macos_info`, `_get_linux_os_info`, `_platform_release`) could have its own parameterised test covering both the happy path and the fallback path.

### Issue 3 of 3
posthog/test/test_utils.py:713-722
**`test_redis_cache_key_helpers` bundles unrelated scenarios into one test**

The test exercises `_redis_key_to_string`, `_key_has_version` (no data), `_key_has_version` (corrupt JSON + implicit deletion), and the resulting store mutation — four distinct behaviours in a single method. Consistent with the team's preference for parameterised/focused tests, splitting these into individual tests (or a `@pytest.mark.parametrize` table) would make failure messages immediately actionable without having to mentally trace which assertion fired.

Reviews (1): Last reviewed commit: "chore: add utils CRAP score check" | Re-trigger Greptile

Comment thread posthog/utils.py
Comment thread posthog/test/test_utils.py Outdated
Comment thread posthog/test/test_utils.py Outdated
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 16, 2026

posthog-python Compliance Report

Date: 2026-05-26 15:35:11 UTC
Duration: 176150ms

✅ All Tests Passed!

45/45 tests passed


Capture Tests

29/29 tests passed

View Details
Test Status Duration
Format Validation.Event Has Required Fields 518ms
Format Validation.Event Has Uuid 1507ms
Format Validation.Event Has Lib Properties 1508ms
Format Validation.Distinct Id Is String 1507ms
Format Validation.Token Is Present 1508ms
Format Validation.Custom Properties Preserved 1507ms
Format Validation.Event Has Timestamp 1507ms
Retry Behavior.Retries On 503 9515ms
Retry Behavior.Does Not Retry On 400 3510ms
Retry Behavior.Does Not Retry On 401 3509ms
Retry Behavior.Respects Retry After Header 9510ms
Retry Behavior.Implements Backoff 23534ms
Retry Behavior.Retries On 500 7505ms
Retry Behavior.Retries On 502 7509ms
Retry Behavior.Retries On 504 7517ms
Retry Behavior.Max Retries Respected 23529ms
Deduplication.Generates Unique Uuids 1498ms
Deduplication.Preserves Uuid On Retry 7512ms
Deduplication.Preserves Uuid And Timestamp On Retry 14526ms
Deduplication.Preserves Uuid And Timestamp On Batch Retry 7505ms
Deduplication.No Duplicate Events In Batch 1509ms
Deduplication.Different Events Have Different Uuids 1507ms
Compression.Sends Gzip When Enabled 1508ms
Batch Format.Uses Proper Batch Structure 1507ms
Batch Format.Flush With No Events Sends Nothing 1006ms
Batch Format.Multiple Events Batched Together 1506ms
Error Handling.Does Not Retry On 403 3510ms
Error Handling.Does Not Retry On 413 3508ms
Error Handling.Retries On 408 7515ms

Feature_Flags Tests

16/16 tests passed

View Details
Test Status Duration
Request Payload.Request With Person Properties Device Id 1003ms
Request Payload.Flags Request Uses V2 Query Param 1008ms
Request Payload.Flags Request Hits Flags Path Not Decide 1007ms
Request Payload.Flags Request Omits Authorization Header 1007ms
Request Payload.Token In Flags Body Matches Init 1007ms
Request Payload.Groups Round Trip 1008ms
Request Payload.Groups Default To Empty Object 1007ms
Request Payload.Person Properties Distinct Id Auto Populated When Caller Omits It 1007ms
Request Payload.Disable Geoip False Propagates As Geoip Disable False 1007ms
Request Payload.Disable Geoip Omitted Defaults To False 1007ms
Request Payload.Flag Keys To Evaluate Contains Only Requested Key 1007ms
Request Lifecycle.No Flags Request On Init Alone 503ms
Request Lifecycle.No Flags Request On Normal Capture 1508ms
Request Lifecycle.Two Flag Calls Produce Two Remote Requests 1012ms
Request Lifecycle.Mock Response Value Is Returned To Caller 1003ms
Side Effect Events.Get Feature Flag Captures Feature Flag Called Event 1510ms

@github-actions
Copy link
Copy Markdown
Contributor

This PR hasn't seen activity in a week! Should it be merged, closed, or further worked on? If you want to keep it open, post a comment or remove the stale label – otherwise this will be closed in another week.

@github-actions github-actions Bot added the stale label May 25, 2026
@marandaneto marandaneto marked this pull request as ready for review May 26, 2026 10:23
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented May 26, 2026

Reviews (2): Last reviewed commit: "restore useful utility comments" | Re-trigger Greptile

@marandaneto
Copy link
Copy Markdown
Member Author

merged #578 into this branch so you can skip the acceptance tests review, forgot about the stacked PRs, sorry

Base automatically changed from chore/add-utils-mutation-tests to main May 26, 2026 15:26
# Conflicts:
#	.github/workflows/ci.yml
#	posthog/test/test_utils.py
#	posthog/utils.py
@marandaneto marandaneto merged commit 461226c into main May 26, 2026
27 checks passed
@marandaneto marandaneto deleted the chore/add-utils-crap-score-check branch May 26, 2026 15:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants