Skip to content

Commit 8f8a32e

Browse files
committed
Add type hints to functions
1 parent 2e1b234 commit 8f8a32e

17 files changed

Lines changed: 105 additions & 61 deletions

pre_commit_hooks/check_autopkg_recipe_list.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
88
import argparse
99
import json
1010
import plistlib
11+
from typing import List, Optional
1112
from xml.parsers.expat import ExpatError
1213

1314
import ruamel.yaml
1415

1516
yaml = ruamel.yaml.YAML(typ="safe")
1617

1718

18-
def build_argument_parser():
19+
def build_argument_parser() -> argparse.ArgumentParser:
1920
"""Build and return the argument parser."""
2021

2122
parser = argparse.ArgumentParser(
@@ -25,7 +26,7 @@ def build_argument_parser():
2526
return parser
2627

2728

28-
def main(argv=None):
29+
def main(argv: Optional[List[str]] = None) -> int:
2930
"""Main process."""
3031

3132
# Parse command line arguments.

pre_commit_hooks/check_autopkg_recipes.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import os
77
import sys
88
from contextlib import contextmanager
9+
from typing import Any, Dict, List
910

1011
from packaging.version import Version
1112

@@ -23,7 +24,7 @@
2324

2425
# Import AutoPkg libraries, but ignore any warnings generated by the import.
2526
@contextmanager
26-
def suppress_stdout():
27+
def suppress_stdout() -> Any:
2728
with open(os.devnull, "w", encoding="utf-8") as devnull:
2829
old_stdout = sys.stdout
2930
sys.stdout = devnull
@@ -36,15 +37,18 @@ def suppress_stdout():
3637
sys.path.append("/Library/AutoPkg")
3738
try:
3839
with suppress_stdout():
39-
from autopkglib import get_processor, processor_names
40+
from autopkglib import ( # type: ignore[import-not-found]
41+
get_processor,
42+
processor_names,
43+
)
4044

4145
HAS_AUTOPKGLIB = True
4246
except ImportError:
4347
# Silently skip checks that require autopkglib.
4448
HAS_AUTOPKGLIB = False
4549

4650

47-
def build_argument_parser():
51+
def build_argument_parser() -> argparse.ArgumentParser:
4852
"""Build and return the argument parser."""
4953

5054
parser = argparse.ArgumentParser(
@@ -83,7 +87,9 @@ def build_argument_parser():
8387
return parser
8488

8589

86-
def validate_recipe_prefix(recipe, filename, prefix):
90+
def validate_recipe_prefix(
91+
recipe: Dict[str, Any], filename: str, prefix: List[str]
92+
) -> bool:
8793
"""Verify that the recipe identifier starts with the expected prefix."""
8894

8995
passed = True

pre_commit_hooks/check_git_config_email.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33

44
import argparse
55
import subprocess
6+
from typing import List, Optional
67

78

8-
def build_argument_parser():
9+
def build_argument_parser() -> argparse.ArgumentParser:
910
"""Build and return the argument parser."""
1011

1112
parser = argparse.ArgumentParser(
@@ -19,7 +20,7 @@ def build_argument_parser():
1920
return parser
2021

2122

22-
def main(argv=None):
23+
def main(argv: Optional[List[str]] = None) -> int:
2324
"""Main process."""
2425

2526
# Parse command line arguments.

pre_commit_hooks/check_jamf_extension_attributes.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33

44
import argparse
55
import re
6+
from typing import List, Optional
67

78
from pre_commit_hooks.util import validate_shebangs
89

910

10-
def build_argument_parser():
11+
def build_argument_parser() -> argparse.ArgumentParser:
1112
"""Build and return the argument parser."""
1213

1314
parser = argparse.ArgumentParser(
@@ -23,7 +24,7 @@ def build_argument_parser():
2324
return parser
2425

2526

26-
def main(argv=None):
27+
def main(argv: Optional[List[str]] = None) -> int:
2728
"""Main process."""
2829

2930
# Parse command line arguments.

pre_commit_hooks/check_jamf_json_manifests.py

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import argparse
99
import json
1010
from datetime import datetime
11+
from typing import Any, Dict, List, Optional, Tuple
1112

1213
from pre_commit_hooks.util import validate_required_keys
1314

@@ -34,7 +35,7 @@
3435
}
3536

3637

37-
def build_argument_parser():
38+
def build_argument_parser() -> argparse.ArgumentParser:
3839
"""Build and return the argument parser."""
3940

4041
parser = argparse.ArgumentParser(
@@ -44,7 +45,7 @@ def build_argument_parser():
4445
return parser
4546

4647

47-
def validate_key_types(name, manifest, filename):
48+
def validate_key_types(name: str, manifest: Dict[str, Any], filename: str) -> bool:
4849
"""Validation of manifest key types."""
4950

5051
# Manifest keys and their known types. Omitted keys are left unvalidated.
@@ -78,7 +79,9 @@ def validate_key_types(name, manifest, filename):
7879
return passed
7980

8081

81-
def validate_type(name, property, filename):
82+
def validate_type(
83+
name: str, property: Dict[str, Any], filename: str
84+
) -> Tuple[bool, Optional[str]]: # noqa: A002
8285
"""Ensure property type keu is present and among expected values."""
8386
passed = True
8487
type_found = None
@@ -98,7 +101,9 @@ def validate_type(name, property, filename):
98101
return passed, type_found
99102

100103

101-
def validate_list_item_types(name, manifest, filename):
104+
def validate_list_item_types(
105+
name: str, manifest: Dict[str, Any], filename: str
106+
) -> bool:
102107
"""Validation of list member items."""
103108

104109
passed = True
@@ -109,10 +114,13 @@ def validate_list_item_types(name, manifest, filename):
109114
except IndexError:
110115
# Probably an empty array; no way to validate items
111116
continue
112-
if isinstance(MANIFEST_LIST_TYPES[name], tuple):
113-
desired_types = MANIFEST_LIST_TYPES[name]
117+
manifest_list_type = MANIFEST_LIST_TYPES[name]
118+
if isinstance(manifest_list_type, tuple):
119+
# MANIFEST_LIST_TYPES[name] is a tuple of types
120+
desired_types = list(manifest_list_type)
114121
else:
115-
desired_types = [MANIFEST_LIST_TYPES[name]]
122+
# MANIFEST_LIST_TYPES[name] is a single type
123+
desired_types = [manifest_list_type]
116124
if actual_type not in desired_types:
117125
print(
118126
f'{filename}: "{name}" items should be {MANIFEST_LIST_TYPES[name]}, not {actual_type}'
@@ -122,7 +130,9 @@ def validate_list_item_types(name, manifest, filename):
122130
return passed
123131

124132

125-
def validate_default(name, prop, type_found, filename):
133+
def validate_default(
134+
name: str, prop: Dict[str, Any], type_found: Optional[str], filename: str
135+
) -> bool:
126136
"""Ensure that default values have the expected type."""
127137
passed = True
128138

@@ -132,16 +142,16 @@ def validate_default(name, prop, type_found, filename):
132142
actual_type = str
133143
else:
134144
actual_type = type(prop[test_key])
135-
if actual_type != MANIFEST_TYPES.get(type_found):
145+
if actual_type != MANIFEST_TYPES.get(type_found) if type_found else None:
136146
print(
137-
f"{filename}: {test_key} value for {name} should be {MANIFEST_TYPES.get(type_found)}, not {type(prop[test_key])}"
147+
f"{filename}: {test_key} value for {name} should be {MANIFEST_TYPES.get(type_found) if type_found else 'Unknown'}, not {type(prop[test_key])}"
138148
)
139149
passed = False
140150

141151
return passed
142152

143153

144-
def validate_urls(name, prop, filename):
154+
def validate_urls(name: str, prop: Dict[str, Any], filename: str) -> bool:
145155
"""Ensure that URL values are actual URLs."""
146156
passed = True
147157

@@ -157,7 +167,7 @@ def validate_urls(name, prop, filename):
157167
return passed
158168

159169

160-
def validate_properties(properties, filename):
170+
def validate_properties(properties: Dict[str, Any], filename: str) -> bool:
161171
"""Given a list of properties, run validation on their contents."""
162172
passed = True
163173

@@ -196,7 +206,7 @@ def validate_properties(properties, filename):
196206
return passed
197207

198208

199-
def main(argv=None):
209+
def main(argv: Optional[List[str]] = None) -> int:
200210
"""Main process."""
201211

202212
# Parse command line arguments.
@@ -214,7 +224,7 @@ def main(argv=None):
214224
break # No need to continue checking this file
215225

216226
# Check for presence of required keys.
217-
required_keys = ("title", "properties", "description")
227+
required_keys = ["title", "properties", "description"]
218228
if not validate_required_keys(manifest, filename, required_keys):
219229
retval = 1
220230
break # No need to continue checking this file

pre_commit_hooks/check_jamf_profiles.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33

44
import argparse
55
import plistlib
6+
from typing import List, Optional
67
from xml.parsers.expat import ExpatError
78

89

9-
def build_argument_parser():
10+
def build_argument_parser() -> argparse.ArgumentParser:
1011
"""Build and return the argument parser."""
1112

1213
parser = argparse.ArgumentParser(
@@ -16,7 +17,7 @@ def build_argument_parser():
1617
return parser
1718

1819

19-
def main(argv=None):
20+
def main(argv: Optional[List[str]] = None) -> int:
2021
"""Main process."""
2122

2223
# Parse command line arguments.

pre_commit_hooks/check_jamf_scripts.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
"""Check Jamf scripts for common issues."""
33

44
import argparse
5+
from typing import List, Optional
56

67
from pre_commit_hooks.util import validate_shebangs
78

89

9-
def build_argument_parser():
10+
def build_argument_parser() -> argparse.ArgumentParser:
1011
"""Build and return the argument parser."""
1112

1213
parser = argparse.ArgumentParser(
@@ -22,7 +23,7 @@ def build_argument_parser():
2223
return parser
2324

2425

25-
def main(argv=None):
26+
def main(argv: Optional[List[str]] = None) -> int:
2627
"""Main process."""
2728

2829
# Parse command line arguments.

pre_commit_hooks/check_munki_pkgsinfo.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import os
66
import plistlib
77
from pathlib import Path
8+
from typing import List, Optional
89
from xml.parsers.expat import ExpatError
910

1011
from pre_commit_hooks.util import (
@@ -19,7 +20,7 @@
1920
)
2021

2122

22-
def build_argument_parser():
23+
def build_argument_parser() -> argparse.ArgumentParser:
2324
"""Build and return the argument parser."""
2425

2526
parser = argparse.ArgumentParser(
@@ -70,7 +71,7 @@ def build_argument_parser():
7071
return parser
7172

7273

73-
def _check_case_sensitive_path(path):
74+
def _check_case_sensitive_path(path: str) -> bool:
7475
"""Check whether a path exists, and on case-sensitive filesystems check
7576
that there is no case conflict."""
7677
# Return immediately if the file does not exist
@@ -88,7 +89,7 @@ def _check_case_sensitive_path(path):
8889
p = p.parent
8990

9091

91-
def main(argv=None):
92+
def main(argv: Optional[List[str]] = None) -> int:
9293
"""Main process."""
9394

9495
# Typical extensions for installer packages.

pre_commit_hooks/check_munkiadmin_scripts.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33

44
import argparse
55
import os
6+
from typing import List, Optional
67

78
from pre_commit_hooks.util import validate_shebangs
89

910

10-
def build_argument_parser():
11+
def build_argument_parser() -> argparse.ArgumentParser:
1112
"""Build and return the argument parser."""
1213

1314
parser = argparse.ArgumentParser(
@@ -17,7 +18,7 @@ def build_argument_parser():
1718
return parser
1819

1920

20-
def main(argv=None):
21+
def main(argv: Optional[List[str]] = None) -> int:
2122
"""Main process."""
2223

2324
# Parse command line arguments.

pre_commit_hooks/check_munkipkg_buildinfo.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import argparse
55
import json
66
import plistlib
7+
from typing import Any, Dict, List, Optional
78
from xml.parsers.expat import ExpatError
89

910
import ruamel.yaml
@@ -13,7 +14,7 @@
1314
yaml = ruamel.yaml.YAML(typ="safe")
1415

1516

16-
def build_argument_parser():
17+
def build_argument_parser() -> argparse.ArgumentParser:
1718
"""Build and return the argument parser."""
1819

1920
parser = argparse.ArgumentParser(
@@ -28,7 +29,7 @@ def build_argument_parser():
2829
return parser
2930

3031

31-
def validate_buildinfo_key_types(buildinfo, filename):
32+
def validate_buildinfo_key_types(buildinfo: Dict[str, Any], filename: str) -> bool:
3233
"""Ensure build-info files contain the proper types."""
3334

3435
# Pkginfo keys and their known types. Omitted keys are left unvalidated.
@@ -61,7 +62,7 @@ def validate_buildinfo_key_types(buildinfo, filename):
6162
return passed
6263

6364

64-
def main(argv=None):
65+
def main(argv: Optional[List[str]] = None) -> int:
6566
"""Main process."""
6667

6768
# Parse command line arguments.
@@ -104,7 +105,7 @@ def main(argv=None):
104105
# Top level keys that all build-info files should contain.
105106
# NOTE: Even though other keys are listed as non-"optional" in the documentation,
106107
# name and version appear to be the only ones that are actually required.
107-
required_keys = ("name", "version")
108+
required_keys = ["name", "version"]
108109
if not validate_required_keys(buildinfo, filename, required_keys):
109110
retval = 1
110111
break # No need to continue checking this file

0 commit comments

Comments
 (0)