civics-cdf-validator 1.69.dev18__tar.gz → 1.69.dev19__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (32) hide show
  1. {civics_cdf_validator-1.69.dev18/civics_cdf_validator.egg-info → civics_cdf_validator-1.69.dev19}/PKG-INFO +1 -1
  2. {civics_cdf_validator-1.69.dev18 → civics_cdf_validator-1.69.dev19/civics_cdf_validator.egg-info}/PKG-INFO +1 -1
  3. {civics_cdf_validator-1.69.dev18 → civics_cdf_validator-1.69.dev19}/rulesets/election_results_rules.py +59 -0
  4. {civics_cdf_validator-1.69.dev18 → civics_cdf_validator-1.69.dev19}/rulesets/election_rules.py +6 -32
  5. {civics_cdf_validator-1.69.dev18 → civics_cdf_validator-1.69.dev19}/rulesets/rule_util.py +15 -0
  6. {civics_cdf_validator-1.69.dev18 → civics_cdf_validator-1.69.dev19}/version.py +1 -1
  7. {civics_cdf_validator-1.69.dev18 → civics_cdf_validator-1.69.dev19}/CONTRIBUTING.md +0 -0
  8. {civics_cdf_validator-1.69.dev18 → civics_cdf_validator-1.69.dev19}/LICENSE-2.0.txt +0 -0
  9. {civics_cdf_validator-1.69.dev18 → civics_cdf_validator-1.69.dev19}/MANIFEST.in +0 -0
  10. {civics_cdf_validator-1.69.dev18 → civics_cdf_validator-1.69.dev19}/README.md +0 -0
  11. {civics_cdf_validator-1.69.dev18 → civics_cdf_validator-1.69.dev19}/__init__.py +0 -0
  12. {civics_cdf_validator-1.69.dev18 → civics_cdf_validator-1.69.dev19}/base.py +0 -0
  13. {civics_cdf_validator-1.69.dev18 → civics_cdf_validator-1.69.dev19}/civics_cdf_validator.egg-info/SOURCES.txt +0 -0
  14. {civics_cdf_validator-1.69.dev18 → civics_cdf_validator-1.69.dev19}/civics_cdf_validator.egg-info/dependency_links.txt +0 -0
  15. {civics_cdf_validator-1.69.dev18 → civics_cdf_validator-1.69.dev19}/civics_cdf_validator.egg-info/entry_points.txt +0 -0
  16. {civics_cdf_validator-1.69.dev18 → civics_cdf_validator-1.69.dev19}/civics_cdf_validator.egg-info/requires.txt +0 -0
  17. {civics_cdf_validator-1.69.dev18 → civics_cdf_validator-1.69.dev19}/civics_cdf_validator.egg-info/top_level.txt +0 -0
  18. {civics_cdf_validator-1.69.dev18 → civics_cdf_validator-1.69.dev19}/gpunit_rules.py +0 -0
  19. {civics_cdf_validator-1.69.dev18 → civics_cdf_validator-1.69.dev19}/loggers.py +0 -0
  20. {civics_cdf_validator-1.69.dev18 → civics_cdf_validator-1.69.dev19}/office_utils.py +0 -0
  21. {civics_cdf_validator-1.69.dev18 → civics_cdf_validator-1.69.dev19}/rules.py +0 -0
  22. {civics_cdf_validator-1.69.dev18 → civics_cdf_validator-1.69.dev19}/rulesets/__init__.py +0 -0
  23. {civics_cdf_validator-1.69.dev18 → civics_cdf_validator-1.69.dev19}/rulesets/committee_rules.py +0 -0
  24. {civics_cdf_validator-1.69.dev18 → civics_cdf_validator-1.69.dev19}/rulesets/common_rules.py +0 -0
  25. {civics_cdf_validator-1.69.dev18 → civics_cdf_validator-1.69.dev19}/rulesets/election_dates_rules.py +0 -0
  26. {civics_cdf_validator-1.69.dev18 → civics_cdf_validator-1.69.dev19}/rulesets/metadata_rules.py +0 -0
  27. {civics_cdf_validator-1.69.dev18 → civics_cdf_validator-1.69.dev19}/rulesets/officeholder_rules.py +0 -0
  28. {civics_cdf_validator-1.69.dev18 → civics_cdf_validator-1.69.dev19}/rulesets/voter_information_rules.py +0 -0
  29. {civics_cdf_validator-1.69.dev18 → civics_cdf_validator-1.69.dev19}/setup.cfg +0 -0
  30. {civics_cdf_validator-1.69.dev18 → civics_cdf_validator-1.69.dev19}/setup.py +0 -0
  31. {civics_cdf_validator-1.69.dev18 → civics_cdf_validator-1.69.dev19}/stats.py +0 -0
  32. {civics_cdf_validator-1.69.dev18 → civics_cdf_validator-1.69.dev19}/validator.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: civics_cdf_validator
