civics-cdf-validator 1.68.dev17__tar.gz → 1.69.dev0__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.68.dev17/civics_cdf_validator.egg-info → civics_cdf_validator-1.69.dev0}/PKG-INFO +1 -1
  2. {civics_cdf_validator-1.68.dev17 → civics_cdf_validator-1.69.dev0/civics_cdf_validator.egg-info}/PKG-INFO +1 -1
  3. {civics_cdf_validator-1.68.dev17 → civics_cdf_validator-1.69.dev0}/rulesets/common_rules.py +105 -85
  4. {civics_cdf_validator-1.68.dev17 → civics_cdf_validator-1.69.dev0}/rulesets/officeholder_rules.py +43 -29
  5. {civics_cdf_validator-1.68.dev17 → civics_cdf_validator-1.69.dev0}/version.py +1 -1
  6. {civics_cdf_validator-1.68.dev17 → civics_cdf_validator-1.69.dev0}/CONTRIBUTING.md +0 -0
  7. {civics_cdf_validator-1.68.dev17 → civics_cdf_validator-1.69.dev0}/LICENSE-2.0.txt +0 -0
  8. {civics_cdf_validator-1.68.dev17 → civics_cdf_validator-1.69.dev0}/MANIFEST.in +0 -0
  9. {civics_cdf_validator-1.68.dev17 → civics_cdf_validator-1.69.dev0}/README.md +0 -0
  10. {civics_cdf_validator-1.68.dev17 → civics_cdf_validator-1.69.dev0}/__init__.py +0 -0
  11. {civics_cdf_validator-1.68.dev17 → civics_cdf_validator-1.69.dev0}/base.py +0 -0
  12. {civics_cdf_validator-1.68.dev17 → civics_cdf_validator-1.69.dev0}/civics_cdf_validator.egg-info/SOURCES.txt +0 -0
  13. {civics_cdf_validator-1.68.dev17 → civics_cdf_validator-1.69.dev0}/civics_cdf_validator.egg-info/dependency_links.txt +0 -0
  14. {civics_cdf_validator-1.68.dev17 → civics_cdf_validator-1.69.dev0}/civics_cdf_validator.egg-info/entry_points.txt +0 -0
  15. {civics_cdf_validator-1.68.dev17 → civics_cdf_validator-1.69.dev0}/civics_cdf_validator.egg-info/requires.txt +0 -0
  16. {civics_cdf_validator-1.68.dev17 → civics_cdf_validator-1.69.dev0}/civics_cdf_validator.egg-info/top_level.txt +0 -0
  17. {civics_cdf_validator-1.68.dev17 → civics_cdf_validator-1.69.dev0}/gpunit_rules.py +0 -0
  18. {civics_cdf_validator-1.68.dev17 → civics_cdf_validator-1.69.dev0}/loggers.py +0 -0
  19. {civics_cdf_validator-1.68.dev17 → civics_cdf_validator-1.69.dev0}/office_utils.py +0 -0
  20. {civics_cdf_validator-1.68.dev17 → civics_cdf_validator-1.69.dev0}/rules.py +0 -0
  21. {civics_cdf_validator-1.68.dev17 → civics_cdf_validator-1.69.dev0}/rulesets/__init__.py +0 -0
  22. {civics_cdf_validator-1.68.dev17 → civics_cdf_validator-1.69.dev0}/rulesets/committee_rules.py +0 -0
  23. {civics_cdf_validator-1.68.dev17 → civics_cdf_validator-1.69.dev0}/rulesets/election_dates_rules.py +0 -0
  24. {civics_cdf_validator-1.68.dev17 → civics_cdf_validator-1.69.dev0}/rulesets/election_results_rules.py +0 -0
  25. {civics_cdf_validator-1.68.dev17 → civics_cdf_validator-1.69.dev0}/rulesets/election_rules.py +0 -0
  26. {civics_cdf_validator-1.68.dev17 → civics_cdf_validator-1.69.dev0}/rulesets/metadata_rules.py +0 -0
  27. {civics_cdf_validator-1.68.dev17 → civics_cdf_validator-1.69.dev0}/rulesets/rule_util.py +0 -0
  28. {civics_cdf_validator-1.68.dev17 → civics_cdf_validator-1.69.dev0}/rulesets/voter_information_rules.py +0 -0
  29. {civics_cdf_validator-1.68.dev17 → civics_cdf_validator-1.69.dev0}/setup.cfg +0 -0
  30. {civics_cdf_validator-1.68.dev17 → civics_cdf_validator-1.69.dev0}/setup.py +0 -0
  31. {civics_cdf_validator-1.68.dev17 → civics_cdf_validator-1.69.dev0}/stats.py +0 -0
  32. {civics_cdf_validator-1.68.dev17 → civics_cdf_validator-1.69.dev0}/validator.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: civics_cdf_validator
3
- Version: 1.68.dev17
3
+ Version: 1.69.dev0
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.68.dev17
3
+ Version: 1.69.dev0
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
@@ -1228,7 +1228,7 @@ class ValidYoutubeURL(base.BaseRule):
1228
1228
  or "hashtag" in parsed_url.path
1229
1229
  ):
