civics-cdf-validator 1.69.dev21__tar.gz → 1.69.dev23__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.dev21/civics_cdf_validator.egg-info → civics_cdf_validator-1.69.dev23}/PKG-INFO +1 -1
  2. {civics_cdf_validator-1.69.dev21 → civics_cdf_validator-1.69.dev23/civics_cdf_validator.egg-info}/PKG-INFO +1 -1
  3. {civics_cdf_validator-1.69.dev21 → civics_cdf_validator-1.69.dev23}/rulesets/election_results_rules.py +108 -0
  4. {civics_cdf_validator-1.69.dev21 → civics_cdf_validator-1.69.dev23}/rulesets/election_rules.py +1 -6
  5. {civics_cdf_validator-1.69.dev21 → civics_cdf_validator-1.69.dev23}/rulesets/rule_util.py +10 -0
  6. {civics_cdf_validator-1.69.dev21 → civics_cdf_validator-1.69.dev23}/version.py +1 -1
  7. {civics_cdf_validator-1.69.dev21 → civics_cdf_validator-1.69.dev23}/CONTRIBUTING.md +0 -0
  8. {civics_cdf_validator-1.69.dev21 → civics_cdf_validator-1.69.dev23}/LICENSE-2.0.txt +0 -0
  9. {civics_cdf_validator-1.69.dev21 → civics_cdf_validator-1.69.dev23}/MANIFEST.in +0 -0
  10. {civics_cdf_validator-1.69.dev21 → civics_cdf_validator-1.69.dev23}/README.md +0 -0
  11. {civics_cdf_validator-1.69.dev21 → civics_cdf_validator-1.69.dev23}/__init__.py +0 -0
  12. {civics_cdf_validator-1.69.dev21 → civics_cdf_validator-1.69.dev23}/base.py +0 -0
  13. {civics_cdf_validator-1.69.dev21 → civics_cdf_validator-1.69.dev23}/civics_cdf_validator.egg-info/SOURCES.txt +0 -0
  14. {civics_cdf_validator-1.69.dev21 → civics_cdf_validator-1.69.dev23}/civics_cdf_validator.egg-info/dependency_links.txt +0 -0
  15. {civics_cdf_validator-1.69.dev21 → civics_cdf_validator-1.69.dev23}/civics_cdf_validator.egg-info/entry_points.txt +0 -0
  16. {civics_cdf_validator-1.69.dev21 → civics_cdf_validator-1.69.dev23}/civics_cdf_validator.egg-info/requires.txt +0 -0
  17. {civics_cdf_validator-1.69.dev21 → civics_cdf_validator-1.69.dev23}/civics_cdf_validator.egg-info/top_level.txt +0 -0
  18. {civics_cdf_validator-1.69.dev21 → civics_cdf_validator-1.69.dev23}/gpunit_rules.py +0 -0
  19. {civics_cdf_validator-1.69.dev21 → civics_cdf_validator-1.69.dev23}/loggers.py +0 -0
  20. {civics_cdf_validator-1.69.dev21 → civics_cdf_validator-1.69.dev23}/office_utils.py +0 -0
  21. {civics_cdf_validator-1.69.dev21 → civics_cdf_validator-1.69.dev23}/rules.py +0 -0
  22. {civics_cdf_validator-1.69.dev21 → civics_cdf_validator-1.69.dev23}/rulesets/__init__.py +0 -0
  23. {civics_cdf_validator-1.69.dev21 → civics_cdf_validator-1.69.dev23}/rulesets/committee_rules.py +0 -0
  24. {civics_cdf_validator-1.69.dev21 → civics_cdf_validator-1.69.dev23}/rulesets/common_rules.py +0 -0
  25. {civics_cdf_validator-1.69.dev21 → civics_cdf_validator-1.69.dev23}/rulesets/election_dates_rules.py +0 -0
  26. {civics_cdf_validator-1.69.dev21 → civics_cdf_validator-1.69.dev23}/rulesets/metadata_rules.py +0 -0
  27. {civics_cdf_validator-1.69.dev21 → civics_cdf_validator-1.69.dev23}/rulesets/officeholder_rules.py +0 -0
  28. {civics_cdf_validator-1.69.dev21 → civics_cdf_validator-1.69.dev23}/rulesets/voter_information_rules.py +0 -0
  29. {civics_cdf_validator-1.69.dev21 → civics_cdf_validator-1.69.dev23}/setup.cfg +0 -0
  30. {civics_cdf_validator-1.69.dev21 → civics_cdf_validator-1.69.dev23}/setup.py +0 -0
  31. {civics_cdf_validator-1.69.dev21 → civics_cdf_validator-1.69.dev23}/stats.py +0 -0
  32. {civics_cdf_validator-1.69.dev21 → civics_cdf_validator-1.69.dev23}/validator.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: civics_cdf_validator
3
- Version: 1.69.dev21
3
+ Version: 1.69.dev23
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.dev21
3
+ Version: 1.69.dev23
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
@@ -203,12 +203,120 @@ class PartySelectionSeatCountsRequired(base.SchemaRule):
203
203
  )
204
204
 
205
205
 