3
- Version: 1.69.dev18
3
+ Version: 1.69.dev19
4
4
  Summary: Checks if an election feed follows best practices
5
5
  Home-page: https://github.com/google/civics_cdf_validator
6
6
  Author: Google Civics
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: civics_cdf_validator
3
- Version: 1.69.dev18
3
+ Version: 1.69.dev19
4
4
  Summary: Checks if an election feed follows best practices
5
5
  Home-page: https://github.com/google/civics_cdf_validator
6
6
  Author: Google Civics
@@ -16,6 +16,7 @@
16
16
  from civics_cdf_validator import base
17
17
  from civics_cdf_validator import loggers
18
18
  from civics_cdf_validator.rulesets import election_rules
19
+ from civics_cdf_validator.rulesets import rule_util
19
20
  from lxml import etree
20
21
 
21
22
  _REQUIRED_CONTEST_RESULTS_FIELDS = (
@@ -26,6 +27,16 @@ _REQUIRED_CONTEST_RESULTS_FIELDS = (
26
27
 
27
28
  _SUB_UNITS_FIELD_NAMES = ("SubUnitsReported", "TotalSubUnits")
28
29
 
30
+ _VOTE_TOTAL_TYPES = ("total", "total-percent")
31
+
32
+
33
+ def _get_vote_count_types(selection: etree._Element) -> set[str]:
34
+ """Returns the types of the VoteCounts in a ballot selection."""
35
+ return {
36
+ rule_util.get_type_or_other_type(vote_counts)
37
+ for vote_counts in selection.findall("VoteCountsCollection/VoteCounts")
38
+ }
39
+
29
40
 
30
41
  class RequiredContestResultsFieldsSet(base.SchemaRule):
31
42
  """Checks that every Contest sets its required results fields.
@@ -100,8 +111,56 @@ class ValidatePercentReporting(base.SchemaRule):
100
111
  )
101
112
 
102
113
 
114
+ class CandidateAndBallotMeasureSelectionTotalsRequired(base.SchemaRule):
115
+ """Checks that candidate and ballot measure selections report vote totals.
116
+
117
+ Election results feeds must report total and total-percent VoteCounts for
118
+ every CandidateSelection and BallotMeasureSelection, and for the
119
+ AggregateBallotSelection of every CandidateContest and BallotMeasureContest.
120
+ """
121
+
122
+ def setup(self) -> None:
123
+ self.aggregate_contest_types = {
124
+ "BallotMeasureContest",
125
+ "CandidateContest",
126
+ *self.get_all_subtypes("BallotMeasureContest"),
127
+ *self.get_all_subtypes("CandidateContest"),
128
+ }
129
+
130
+ def elements(self) -> list[str]:
131
+ return [
132
+ "AggregateBallotSelection",
133
+ "BallotMeasureSelection",
134
+ "CandidateSelection",
135
+ *self.get_all_subtypes("BallotMeasureSelection"),
136
+ *self.get_all_subtypes("CandidateSelection"),
137
+ ]
138
+
139
+ def check(self, element: etree._Element) -> None:
140
+ element_class = self.get_element_class(element)
141
+ if (
142
+ element_class == "AggregateBallotSelection"
143
+ and self.get_element_class(element.getparent())
144
+ not in self.aggregate_contest_types
145
+ ):
146
+ return
147
+ vote_count_types = _get_vote_count_types(element)
148
+ missing_types = [
149
+ vote_total_type
150
+ for vote_total_type in _VOTE_TOTAL_TYPES
151
+ if vote_total_type not in vote_count_types
152
+ ]
153
+ if missing_types:
154
+ raise loggers.ElectionError.from_message(
155
+ f"{element_class} {element.get('objectId')} is missing VoteCounts of"
156
+ f" type(s) {', '.join(missing_types)}.",
157
+ [element],
158
+ )
159
+
160
+
103
161
  ELECTION_RESULTS_RULES = election_rules.ELECTION_RULES + (
104
162
  # go/keep-sorted start
163
+ CandidateAndBallotMeasureSelectionTotalsRequired,
105
164
  DataSourcesRequired,
106
165
  RequiredContestResultsFieldsSet,
107
166
  ValidatePercentReporting,
@@ -49,23 +49,6 @@ _SEATS_DELTA_VOTE_COUNT_TYPES = frozenset([
49
49
  ])
50
50
 
51
51
 
52
- def _get_type_or_other_type(element: etree._Element) -> str:
53
- """Returns the OtherType text if Type is "other", else the Type text."""
54
- type_element = element.find("Type")
55
- other_type_element = element.find("OtherType")
56
- type_text = (
57
- type_element.text.strip()
58
- if rule_util.element_has_text(type_element)
59
- else ""
60
- )
61
- other_type_text = (
62
- other_type_element.text.strip()
63
- if rule_util.element_has_text(other_type_element)
64
- else ""
65
- )
66
- return other_type_text if type_text == "other" else type_text
67
-
68
-
69
52
  class PercentSum(base.SchemaRule):
70
53
  """Check that Contest elements have percents summing to 0 or 100.
71
54
 
@@ -97,9 +80,7 @@ class PercentSum(base.SchemaRule):
97
80
  ):
98
81
  other_type = vote_counts.find("OtherType")
99
82
  if other_type is not None and other_type.text == "total-percent":
100
- count = rule_util.get_float(vote_counts.find("Count"))
101
- if count is not None:
102
- sum_percents += count
83
+ sum_percents += rule_util.get_float(vote_counts.find("Count")) or 0
103
84
  if not PercentSum.fuzzy_equals(
104
85
  sum_percents, 0
105
86
  ) and not PercentSum.fuzzy_equals(sum_percents, 100):
@@ -772,11 +753,8 @@ class VoteCountValidSeatsDeltaTypes(base.SchemaRule):
772
753
 
773
754
  def check(self, element: etree._Element) -> None:
774
755
  vote_count_types = set()
775
- vote_counts_collection = element.find("VoteCountsCollection")
776
- if vote_counts_collection is None:
777
- return
778
- for vote_counts in vote_counts_collection.findall("VoteCounts"):
779
- vote_count_types.add(_get_type_or_other_type(vote_counts))
756
+ for vote_counts in element.findall("VoteCountsCollection/VoteCounts"):
757
+ vote_count_types.add(rule_util.get_type_or_other_type(vote_counts))
780
758
  if "seats-delta" in vote_count_types:
781
759
  raise loggers.ElectionError.from_message(
782
760
  "Legacy VoteCounts type seats-delta is no longer supported as of"
@@ -801,7 +779,7 @@ class VoteCountsNotNegative(base.BaseRule):
801
779
  count = rule_util.get_float(count_element)
802
780
  if count is None:
803
781
  return
804
- vote_count_type = _get_type_or_other_type(element)
782
+ vote_count_type = rule_util.get_type_or_other_type(element)
805
783
  if count < 0 and vote_count_type not in _SEATS_DELTA_VOTE_COUNT_TYPES:
806
784
  raise loggers.ElectionError.from_message(
807
785
  f"VoteCounts of type '{vote_count_type}' has a negative Count of"
@@ -2365,16 +2343,12 @@ class ValidateIncludeInAggregationBallotSelections(base.BaseRule):
2365
2343
  ) -> collections.defaultdict[tuple[str, str], float]:
2366
2344
  """Gathers vote counts from a selection element grouped by type."""
2367
2345
  count_by_type_and_gp_unit = collections.defaultdict(float)
2368
- vote_counts_collection = element.find("VoteCountsCollection")
2369
- if vote_counts_collection is None:
2370
- return count_by_type_and_gp_unit
2371
-
2372
- for vote_counts in vote_counts_collection.findall("VoteCounts"):
2346
+ for vote_counts in element.findall("VoteCountsCollection/VoteCounts"):
2373
2347
  count = rule_util.get_float(vote_counts.find("Count"))
2374
2348
  if count is None:
2375
2349
  continue
2376
2350
 
2377
- vote_count_type = _get_type_or_other_type(vote_counts)
2351
+ vote_count_type = rule_util.get_type_or_other_type(vote_counts)
2378
2352
  gp_unit_id_element = vote_counts.find("GpUnitId")
2379
2353
  gp_unit_id = (
2380
2354
  gp_unit_id_element.text.strip()
@@ -148,6 +148,21 @@ def element_has_text(element: etree._Element | None) -> bool:
148
148
  )
149
149
 
150
150
 
151
+ def get_type_or_other_type(element: etree._Element) -> str:
152
+ """Returns the OtherType text if Type is "other", else the Type text."""
153
+ type_element = element.find("Type")
154
+ other_type_element = element.find("OtherType")
155
+ type_text = (
156
+ type_element.text.strip() if element_has_text(type_element) else ""
157
+ )
158
+ other_type_text = (
159
+ other_type_element.text.strip()
160
+ if element_has_text(other_type_element)
161
+ else ""
162
+ )
163
+ return other_type_text if type_text == "other" else type_text
164
+
165
+
151
166
  def get_float(element: etree._Element | None) -> float | None:
152
167
  """Returns the element's text as a float, or None if it isn't one."""
153
168
  if not element_has_text(element):
@@ -5,4 +5,4 @@ No dependencies should be added to this module.
5
5
  See https://packaging.python.org/guides/single-sourcing-package-version/
6
6
  """
7
7
 
8
- __version__ = '1.69.dev18'
8
+ __version__ = '1.69.dev19'