1230
1230
  raise loggers.ElectionError.from_message(
1231
- "'{}' is not an expected value for a youtube channel.".format(url),
1231
+ f"'{url}' is not an expected value for a youtube channel.",
1232
1232
  [element],
1233
1233
  )
1234
1234
 
@@ -1239,10 +1239,10 @@ class ValidTiktokURL(base.BaseRule):
1239
1239
  Ensure the provided URL is a valid Tiktok URL types.
1240
1240
  """
1241
1241
 
1242
- def elements(self):
1242
+ def elements(self) -> list[str]:
1243
1243
  return ["Uri"]
1244
1244
 
1245
- def check(self, element):
1245
+ def check(self, element: etree._Element) -> None:
1246
1246
  url = element.text.strip()
1247
1247
  parsed_url = urlparse(url)
1248
1248
  if "tiktok" in parsed_url.netloc and (
@@ -1251,7 +1251,7 @@ class ValidTiktokURL(base.BaseRule):
1251
1251
  or parsed_url.fragment
1252
1252
  ):
1253
1253
  raise loggers.ElectionError.from_message(
1254
- "'{}' is not an expected value for a tiktok account.".format(url),
1254
+ f"'{url}' is not an expected value for a tiktok account.",
1255
1255
  [element],
1256
1256
  )
1257
1257
 
@@ -1263,10 +1263,12 @@ class ValidURIAnnotation(base.BaseRule):
1263
1263
  Throws Warnings and Errors depending on type of invalidity.
1264
1264
  """
1265
1265
 
1266
- def elements(self):
1266
+ def elements(self) -> list[str]:
1267
1267
  return ["ContactInformation"]
1268
1268
 
1269
- def check_url(self, uri, annotation, platform):
1269
+ def check_url(
1270
+ self, uri: etree._Element, annotation: str, platform: str
1271
+ ) -> None:
1270
1272
  url = uri.text.strip()
1271
1273
  parsed_url = urlparse(url)
1272
1274
  # Ensure media platform name is in URL.
@@ -1278,13 +1280,12 @@ class ValidURIAnnotation(base.BaseRule):
1278
1280
  ):
1279
1281
  # Note that the URL is encoded for printing purposes
1280
1282
  raise loggers.ElectionError.from_message(
1281
- "Annotation '{}' is incorrect for URI {}.".format(
1282
- annotation, url.encode("ascii", "ignore")
1283
- ),
1283
+ f"Annotation '{annotation}' is incorrect for URI"
1284
+ f" {url.encode('ascii', 'ignore')}.",
1284
1285
  [uri],
1285
1286
  )
1286
1287
 
1287
- def check(self, element):
1288
+ def check(self, element: etree._Element) -> None:
1288
1289
  uris = element.findall("Uri")
1289
1290
 
1290
1291
  for uri in uris:
@@ -1294,7 +1295,7 @@ class ValidURIAnnotation(base.BaseRule):
1294
1295
 
1295
1296
  if not annotation:
1296
1297
  raise loggers.ElectionWarning.from_message(
1297
- "URI {} is missing annotation.".format(ascii_url), [uri]
1298
+ f"URI {ascii_url} is missing annotation.", [uri]
1298
1299
  )
1299
1300
 
1300
1301
  # Skip platform checks for office contact form and candidate-image
@@ -1309,20 +1310,17 @@ class ValidURIAnnotation(base.BaseRule):
1309
1310
  # without a usage type, which is checked here.
1310
1311
  if platform in _TYPED_URI_PLATFORMS:
1311
1312
  raise loggers.ElectionWarning.from_message(
1312
- "Annotation '{}' missing usage type.".format(annotation), [uri]
1313
+ f"Annotation '{annotation}' missing usage type.", [uri]
1313
1314
  )
1314
1315
  elif platform in _URI_USAGE_TYPES:
1315
1316
  raise loggers.ElectionError.from_message(
1316
- "Annotation '{}' has usage type, missing platform.".format(
1317
- annotation
1318
- ),
1317
+ f"Annotation '{annotation}' has usage type, missing platform.",
1319
1318
  [uri],
1320
1319
  )
1321
1320
  elif platform not in _UNTYPED_URI_PLATFORMS:
1322
1321
  raise loggers.ElectionError.from_message(
1323
- "Annotation '{}' is not a valid annotation for URI {}.".format(
1324
- annotation, ascii_url
1325
- ),
1322
+ f"Annotation '{annotation}' is not a valid annotation for URI"
1323
+ f" {ascii_url}.",
1326
1324
  [uri],
1327
1325
  )
1328
1326
  elif len(ann_elements) == 2:
@@ -1334,14 +1332,12 @@ class ValidURIAnnotation(base.BaseRule):
1334
1332
  or platform not in _TYPED_URI_PLATFORMS
1335
1333
  ):
1336
1334
  raise loggers.ElectionWarning.from_message(
1337
- "'{}' is not a valid annotation.".format(annotation), [uri]
1335
+ f"'{annotation}' is not a valid annotation.", [uri]
1338
1336
  )
1339
1337
  else:
1340
1338
  # More than two implies an invalid annotation.
1341
1339
  raise loggers.ElectionError.from_message(
1342
- "Annotation '{}' is invalid for URI {}.".format(
1343
- annotation, ascii_url
1344
- ),
1340
+ f"Annotation '{annotation}' is invalid for URI {ascii_url}.",
1345
1341
  [uri],
1346
1342
  )
