civics-cdf-validator 1.69.dev17__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.dev17/civics_cdf_validator.egg-info → civics_cdf_validator-1.69.dev19}/PKG-INFO +1 -1
  2. {civics_cdf_validator-1.69.dev17 → civics_cdf_validator-1.69.dev19/civics_cdf_validator.egg-info}/PKG-INFO +1 -1
  3. {civics_cdf_validator-1.69.dev17 → civics_cdf_validator-1.69.dev19}/rulesets/election_results_rules.py +59 -0
  4. {civics_cdf_validator-1.69.dev17 → civics_cdf_validator-1.69.dev19}/rulesets/election_rules.py +39 -30
  5. {civics_cdf_validator-1.69.dev17 → civics_cdf_validator-1.69.dev19}/rulesets/rule_util.py +25 -0
  6. {civics_cdf_validator-1.69.dev17 → civics_cdf_validator-1.69.dev19}/version.py +1 -1
  7. {civics_cdf_validator-1.69.dev17 → civics_cdf_validator-1.69.dev19}/CONTRIBUTING.md +0 -0
  8. {civics_cdf_validator-1.69.dev17 → civics_cdf_validator-1.69.dev19}/LICENSE-2.0.txt +0 -0
  9. {civics_cdf_validator-1.69.dev17 → civics_cdf_validator-1.69.dev19}/MANIFEST.in +0 -0
  10. {civics_cdf_validator-1.69.dev17 → civics_cdf_validator-1.69.dev19}/README.md +0 -0
  11. {civics_cdf_validator-1.69.dev17 → civics_cdf_validator-1.69.dev19}/__init__.py +0 -0
  12. {civics_cdf_validator-1.69.dev17 → civics_cdf_validator-1.69.dev19}/base.py +0 -0
  13. {civics_cdf_validator-1.69.dev17 → civics_cdf_validator-1.69.dev19}/civics_cdf_validator.egg-info/SOURCES.txt +0 -0
  14. {civics_cdf_validator-1.69.dev17 → civics_cdf_validator-1.69.dev19}/civics_cdf_validator.egg-info/dependency_links.txt +0 -0
  15. {civics_cdf_validator-1.69.dev17 → civics_cdf_validator-1.69.dev19}/civics_cdf_validator.egg-info/entry_points.txt +0 -0
  16. {civics_cdf_validator-1.69.dev17 → civics_cdf_validator-1.69.dev19}/civics_cdf_validator.egg-info/requires.txt +0 -0
  17. {civics_cdf_validator-1.69.dev17 → civics_cdf_validator-1.69.dev19}/civics_cdf_validator.egg-info/top_level.txt +0 -0
  18. {civics_cdf_validator-1.69.dev17 → civics_cdf_validator-1.69.dev19}/gpunit_rules.py +0 -0
  19. {civics_cdf_validator-1.69.dev17 → civics_cdf_validator-1.69.dev19}/loggers.py +0 -0
  20. {civics_cdf_validator-1.69.dev17 → civics_cdf_validator-1.69.dev19}/office_utils.py +0 -0
  21. {civics_cdf_validator-1.69.dev17 → civics_cdf_validator-1.69.dev19}/rules.py +0 -0
  22. {civics_cdf_validator-1.69.dev17 → civics_cdf_validator-1.69.dev19}/rulesets/__init__.py +0 -0
  23. {civics_cdf_validator-1.69.dev17 → civics_cdf_validator-1.69.dev19}/rulesets/committee_rules.py +0 -0
  24. {civics_cdf_validator-1.69.dev17 → civics_cdf_validator-1.69.dev19}/rulesets/common_rules.py +0 -0
  25. {civics_cdf_validator-1.69.dev17 → civics_cdf_validator-1.69.dev19}/rulesets/election_dates_rules.py +0 -0
  26. {civics_cdf_validator-1.69.dev17 → civics_cdf_validator-1.69.dev19}/rulesets/metadata_rules.py +0 -0
  27. {civics_cdf_validator-1.69.dev17 → civics_cdf_validator-1.69.dev19}/rulesets/officeholder_rules.py +0 -0
  28. {civics_cdf_validator-1.69.dev17 → civics_cdf_validator-1.69.dev19}/rulesets/voter_information_rules.py +0 -0
  29. {civics_cdf_validator-1.69.dev17 → civics_cdf_validator-1.69.dev19}/setup.cfg +0 -0
  30. {civics_cdf_validator-1.69.dev17 → civics_cdf_validator-1.69.dev19}/setup.py +0 -0
  31. {civics_cdf_validator-1.69.dev17 → civics_cdf_validator-1.69.dev19}/stats.py +0 -0
  32. {civics_cdf_validator-1.69.dev17 → 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.dev17
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.dev17
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,
@@ -42,21 +42,11 @@ _WINNER_POST_ELECTION_STATUSES = frozenset([
42
42
  ])
43
43
 
44
44
 
45
- def _get_type_or_other_type(element: etree._Element) -> str:
46
- """Returns the OtherType text if Type is "other", else the Type text."""
47
- type_element = element.find("Type")
48
- other_type_element = element.find("OtherType")
49
- type_text = (
50
- type_element.text.strip()
51
- if rule_util.element_has_text(type_element)
52
- else ""
53
- )
54
- other_type_text = (
55
- other_type_element.text.strip()
56
- if rule_util.element_has_text(other_type_element)
57
- else ""
58
- )
59
- return other_type_text if type_text == "other" else type_text
45
+ _SEATS_DELTA_VOTE_COUNT_TYPES = frozenset([
46
+ "seats-delta",
47
+ "seats-delta-institutional",
48
+ "seats-delta-mandate",
49
+ ])
60
50
 
61
51
 
62
52
  class PercentSum(base.SchemaRule):
@@ -90,7 +80,7 @@ class PercentSum(base.SchemaRule):
90
80
  ):