206
+ class ValidateSeatsNoElection(base.SchemaRule):
207
+ """Checks that party selections report seats-no-election only when needed.
208
+
209
+ When a PartyContest's NumberElected differs from its TotalSeats, some seats
210
+ aren't up for election, so every PartySelection in it, and its
211
+ AggregateBallotSelection, must report seats-no-election VoteCounts. When the
212
+ two are equal, none of them may report them.
213
+ """
214
+
215
+ def setup(self) -> None:
216
+ self.party_contest_types = {
217
+ "PartyContest",
218
+ *self.get_all_subtypes("PartyContest"),
219
+ }
220
+
221
+ def elements(self) -> list[str]:
222
+ return [
223
+ "AggregateBallotSelection",
224
+ "PartySelection",
225
+ *self.get_all_subtypes("PartySelection"),
226
+ ]
227
+
228
+ def check(self, element: etree._Element) -> None:
229
+ contest = element.getparent()
230
+ if self.get_element_class(contest) not in self.party_contest_types:
231
+ return
232
+ number_elected = rule_util.get_integer(contest.find("NumberElected"))
233
+ total_seats = rule_util.get_integer(contest.find("TotalSeats"))
234
+ if number_elected is None or total_seats is None:
235
+ return
236
+ selection_name = (
237
+ f"{self.get_element_class(element)} {element.get('objectId')}"
238
+ )
239
+ contest_id = contest.get("objectId")
240
+ has_seats_no_election = "seats-no-election" in _get_vote_count_types(
241
+ element
242
+ )
243
+ if number_elected != total_seats and not has_seats_no_election:
244
+ raise loggers.ElectionError.from_message(
245
+ f"{selection_name} is missing VoteCounts of type"
246
+ " seats-no-election, which is required because PartyContest"
247
+ f" {contest_id} has a NumberElected ({number_elected}) different"
248
+ f" from its TotalSeats ({total_seats}).",
249
+ [element],
250
+ )
251
+ if number_elected == total_seats and has_seats_no_election:
252
+ raise loggers.ElectionError.from_message(
253
+ f"{selection_name} has VoteCounts of type"
254
+ f" seats-no-election, but PartyContest {contest_id} has a"
255
+ f" NumberElected equal to its TotalSeats ({total_seats}).",
256
+ [element],
257
+ )
258
+
259
+
260
+ class BlankNullAndNoneOfTheAboveTotalsRequired(base.BaseRule):
261
+ """Checks the vote totals of blank, null, and none of the above selections.
262
+
263
+ Election results feeds must report total VoteCounts for every
264
+ BlankBallotSelection, NullBallotSelection, and NoneOfTheAboveBallotSelection.
265
+ Those selections must also report total-percent VoteCounts when
266
+ CountedInTotal is true, and must not report them when it is false.
267
+ """
268
+
269
+ def elements(self) -> list[str]:
270
+ return [
271
+ "BlankBallotSelection",
272
+ "NoneOfTheAboveBallotSelection",
273
+ "NullBallotSelection",
274
+ ]
275
+
276
+ def check(self, element: etree._Element) -> None:
277
+ selection_name = f"{element.tag} {element.get('objectId')}"
278
+ vote_count_types = _get_vote_count_types(element)
279
+ error_log = []
280
+ if "total" not in vote_count_types:
281
+ error_log.append(
282
+ loggers.LogEntry(
283
+ f"{selection_name} is missing VoteCounts of type total.",
284
+ [element],
285
+ )
286
+ )
287
+ counted_in_total = element.find("CountedInTotal")
288
+ if rule_util.element_has_text(counted_in_total):
289
+ has_total_percent = "total-percent" in vote_count_types
290
+ if rule_util.is_xml_true(counted_in_total):
291
+ if not has_total_percent:
292
+ error_log.append(
293
+ loggers.LogEntry(
294
+ f"{selection_name} is missing VoteCounts of type"
295
+ " total-percent, which is required when CountedInTotal is"
296
+ " true.",
297
+ [element],
298
+ )
299
+ )
300
+ elif has_total_percent:
301
+ error_log.append(
302
+ loggers.LogEntry(
303
+ f"{selection_name} has VoteCounts of type total-percent, which"
304
+ " are not allowed when CountedInTotal is false.",
305
+ [element],
306
+ )
307
+ )
308
+ if error_log:
309
+ raise loggers.ElectionError(error_log)
310
+
311
+
206
312
  ELECTION_RESULTS_RULES = election_rules.ELECTION_RULES + (
207
313
  # go/keep-sorted start
314
+ BlankNullAndNoneOfTheAboveTotalsRequired,
208
315
  CandidateAndBallotMeasureSelectionTotalsRequired,
209
316
  DataSourcesRequired,
210
317
  PartySelectionSeatCountsRequired,
211
318
  RequiredContestResultsFieldsSet,
212
319
  ValidatePercentReporting,
320
+ ValidateSeatsNoElection,
213
321
  # go/keep-sorted end
214
322
  )
@@ -1336,12 +1336,7 @@ class WinnerCountLimit(base.BaseRule):
1336
1336
  return ["CandidateContest"]
1337
1337
 
1338
1338
  def check(self, element: etree._Element) -> None:
1339
- number_elected_element = element.find("NumberElected")
1340
- number_elected = (
1341
- int(number_elected_element.text)
1342
- if rule_util.element_has_text(number_elected_element)
1343
- else 1
1344
- )
1339
+ number_elected = rule_util.get_integer(element.find("NumberElected")) or 1
1345
1340
 
1346
1341
  candidate_ids_elements = element.findall("BallotSelection//CandidateIds")
1347
1342
  candidate_ids = set()
@@ -163,6 +163,16 @@ def get_type_or_other_type(element: etree._Element) -> str:
163
163
  return other_type_text if type_text == "other" else type_text
164
164
 
165
165
 
166
+ def get_integer(element: etree._Element | None) -> int | None:
167
+ """Returns the element's text as an integer, or None if it isn't one."""
168
+ if not element_has_text(element):
169
+ return None
170
+ try:
171
+ return int(element.text.strip())
172
+ except ValueError:
173
+ return None
174
+
175
+
166
176
  def get_float(element: etree._Element | None) -> float | None:
167
177
  """Returns the element's text as a float, or None if it isn't one."""
168
178
  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.dev21'
8
+ __version__ = '1.69.dev23'