1347
1343
  # Finally, check platform is in the URL.
@@ -1351,10 +1347,10 @@ class ValidURIAnnotation(base.BaseRule):
1351
1347
  class OfficesHaveJurisdictionID(base.BaseRule):
1352
1348
  """Each office must have a jurisdiction-id."""
1353
1349
 
1354
- def elements(self):
1350
+ def elements(self) -> list[str]:
1355
1351
  return ["Office"]
1356
1352
 
1357
- def check(self, element):
1353
+ def check(self, element: etree._Element) -> None:
1358
1354
  jurisdiction_values = []
1359
1355
  post_office_split_jurisdiction_element = element.find("JurisdictionId")
1360
1356
  if rule_util.element_has_text(post_office_split_jurisdiction_element):
@@ -1379,19 +1375,19 @@ class OfficesHaveJurisdictionID(base.BaseRule):
1379
1375
  class ValidJurisdictionID(base.ValidReferenceRule):
1380
1376
  """Each jurisdiction id should refer to a valid GpUnit."""
1381
1377
 
1382
- def __init__(self, *args, **kwargs):
1378
+ def __init__(self, *args, **kwargs) -> None:
1383
1379
  super(ValidJurisdictionID, self).__init__(
1384
1380
  *args, missing_element="GpUnit", **kwargs
1385
1381
  )
1386
1382
 
1387
- def _gather_reference_values(self):
1383
+ def _gather_reference_values(self) -> set[str]:
1388
1384
  root = self.election_tree.getroot()
1389
1385
  jurisdiction_values = rule_util.get_entity_info_for_value_type(
1390
1386
  root, "jurisdiction-id"
1391
1387
  )
1392
1388
  return set(jurisdiction_values)
1393
1389
 
1394
- def _gather_defined_values(self):
1390
+ def _gather_defined_values(self) -> set[str]:
1395
1391
  gp_unit_elements = self.election_tree.getroot().findall(".//GpUnit")
1396
1392
  return {elem.get("objectId") for elem in gp_unit_elements}
1397
1393
 
@@ -1399,10 +1395,10 @@ class ValidJurisdictionID(base.ValidReferenceRule):
1399
1395
  class OfficesHaveValidOfficeLevel(base.BaseRule):
1400
1396
  """Each office must have a valid office-level."""
1401
1397
 
1402
- def elements(self):
1398
+ def elements(self) -> list[str]:
1403
1399
  return ["Office"]
1404
1400
 
1405
- def check(self, element):
1401
+ def check(self, element: etree._Element) -> None:
1406
1402
  office_levels = []
1407
1403
  post_office_split_office_level_element = element.find("Level")
1408
1404
  if rule_util.element_has_text(post_office_split_office_level_element):
@@ -1435,10 +1431,10 @@ class OfficesHaveValidOfficeLevel(base.BaseRule):
1435
1431
  class OfficesHaveValidOfficeRole(base.BaseRule):
1436
1432
  """Each office must have valid office role(s)."""
1437
1433
 
1438
- def elements(self):
1434
+ def elements(self) -> list[str]:
1439
1435
  return ["Office"]
1440
1436
 
1441
- def check(self, element):
1437
+ def check(self, element: etree._Element) -> None:
1442
1438
  office_roles = [role.text for role in element.findall("Role")]
1443
1439
  if not office_roles:
