Repository navigation
feat(comparators): add AngleComparator for wraparound angles - #633
Conversation
- NumericComparator scores 359 against 1 as 358 apart; AngleComparator uses the shortest difference around the circle, so they are 2 apart - binary score like NumericComparator: 1.0 within max_angular_difference, 0.0 beyond it; angle_unit is degrees or radians - each angle is reduced to one turn before subtracting, so very large inputs neither overflow nor lose the angle - the tolerance check reuses the ThresholdHelper float tolerance, so 359.9 against 0.1 meets 0.2 - an unknown unit or a tolerance that is negative, non-finite, too large for a float or at least half a turn raises ValueError - strings, booleans, Decimal, infinite and overflowing values score 0.0 - registered for JSON Schema and stickler config import and exported from stickler and stickler.comparators
Security Scan ResultsASH Security Scan Report
Scan Metadata
SummaryScanner ResultsThe table below shows findings by scanner, with status based on severity thresholds and dependencies:
Report generated by Automated Security Helper (ASH) at 2026-11-27T21:19:34+00:00 |
vawsgit
left a comment
There was a problem hiding this comment.
needs changes
One blocker, posted inline on angle.py:102-105: the math.remainder calls trip a semgrep Python-3.7 compatibility rule. The required security (3.12) check fails because of it, so the PR cannot merge. It is a false positive. The fix is two # nosemgrep: comments. The inline comment has the exact text, and I tested it.
The comparator behaved correctly in every case I ran. It is ready apart from the blocker.
What I verified at fb027c0
- Full suite with
PYTHONPATHset to the PR'ssrcgives 6627 passed, 13 skipped, 1 xfailed, 0 failed. The 13 skips are thebertandstrandsextras plus placeholder tests, and none touchesAngleComparator.test_angle.pygives 29 passed. Every other CI check is green. - Wraparound in degrees with a tolerance of 5: 359/1, -1/1 and 359.9/0.1 score 1.0. 0/180, 90/-90 and 184/4 score 0.0.
- Whole turns at the default tolerance: 0/360, 0/720 and -90/630 score 1.0.
- Large values do not overflow: 1e308 vs -1e308 scores 0.0, and 1e17 vs 280 scores 1.0, as the PR body claims.
- Radians: 0/2π and π/-π score 1.0. Tolerance edges are inclusive: 2 for 359/1, and 0.2 for 359.9/0.1.
- Bad configuration raises
ValueError: unknown unit, negative, 180, π in radians, NaN, bool, str,10**400and None. - Bad inputs (str, bool, Decimal, complex, inf) score 0.0. None and NaN follow the shared
base.pypolicy, the same asNumericComparator. - Integration: the registry name resolves.
from_json_schemaandmodel_from_jsonboth build a working comparator. Exporting withto_json_schemaorto_stickler_configand re-importing keepsangle_unit="radians", and scores match.List[float]elements pair across the wrap. - Mutation testing: the tests caught 11 of 13 mutations. The one meaningful survivor is the first low item below. The other,
fmodfor the inner calls, gives the same results. - The PR is not stale. Its merge base is the current
devtip, and the change only adds lines.
Low, non-blocking
test_config_round_tripscomparesconfigwithconfig, so droppingangle_unitfromconfigstill passes all 29 tests. Asserting behaviour after the round trip would catch it, for exampleAngleComparator(**c.config).compare(0, 2*math.pi) == 1.0for a radians comparator.- The bool-input test uses
Truevs 10, which scores 0.0 even without the bool check. UsingTruevs 1 would pin the check. - My test counts differ from the PR body: 6627 against 6626 for the full suite, and 641 against 821 for
tests/integrations. The likely cause is different installed extras. Neither difference points to a defect.
| math.remainder( | ||
| math.remainder(float1, full_turn) - math.remainder(float2, full_turn), | ||
| full_turn, | ||
| ) |
There was a problem hiding this comment.
Blocker (high): these math.remainder calls make the required security (3.12) check fail.
Semgrep rule python.lang.compatibility.python37.python37-compatibility-math1 reports 3 findings here: the outer call (lines 102-105) and both inner calls (line 103). They are false positives, because pyproject.toml sets requires-python = ">=3.10" and math.remainder exists from 3.7. ASH still fails at its MEDIUM threshold:
semgrep │ 3 │ 3 │ 0 │ 0 │ 0 │ 0 │ 23.1s │ 3 │ FAILED │ MED (g) │
ERROR (2) Exiting due to 3 actionable findings found in ASH scan
(run 45744687421). I reproduced it locally: uvx semgrep --metrics=off --config r/python.lang.compatibility.python37.python37-compatibility-math1 angle.py gives Ran 1 rule on 1 file: 3 findings. security (3.12) is in the dev ruleset's required status checks, so this PR cannot merge as it stands.
Fix: suppress it with # nosemgrep: and a reason, as the repo already does for the non-literal-import false positives (comparator_registry.py:114). It needs a comment above line 102 and one above line 103. One comment does not cover the inner calls. With both comments, the same semgrep command gives 0 findings:
# requires-python is >=3.10, so math.remainder (3.7+) is always present.
# nosemgrep: python.lang.compatibility.python37.python37-compatibility-math1
math.remainder(
# nosemgrep: python.lang.compatibility.python37.python37-compatibility-math1
math.remainder(float1, full_turn) - math.remainder(float2, full_turn),Changing the inner calls to math.fmod, as the scanner suggests, does not clear the scan, because the outer remainder is still flagged.
…mainder requires-python is >=3.10 and math.remainder exists since 3.7. The outer and inner calls each need a nosemgrep comment.
|
Both points are addressed.
Ready for another look. |
vawsgit
left a comment
There was a problem hiding this comment.
✅ LGTM!
The two new commits fix the blocker and both low items from my review at fb027c0. I found no new medium or high problems.
Previous findings, checked at df9f245
- Blocker (semgrep
python37-compatibility-math1): fixed.angle.py:102-106has two# nosemgrep:comments, each naming only that rule. The rule went from 3 findings to 0:Ran 1 rule on 1 file: 0 findings.With--disable-nosemit reports the same 3 findings, andp/pythonplusp/default(320 rules) also gives 3 with--disable-nosemand 0 without. So the comments hide nothing else. The parsed code ofangle.pyis identical before and after, so behaviour is unchanged.security (3.12)now passes. test_config_round_trips: fixed. It now scores the rebuilt comparator. Droppingangle_unitfromconfigfails it (1 failed, 28 passed). Droppingmax_angular_differencealso fails it.- Bool input: fixed. The test now compares
Truewith1. Removing the bool check in_finite_floatfailstest_malformed_input_scores_zero[True-1].
Other checks
test_angle.py: 29 passed. Full suite with the PR'ssrc(and thellmextra installed): 6636 passed, 11 skipped, 1 xfailed, 0 failed.- Three more mutations of the input checks (
Real,isfinite, theOverflowErrorguard) each fail at least one test. - Not stale: the merge base is the current
devtip,devhas no commits the PR lacks, and the merge is clean. - All 11 CI checks pass at
df9f245.
# Conflicts: # CHANGELOG.md # src/stickler/comparators/README.md # src/stickler/structured_object_evaluator/README.md
tl;dr for humans
NumericComparatorscores a heading of 359 against 1 as 358 apart, so orientation cannot be scored. This addsAngleComparator, which uses the shortest difference around the circle, in degrees or radians. No existing comparator, default or output changes.for agents
AngleComparator(threshold=None, max_angular_difference=0.0, angle_unit="degrees"),DEFAULT_THRESHOLD = 1.0.max_angular_difference(inclusive, with theThresholdHelperfloat tolerance), else 0.0, the same binary rule asNumericComparator(absolute_tolerance=...).1e308vs-1e308scores 0.0 and1e17vs280.0scores 1.0 without overflow.ValueErrorfor an unknown unit, or a tolerance that is negative, non-finite, a bool, a string, too large for a float, or at least half a turn (which would match every pair).Decimal, inf and overflowing values score 0.0;Noneand float NaN follow the shared policy inbase.py.List[float]field each element is an angle and elements pair as usual.src/stickler/comparators/angle.py; one line each instickler/__init__.py,stickler/comparators/__init__.py,comparator_registry.py,tests/test_top_level_exports.py,tests/common/comparators/test_none_handling.py; docs inREADME.md,docs/docs/API-Reference/comparators.md,docs/docs/Guides/Comparators/README.md,src/stickler/comparators/Comparators.md,src/stickler/comparators/README.md,src/stickler/structured_object_evaluator/README.md;CHANGELOG.mdunder[Unreleased]/Added.fb027c0feat(comparators): add AngleComparator for wraparound angles, minor: a new capability.tests/common/comparators/test_angle.py(29 tests:test_degrees_use_the_shortest_difference,test_tolerance_is_inclusive,test_large_angles_reduce_without_overflow,test_radians,test_invalid_config_raises,test_malformed_input_scores_zero,test_missing_values_follow_the_shared_policy,test_config_round_trips,test_structured_model_field_and_list_field,test_json_schema_and_config_import, and more). Full suite on this branch (inspect.getfile(stickler)pointed at the branch'ssrc/): 6626 passed, 13 skipped, 1 xfailed;tests/integrations821 passed.compare_withoutput for models using only existing comparators is byte-identical todevover 1200 runs.uv lock --checkpasses; no dependency or workflow change.PointDistanceComparatorPR conflicts only in three docs files, keeping both sides.