91
81
  other_type = vote_counts.find("OtherType")
92
82
  if other_type is not None and other_type.text == "total-percent":
93
- sum_percents += float(vote_counts.find("Count").text)
83
+ sum_percents += rule_util.get_float(vote_counts.find("Count")) or 0
94
84
  if not PercentSum.fuzzy_equals(
95
85
  sum_percents, 0
96
86
  ) and not PercentSum.fuzzy_equals(sum_percents, 100):
@@ -763,11 +753,8 @@ class VoteCountValidSeatsDeltaTypes(base.SchemaRule):
763
753
 
764
754
  def check(self, element: etree._Element) -> None:
765
755
  vote_count_types = set()
766
- vote_counts_collection = element.find("VoteCountsCollection")
767
- if vote_counts_collection is None:
768
- return
769
- for vote_counts in vote_counts_collection.findall("VoteCounts"):
770
- 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))
771
758
  if "seats-delta" in vote_count_types:
772
759
  raise loggers.ElectionError.from_message(
773
760
  "Legacy VoteCounts type seats-delta is no longer supported as of"
@@ -777,6 +764,32 @@ class VoteCountValidSeatsDeltaTypes(base.SchemaRule):
777
764
  )
778
765
 
779
766
 
767
+ class VoteCountsNotNegative(base.BaseRule):
768
+ """Checks that every VoteCounts has a Count of zero or more.
769
+
770
+ Only seats-delta, seats-delta-mandate, and seats-delta-institutional
771
+ VoteCounts can be negative, since they report a change in the number of seats.
772
+ """
773
+
774
+ def elements(self) -> list[str]:
775
+ return ["VoteCounts"]
776
+
777
+ def check(self, element: etree._Element) -> None:
778
+ count_element = element.find("Count")
779
+ count = rule_util.get_float(count_element)
780
+ if count is None:
781
+ return
782
+ vote_count_type = rule_util.get_type_or_other_type(element)
783
+ if count < 0 and vote_count_type not in _SEATS_DELTA_VOTE_COUNT_TYPES:
784
+ raise loggers.ElectionError.from_message(
785
+ f"VoteCounts of type '{vote_count_type}' has a negative Count of"
786
+ f" {count_element.text.strip()}. Only"
787
+ f" {', '.join(sorted(_SEATS_DELTA_VOTE_COUNT_TYPES))} VoteCounts"
788
+ " can be negative.",
789
+ [element],
790
+ )
791
+
792
+
780
793
  class OfficeHasjurisdictionSameAsElectoralDistrict(base.BaseRule):
781
794
  """In election feeds, office has the electoral district same as jurisdiction."""
782
795
 
@@ -2330,17 +2343,12 @@ class ValidateIncludeInAggregationBallotSelections(base.BaseRule):
2330
2343
  ) -> collections.defaultdict[tuple[str, str], float]:
2331
2344
  """Gathers vote counts from a selection element grouped by type."""
2332
2345
  count_by_type_and_gp_unit = collections.defaultdict(float)
2333
- vote_counts_collection = element.find("VoteCountsCollection")
2334
- if vote_counts_collection is None:
2335
- return count_by_type_and_gp_unit
2336
-
2337
- for vote_counts in vote_counts_collection.findall("VoteCounts"):
2338
- count_element = vote_counts.find("Count")
2339
- if not rule_util.element_has_text(count_element):
2346
+ for vote_counts in element.findall("VoteCountsCollection/VoteCounts"):
2347
+ count = rule_util.get_float(vote_counts.find("Count"))
2348
+ if count is None:
2340
2349
  continue
2341
- count = float(count_element.text)
2342
2350
 
2343
- vote_count_type = _get_type_or_other_type(vote_counts)
2351
+ vote_count_type = rule_util.get_type_or_other_type(vote_counts)
2344
2352
  gp_unit_id_element = vote_counts.find("GpUnitId")
2345
2353
  gp_unit_id = (
2346
2354
  gp_unit_id_element.text.strip()
@@ -2463,6 +2471,7 @@ ELECTION_RULES = common_rules.COMMON_RULES + (
2463
2471
  ValidateSpecialBallotSelectionCountedInTotal,
2464
2472
  VoteCountTypesCoherency,
2465
2473
  VoteCountValidSeatsDeltaTypes,
2474
+ VoteCountsNotNegative,
2466
2475
  WinnerCountLimit,
2467
2476
  # go/keep-sorted end
2468
2477
  )
@@ -148,6 +148,31 @@ 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
+
166
+ def get_float(element: etree._Element | None) -> float | None:
167
+ """Returns the element's text as a float, or None if it isn't one."""
168
+ if not element_has_text(element):
169
+ return None
170
+ try:
171
+ return float(element.text.strip())
172
+ except ValueError:
173
+ return None
174
+
175
+
151
176
  def country_code_is_valid(country_code: str) -> bool:
152
177
  # EU is part of ISO 3166/MA
153
178
  return (
@@ -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.dev17'
8
+ __version__ = '1.69.dev19'