1444
1440
  office_roles = [
@@ -1478,10 +1474,10 @@ class OfficesHaveValidOfficeRole(base.BaseRule):
1478
1474
  class DiplomaticOfficesHaveCorrespondingGpUnitsSet(base.BaseRule):
1479
1475
  """Offices with the 'diplomat' role must have a sending or receiving state."""
1480
1476
 
1481
- def elements(self):
1477
+ def elements(self) -> list[str]:
1482
1478
  return ["Office"]
1483
1479
 
1484
- def check(self, element):
1480
+ def check(self, element: etree._Element) -> None:
1485
1481
  office_roles = (
1486
1482
  role.text.strip()
1487
1483
  for role in element.findall("Role")
@@ -1508,10 +1504,10 @@ class ValidPartyLeadershipDates(base.DateRule):
1508
1504
  End dates should not occur before the start date.
1509
1505
  """
1510
1506
 
1511
- def elements(self):
1507
+ def elements(self) -> list[str]:
1512
1508
  return ["PartyLeadership"]
1513
1509
 
1514
- def check(self, element):
1510
+ def check(self, element: etree._Element) -> None:
1515
1511
  self.reset_instance_vars()
1516
1512
  self.gather_dates(element)
1517
1513
  self.check_end_after_start()
@@ -1525,7 +1521,7 @@ class GpUnitsHaveInternationalizedName(base.BaseRule):
1525
1521
  def elements(self):
1526
1522
  return ["GpUnit"]
1527
1523
 
1528
- def check(self, element):
1524
+ def check(self, element: etree._Element) -> None:
1529
1525
  intl_names = element.findall("InternationalizedName")
1530
1526
  if intl_names is None or not intl_names or len(intl_names) > 1:
1531
1527
  raise loggers.ElectionError.from_message(
@@ -1564,10 +1560,10 @@ class MissingFieldsError(base.MissingFieldRule):
1564
1560
  and field in element_field_mapping.
1565
1561
  """
1566
1562
 
1567
- def get_severity(self):
1563
+ def get_severity(self) -> int:
1568
1564
  return 2
1569
1565
 
1570
- def element_field_mapping(self):
1566
+ def element_field_mapping(self) -> dict[str, list[str]]:
1571
1567
  return {
1572
1568
  "Person": [
1573
1569
  "FullName//Text",
@@ -1592,10 +1588,10 @@ class MissingFieldsWarning(base.MissingFieldRule):
1592
1588
  and field in element_field_mapping.
1593
1589
  """
1594
1590
 
1595
- def get_severity(self):
1591
+ def get_severity(self) -> int:
1596
1592
  return 1
1597
1593
 
1598
- def element_field_mapping(self):
1594
+ def element_field_mapping(self) -> dict[str, list[str]]:
1599
1595
  return {
1600
1596
  "Candidate": [
1601
1597
  "PartyId",
@@ -1610,10 +1606,10 @@ class MissingFieldsInfo(base.MissingFieldRule):
1610
1606
  and field in element_field_mapping.
1611
1607
  """
1612
1608
 
1613
- def get_severity(self):
1609
+ def get_severity(self) -> int:
1614
1610
  return 0
1615
1611
 
1616
- def element_field_mapping(self):
1612
+ def element_field_mapping(self) -> dict[str, list[str]]:
1617
1613
  return {}
1618
1614
 
1619
1615
 
@@ -1624,7 +1620,12 @@ class PartySpanMultipleCountries(base.BaseRule):
1624
1620
  sometimes correct, but we should flag it to double check.
1625
1621
  """
1626
1622
 
1627
- def __init__(self, election_tree, schema_tree=None, **kwargs):
1623
+ def __init__(
1624
+ self,
1625
+ election_tree: etree._ElementTree,
1626
+ schema_tree: etree._ElementTree | None = None,
1627
+ **kwargs,
1628
+ ) -> None:
1628
1629
  super(PartySpanMultipleCountries, self).__init__(
1629
1630
  election_tree, schema_tree, **kwargs
1630
1631
  )
@@ -1638,10 +1639,10 @@ class PartySpanMultipleCountries(base.BaseRule):
1638
1639
  country = country_match[0].split("/")[1]
1639
1640
  self.existing_gpunits[gpunit.get("objectId")] = country
1640
1641
 
1641
- def elements(self):
1642
+ def elements(self) -> list[str]:
1642
1643
  return ["PartyScopeGpUnitIds"]
1643
1644
 
1644
- def check(self, element):
1645
+ def check(self, element: etree._Element) -> None:
1645
1646
  if element.text is None:
1646
1647
  return
1647
1648
  referenced_country = dict()
@@ -1653,15 +1654,14 @@ class PartySpanMultipleCountries(base.BaseRule):
1653
1654
  referenced_country[country].append(gpunit_id)
1654
1655
 
1655
1656
  if len(referenced_country) > 1:
1656
- gpunit_country_mapping = " / ".join([
1657
- "%s -> %s" % (key, str(value))
1658
- for (key, value) in referenced_country.items()
1659
- ])
1657
+ gpunit_country_mapping = " / ".join(
1658
+ [f"{key} -> {value}" for (key, value) in referenced_country.items()]
1659
+ )
1660
1660
 
1661
1661
  raise loggers.ElectionWarning.from_message(
1662
1662
  (
1663
1663
  "PartyScopeGpUnitIds refer to GpUnit from different countries:"
1664
- " {}. Please double check.".format(gpunit_country_mapping)
1664
+ f" {gpunit_country_mapping}. Please double check."
1665
1665
  ),
1666
1666
  [element],
1667
1667
  )
@@ -1670,7 +1670,12 @@ class PartySpanMultipleCountries(base.BaseRule):
1670
1670
  class NonExecutiveOfficeShouldHaveGovernmentBody(base.BaseRule):
1671
1671
  """Ensure non-Head of Government/State Office elements have a government body defined."""
1672
1672
 
1673
- def __init__(self, election_tree, schema_tree=None, **kwargs):
1673
+ def __init__(
1674
+ self,
1675
+ election_tree: etree._ElementTree,
1676
+ schema_tree: etree._ElementTree | None = None,
1677
+ **kwargs,
1678
+ ) -> None:
1674
1679
  self.is_post_office_split_feed = False
1675
1680
  officeholder_tenure_collection_element = self.get_elements_by_class(
1676
1681
  election_tree, "OfficeHolderTenureCollection"
@@ -1679,10 +1684,10 @@ class NonExecutiveOfficeShouldHaveGovernmentBody(base.BaseRule):
1679
1684
  if officeholder_tenure_collection_element or role_element:
1680
1685
  self.is_post_office_split_feed = True
1681
1686
 
1682
- def elements(self):
1687
+ def elements(self) -> list[str]:
1683
1688
  return ["Office"]
1684
1689
 
1685
- def check(self, element):
1690
+ def check(self, element: etree._Element) -> None:
1686
1691
  if not _is_executive_office(
1687
1692
  element, self.is_post_office_split_feed
1688
1693
  ) and not _has_government_body(element):
@@ -1696,7 +1701,12 @@ class NonExecutiveOfficeShouldHaveGovernmentBody(base.BaseRule):
1696
1701
  class ExecutiveOfficeShouldNotHaveGovernmentBody(base.BaseRule):
1697
1702
  """Ensure Head of Government/State Office elements do not have a government body defined."""
1698
1703
 
1699
- def __init__(self, election_tree, schema_tree=None, **kwargs):
1704
+ def __init__(
1705
+ self,
1706
+ election_tree: etree._ElementTree,
1707
+ schema_tree: etree._ElementTree | None = None,
1708
+ **kwargs,
1709
+ ) -> None:
1700
1710
  self.is_post_office_split_feed = False
1701
1711
  officeholder_tenure_collection_element = self.get_elements_by_class(
1702
1712
  election_tree, "OfficeHolderTenureCollection"
@@ -1705,10 +1715,10 @@ class ExecutiveOfficeShouldNotHaveGovernmentBody(base.BaseRule):
1705
1715
  if officeholder_tenure_collection_element or role_element:
1706
1716
  self.is_post_office_split_feed = True
1707
1717
 
1708
- def elements(self):
1718
+ def elements(self) -> list[str]:
1709
1719
  return ["Office"]
1710
1720
 
1711
- def check(self, element):
1721
+ def check(self, element: etree._Element) -> None:
1712
1722
  if _is_executive_office(
1713
1723
  element, self.is_post_office_split_feed
1714
1724
  ) and _has_government_body(element):
@@ -1733,10 +1743,10 @@ class MissingOfficeSelectionMethod(base.BaseRule):
1733
1743
  -ex-officio
1734
1744
  """
1735
1745
 
1736
- def elements(self):
1746
+ def elements(self) -> list[str]:
1737
1747
  return ["Office"]
1738
1748
 
1739
- def check(self, element):
1749
+ def check(self, element: etree._Element) -> None:
1740
1750
  selection = element.find("SelectionMethod")
1741
1751
  if selection is None:
1742
1752
  raise loggers.ElectionWarning.from_message(
@@ -1747,10 +1757,10 @@ class MissingOfficeSelectionMethod(base.BaseRule):
1747
1757
  class AllInternationalizedTextHaveEnVersion(base.SchemaRule):
1748
1758
  """Checks for InternationalizedText elements missing an English version."""
1749
1759
 
1750
- def elements(self):
1760
+ def elements(self) -> list[str]:
1751
1761
  return list(self.get_element_names_for_type("InternationalizedText"))
1752
1762
 
1753
- def check(self, element):
1763
+ def check(self, element: etree._Element) -> None:
1754
1764
  language_map = rule_util.get_language_to_text_map(element)
1755
1765
  if "en" not in language_map:
1756
1766
  raise loggers.ElectionInfo.from_message(
@@ -1762,10 +1772,10 @@ class AllInternationalizedTextHaveEnVersion(base.SchemaRule):
1762
1772
  class MultipleInternationalizedTextWithSameLanguageCode(base.BaseRule):
1763
1773
  """Checks for multiple InternationalizedText with the same language code."""
1764
1774
 
1765
- def elements(self):
1775
+ def elements(self) -> list[str]:
1766
1776
  return _INTERNATIONALIZED_TEXT_ELEMENTS_WITH_ONLY_ONE_TEXT_PER_LANGUAGE
1767
1777
 
1768
- def check(self, element):
1778
+ def check(self, element: etree._Element) -> None:
1769
1779
  language_map = rule_util.get_language_to_text_map(element)
1770
1780
 
1771
1781
  for language, texts in language_map.items():
@@ -1782,7 +1792,7 @@ class DeprecatedPartyLeadershipSchema(base.BaseRule):
1782
1792
  def elements(self):
1783
1793
  return ["Party"]
1784
1794
 
1785
- def check(self, element):
1795
+ def check(self, element: etree._Element) -> None:
1786
1796
  if len(rule_util.get_external_id_values(element, "party-leader-id")) or len(
1787
1797
  rule_util.get_external_id_values(element, "party-chair-id")
1788
1798
  ):
@@ -1804,7 +1814,7 @@ _IDREF_EXTERNAL_IDENTIFIERS = frozenset(
1804
1814
  class ProhibitElectionData(base.TreeRule):
1805
1815
  """Ensure that election data is not provided for officeholder feeds."""
1806
1816
 
1807
- def check(self):
1817
+ def check(self) -> None:
1808
1818
  root = self.election_tree.getroot()
1809
1819
  if root is not None and root.find("Election") is not None:
1810
1820
  raise loggers.ElectionError.from_message(
@@ -1826,12 +1836,12 @@ class UnreferencedEntitiesBase(base.TreeRule, base.SchemaRule):
1826
1836
 
1827
1837
  def __init__(
1828
1838
  self,
1829
- election_tree,
1830
- schema_tree,
1831
- top_level_entity_types,
1832
- warned_entity_types,
1839
+ election_tree: etree._ElementTree,
1840
+ schema_tree: etree._ElementTree,
1841
+ top_level_entity_types: Collection[str],
1842
+ warned_entity_types: Collection[str],
1833
1843
  **kwargs,
1834
- ):
1844
+ ) -> None:
1835
1845
  super(UnreferencedEntitiesBase, self).__init__(
1836
1846
  election_tree, schema_tree, **kwargs
1837
1847
  )
@@ -1839,7 +1849,7 @@ class UnreferencedEntitiesBase(base.TreeRule, base.SchemaRule):
1839
1849
  self.top_level_entity_types = top_level_entity_types
1840
1850
  self.warned_entity_types = warned_entity_types
1841
1851
 
1842
- def _gather_referenced_entities(self):
1852
+ def _gather_referenced_entities(self) -> set[str]:
1843
1853
  """Create a set of all entities referenced by either IDREF(S) elements or external identifiers."""
1844
1854
  referenced_entities = set()
1845
1855
  for external_id_type in _IDREF_EXTERNAL_IDENTIFIERS:
@@ -1853,7 +1863,7 @@ class UnreferencedEntitiesBase(base.TreeRule, base.SchemaRule):
1853
1863
  referenced_entities.update(element.text.split())
1854
1864
  return referenced_entities
1855
1865
 
1856
- def check(self):
1866
+ def check(self) -> None:
1857
1867
  for _, element in etree.iterwalk(self.election_tree):
1858
1868
  element_name = element.tag
1859
1869
  # Skip anything without an object id.
@@ -1893,10 +1903,10 @@ class UnreferencedEntitiesBase(base.TreeRule, base.SchemaRule):
1893
1903
  class CoalitionParties(base.BaseRule):
1894
1904
  """Coalitions should always define the Party IDs."""
1895
1905
 
1896
- def elements(self):
1906
+ def elements(self) -> list[str]:
1897
1907
  return ["Coalition"]
1898
1908
 
1899
- def check(self, element):
1909
+ def check(self, element: etree._Element) -> None:
1900
1910
  party_id = element.find("PartyIds")
1901
1911
  if party_id is None or not party_id.text or not party_id.text.strip():
1902
1912
  raise loggers.ElectionError.from_message(
@@ -1911,14 +1921,16 @@ class ValidatePartyCollection(base.BaseRule):
1911
1921
  contains basic checks.
1912
1922
  """
1913
1923
 
1914
- def elements(self):
1924
+ def elements(self) -> list[str]:
1915
1925
  return ["PartyCollection"]
1916
1926
 
1917
- def check_specific(self, parties):
1927
+ def check_specific(
1928
+ self, parties: Iterable[etree._Element]
1929
+ ) -> list[loggers.LogEntry]:
1918
1930
  """Return a list of info log to be raised."""
1919
1931
  raise NotImplementedError
1920
1932
 
1921
- def check(self, element):
1933
+ def check(self, element: etree._Element) -> None:
1922
1934
  info_log = []
1923
1935
  parties = element.findall("Party")
1924
1936
  if len(parties) < 1:
@@ -1936,7 +1948,9 @@ class DuplicatedPartyAbbreviation(ValidatePartyCollection):
1936
1948
  within this class and returned to the user as an Info message.
1937
1949
  """
1938
1950
 
1939
- def check_specific(self, parties):
1951
+ def check_specific(
1952
+ self, parties: Iterable[etree._Element]
1953
+ ) -> list[loggers.LogEntry]:
1940
1954
  info_log = []
1941
1955
  party_abbreviations_by_language = {}
1942
1956
  for party in parties:
@@ -1978,7 +1992,9 @@ class DuplicatedPartyName(ValidatePartyCollection):
1978
1992
  within this class and returned to the user as an Info message.
1979
1993
  """
1980
1994
 
1981
- def check_specific(self, parties):
1995
+ def check_specific(
1996
+ self, parties: Iterable[etree._Element]
1997
+ ) -> list[loggers.LogEntry]:
1982
1998
  info_log = []
1983
1999
  party_names_by_language = {}
1984
2000
  for party in parties:
@@ -2017,7 +2033,9 @@ class MissingPartyAbbreviationTranslation(ValidatePartyCollection):
2017
2033
  class and returned to the user as an Info message.
2018
2034
  """
2019
2035
 
2020
- def check_specific(self, parties):
2036
+ def check_specific(
2037
+ self, parties: Iterable[etree._Element]
2038
+ ) -> list[loggers.LogEntry]:
2021
2039
  info_log = []
2022
2040
  feed_languages = set()
2023
2041
  feed_party_ids = set()
@@ -2067,7 +2085,9 @@ class MissingPartyNameTranslation(ValidatePartyCollection):
2067
2085
  an Info message.
2068
2086
  """
2069
2087
 
2070
- def check_specific(self, parties):
2088
+ def check_specific(
2089
+ self, parties: Iterable[etree._Element]
2090
+ ) -> list[loggers.LogEntry]:
2071
2091
  info_log = []
2072
2092
  feed_languages = set()
2073
2093
  feed_party_ids = set()
@@ -2109,10 +2129,10 @@ class MissingPartyNameTranslation(ValidatePartyCollection):
2109
2129
  class DateOfBirthIsInPast(base.DateRule):
2110
2130
  """Date of Birth should not be in the future."""
2111
2131
 
2112
- def elements(self):
2132
+ def elements(self) -> list[str]:
2113
2133
  return ["Person"]
2114
2134
 
2115
- def check(self, element):
2135
+ def check(self, element: etree._Element) -> None:
2116
2136
  self.reset_instance_vars()
2117
2137
  date_of_birth_element = element.find("DateOfBirth")
2118
2138
  if date_of_birth_element is not None and date_of_birth_element.text:
@@ -2127,11 +2147,11 @@ class DateOfBirthIsInPast(base.DateRule):
2127
2147
  class GpUnitsHaveSingleRoot(base.TreeRule):
2128
2148
  """Ensure that GpUnits form a single-rooted tree."""
2129
2149
 
2130
- def __init__(self, *args, **kwargs):
2150
+ def __init__(self, *args, **kwargs) -> None:
2131
2151
  super(GpUnitsHaveSingleRoot, self).__init__(*args, **kwargs)
2132
2152
  self.error_log = []
2133
2153
 
2134
- def check(self):
2154
+ def check(self) -> None:
2135
2155
  # Make sure there's at most one GpUnit as a root.
2136
2156
  # The root is defined as having ComposingGpUnitIds but
2137
2157
  # is not in the ComposingGpUnitIds of any other GpUnit.
@@ -2175,7 +2195,7 @@ class GpUnitsHaveSingleRoot(base.TreeRule):
2175
2195
  if self.error_log:
2176
2196
  raise loggers.ElectionError(self.error_log)
2177
2197
 
2178
- def _get_gpunit_collection(self):
2198
+ def _get_gpunit_collection(self) -> etree._Element:
2179
2199
  """Returns the GpUnitCollection element, or the root if there is none."""
2180
2200
  root = self.election_tree.getroot()
2181
2201
  gpunit_collection = root.find("GpUnitCollection")
@@ -15,19 +15,22 @@
15
15
 
16
16
  from __future__ import print_function
17
17
 
18
+ from collections.abc import Iterable
18
19
  import datetime
19
20
  import hashlib
21
+ from typing import Any
20
22
 
21
23
  from civics_cdf_validator import base
22
24
  from civics_cdf_validator import loggers
23
25
  from civics_cdf_validator.rulesets import common_rules
24
26
  from civics_cdf_validator.rulesets import rule_util
27
+ from lxml import etree
25
28
 
26
29
 
27
30
  class PersonHasOffice(base.ValidReferenceRule):
28
31
  """Ensure that each non-party-leader Person object linked to one Office."""
29
32
 
30
- def _gather_reference_values(self):
33
+ def _gather_reference_values(self) -> set[str]:
31
34
  root = self.election_tree.getroot()
32
35
 
33
36
  person_ids = set()
@@ -36,7 +39,7 @@ class PersonHasOffice(base.ValidReferenceRule):
36
39
  person_ids = {p.attrib["objectId"] for p in person_collection}
37
40
  return person_ids
38
41
 
39
- def _gather_defined_values(self):
42
+ def _gather_defined_values(self) -> set[str]:
40
43
  root = self.election_tree.getroot()
41
44
 
42
45
  person_reference_ids = set()
@@ -72,9 +75,7 @@ class PersonHasOffice(base.ValidReferenceRule):
72
75
  if id_obj is not None and id_obj.text:
73
76
  ids = id_obj.text.strip().split()
74
77
  if len(ids) > 1:
75
- msg = "Office has {} OfficeHolders. Must have exactly one.".format(
76
- str(len(ids))
77
- )
78
+ msg = f"Office has {len(ids)} OfficeHolders. Must have exactly one."
78
79
  raise loggers.ElectionError.from_message(msg, [office])
79
80
  person_reference_ids.update(ids)
80
81
 
@@ -89,7 +90,12 @@ class OfficeSelectionMethodMatch(base.BaseRule):
89
90
  present in the list of SelectionMethods of the Office it points to.
90
91
  """
91
92
 
92
- def __init__(self, election_tree, schema_tree=None, **kwargs):
93
+ def __init__(
94
+ self,
95
+ election_tree: etree._ElementTree,
96
+ schema_tree: etree._ElementTree | None = None,
97
+ **kwargs,
98
+ ) -> None:
93
99
  self.office_selection_methods = {}
94
100
  officeholder_tenure_elements = self.get_elements_by_class(
95
101
  election_tree, "OfficeHolderTenure"
@@ -104,10 +110,10 @@ class OfficeSelectionMethodMatch(base.BaseRule):
104
110
  }
105
111
  self.office_selection_methods[office_id] = selection_methods
106
112
 
107
- def elements(self):
113
+ def elements(self) -> list[str]:
108
114
  return ["OfficeHolderTenure"]
109
115
 
110
- def check(self, element):
116
+ def check(self, element: etree._Element) -> None:
111
117
  office_id = element.find("OfficeId").text
112
118
  office_selection_method = element.find("OfficeSelectionMethod").text
113
119
  if (
@@ -125,10 +131,10 @@ class OfficeSelectionMethodMatch(base.BaseRule):
125
131
  class OfficeHolderTenureTermDates(base.DateRule):
126
132
  """OfficeHolderTenure elements should contain valid term dates."""
127
133
 
128
- def elements(self):
134
+ def elements(self) -> list[str]:
129
135
  return ["OfficeHolderTenure"]
130
136
 
131
- def check(self, element):
137
+ def check(self, element: etree._Element) -> None:
132
138
  start_date = element.find("StartDate")
133
139
  end_date = element.find("EndDate")
134
140
  if rule_util.element_has_text(start_date) and rule_util.element_has_text(
@@ -153,7 +159,12 @@ class OfficeTermDates(base.DateRule):
153
159
  not have a Term element.
154
160
  """
155
161
 
156
- def __init__(self, election_tree, schema_tree=None, **kwargs):
162
+ def __init__(
163
+ self,
164
+ election_tree: etree._ElementTree,
165
+ schema_tree: etree._ElementTree | None = None,
166
+ **kwargs,
167
+ ) -> None:
157
168
  self.is_post_office_split_feed = False
158
169
  officeholder_tenure_collection_element = self.get_elements_by_class(
159
170
  election_tree, "OfficeHolderTenureCollection"
@@ -161,10 +172,10 @@ class OfficeTermDates(base.DateRule):
161
172
  if officeholder_tenure_collection_element:
162
173
  self.is_post_office_split_feed = True
163
174
 
164
- def elements(self):
175
+ def elements(self) -> list[str]:
165
176
  return ["Office"]
166
177
 
167
- def check(self, element):
178
+ def check(self, element: etree._Element) -> None:
168
179
  self.reset_instance_vars()
169
180
  if self.is_post_office_split_feed:
170
181
  term = element.find("Term")
@@ -205,7 +216,7 @@ class RemovePersonAndOfficeHolderId60DaysAfterEndDate(base.TreeRule):
205
216
  they will be kept in the feed with the same stable ID.
206
217
  """
207
218
 
208
- def check(self):
219
+ def check(self) -> None:
209
220
  info_log = []
210
221
  persons = self.get_elements_by_class(self.election_tree, "Person")
211
222
  offices = self.get_elements_by_class(self.election_tree, "Office")
@@ -329,10 +340,12 @@ class UniqueStartDatesForOfficeRoleAndJurisdiction(base.BaseRule):
329
340
  with the same jurisdiction-id and office-role.
330
341
  """
331
342
 
332
- def elements(self):
343
+ def elements(self) -> list[str]:
333
344
  return ["OfficeCollection"]
334
345
 
335
- def _filter_out_past_end_dates(self, offices):
346
+ def _filter_out_past_end_dates(
347
+ self, offices: Iterable[etree._Element]
348
+ ) -> list[etree._Element]:
336
349
  valid_offices = []
337
350
  for office in offices:
338
351
  term = office.find(".//Term")
@@ -350,7 +363,9 @@ class UniqueStartDatesForOfficeRoleAndJurisdiction(base.BaseRule):
350
363
  continue
351
364
  return valid_offices
352
365
 
353
- def _count_start_dates_by_jurisdiction_role(self, element):
366
+ def _count_start_dates_by_jurisdiction_role(
367
+ self, element: etree._Element
368
+ ) -> dict[str, dict[str, Any]]:
354
369
  offices = element.findall("Office")
355
370
  offices = self._filter_out_past_end_dates(offices)
356
371
  jurisdiction_role_mapping = {}
@@ -394,7 +409,7 @@ class UniqueStartDatesForOfficeRoleAndJurisdiction(base.BaseRule):
394
409
 
395
410
  return jurisdiction_role_mapping
396
411
 
397
- def check(self, element):
412
+ def check(self, element: etree._Element) -> None:
398
413
  warning_log = []
399
414
 
400
415
  start_counts = self._count_start_dates_by_jurisdiction_role(element)
@@ -406,16 +421,10 @@ class UniqueStartDatesForOfficeRoleAndJurisdiction(base.BaseRule):
406
421
  if len(start_date_map[start_date]) > 1:
407
422
  warning_log.append(
408
423
  loggers.LogEntry(
409
- (
410
- "Only one unique StartDate found for each"
411
- " jurisdiction-id: {} and office-role: {}. {} appears {}"
412
- " times."
413
- ).format(
414
- start_info["jurisdiction_id"],
415
- start_info["office_role"],
416
- start_date,
417
- len(start_date_map[start_date]),
418
- ),
424
+ "Only one unique StartDate found for each jurisdiction-id:"
425
+ f" {start_info['jurisdiction_id']} and office-role:"
426
+ f" {start_info['office_role']}. {start_date} appears"
427
+ f" {len(start_date_map[start_date])} times.",
419
428
  start_date_map[start_date],
420
429
  )
421
430
  )
@@ -431,7 +440,12 @@ class UnreferencedEntitiesOfficeholders(common_rules.UnreferencedEntitiesBase):
431
440
  LatAm feeds.
432
441
  """
433
442
 
434
- def __init__(self, election_tree, schema_tree, **kwargs):
443
+ def __init__(
444
+ self,
445
+ election_tree: etree._ElementTree,
446
+ schema_tree: etree._ElementTree,
447
+ **kwargs,
448
+ ) -> None:
435
449
  office_holder_tenure_collection = self.get_elements_by_class(
436
450
  election_tree, "OfficeHolderTenure"
437
451
  )
@@ -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.68.dev17'
8
+ __version__ = '1.69.dev0'