Add type information to symbolfile and ndkstubgen.
Test: mypy symbolfile
Test: pytest
Bug: None
Change-Id: I6b1045d315e5a10e699d31de9fafc084d82768b2
diff --git a/cc/ndkstubgen/__init__.py b/cc/ndkstubgen/__init__.py
index 2f4326a..86bf6ff 100755
--- a/cc/ndkstubgen/__init__.py
+++ b/cc/ndkstubgen/__init__.py
@@ -20,13 +20,16 @@
import logging
import os
import sys
+from typing import Iterable, TextIO
import symbolfile
+from symbolfile import Arch, Version
class Generator:
"""Output generator that writes stub source files and version scripts."""
- def __init__(self, src_file, version_script, arch, api, llndk, apex):
+ def __init__(self, src_file: TextIO, version_script: TextIO, arch: Arch,
+ api: int, llndk: bool, apex: bool) -> None:
self.src_file = src_file
self.version_script = version_script
self.arch = arch
@@ -34,12 +37,12 @@
self.llndk = llndk
self.apex = apex
- def write(self, versions):
+ def write(self, versions: Iterable[Version]) -> None:
"""Writes all symbol data to the output files."""
for version in versions:
self.write_version(version)
- def write_version(self, version):
+ def write_version(self, version: Version) -> None:
"""Writes a single version block's data to the output files."""
if symbolfile.should_omit_version(version, self.arch, self.api,
self.llndk, self.apex):
@@ -84,7 +87,7 @@
self.version_script.write('}' + base + ';\n')
-def parse_args():
+def parse_args() -> argparse.Namespace:
"""Parses and returns command line arguments."""
parser = argparse.ArgumentParser()
@@ -100,23 +103,31 @@
parser.add_argument(
'--apex', action='store_true', help='Use the APEX variant.')
+ # https://github.com/python/mypy/issues/1317
+ # mypy has issues with using os.path.realpath as an argument here.
parser.add_argument(
- '--api-map', type=os.path.realpath, required=True,
+ '--api-map',
+ type=os.path.realpath, # type: ignore
+ required=True,
help='Path to the API level map JSON file.')
parser.add_argument(
- 'symbol_file', type=os.path.realpath, help='Path to symbol file.')
+ 'symbol_file',
+ type=os.path.realpath, # type: ignore
+ help='Path to symbol file.')
parser.add_argument(
- 'stub_src', type=os.path.realpath,
+ 'stub_src',
+ type=os.path.realpath, # type: ignore
help='Path to output stub source file.')
parser.add_argument(
- 'version_script', type=os.path.realpath,
+ 'version_script',
+ type=os.path.realpath, # type: ignore
help='Path to output version script.')
return parser.parse_args()
-def main():
+def main() -> None:
"""Program entry point."""
args = parse_args()
diff --git a/cc/ndkstubgen/mypy.ini b/cc/ndkstubgen/mypy.ini
new file mode 100644
index 0000000..82aa7eb
--- /dev/null
+++ b/cc/ndkstubgen/mypy.ini
@@ -0,0 +1,2 @@
+[mypy]
+disallow_untyped_defs = True
diff --git a/cc/ndkstubgen/test_ndkstubgen.py b/cc/ndkstubgen/test_ndkstubgen.py
index 70bcf78..6d2c9d6 100755
--- a/cc/ndkstubgen/test_ndkstubgen.py
+++ b/cc/ndkstubgen/test_ndkstubgen.py
@@ -21,19 +21,20 @@
import ndkstubgen
import symbolfile
+from symbolfile import Arch, Tag
# pylint: disable=missing-docstring
class GeneratorTest(unittest.TestCase):
- def test_omit_version(self):
+ def test_omit_version(self) -> None:
# Thorough testing of the cases involved here is handled by
# OmitVersionTest, PrivateVersionTest, and SymbolPresenceTest.
src_file = io.StringIO()
version_file = io.StringIO()
- generator = ndkstubgen.Generator(src_file, version_file, 'arm', 9,
- False, False)
+ generator = ndkstubgen.Generator(src_file, version_file, Arch('arm'),
+ 9, False, False)
version = symbolfile.Version('VERSION_PRIVATE', None, [], [
symbolfile.Symbol('foo', []),
@@ -42,74 +43,75 @@
self.assertEqual('', src_file.getvalue())
self.assertEqual('', version_file.getvalue())
- version = symbolfile.Version('VERSION', None, ['x86'], [
+ version = symbolfile.Version('VERSION', None, [Tag('x86')], [
symbolfile.Symbol('foo', []),
])
generator.write_version(version)
self.assertEqual('', src_file.getvalue())
self.assertEqual('', version_file.getvalue())
- version = symbolfile.Version('VERSION', None, ['introduced=14'], [
+ version = symbolfile.Version('VERSION', None, [Tag('introduced=14')], [
symbolfile.Symbol('foo', []),
])
generator.write_version(version)
self.assertEqual('', src_file.getvalue())
self.assertEqual('', version_file.getvalue())
- def test_omit_symbol(self):
+ def test_omit_symbol(self) -> None:
# Thorough testing of the cases involved here is handled by
# SymbolPresenceTest.
src_file = io.StringIO()
version_file = io.StringIO()
- generator = ndkstubgen.Generator(src_file, version_file, 'arm', 9,
- False, False)
+ generator = ndkstubgen.Generator(src_file, version_file, Arch('arm'),
+ 9, False, False)
version = symbolfile.Version('VERSION_1', None, [], [
- symbolfile.Symbol('foo', ['x86']),
+ symbolfile.Symbol('foo', [Tag('x86')]),
])
generator.write_version(version)
self.assertEqual('', src_file.getvalue())
self.assertEqual('', version_file.getvalue())
version = symbolfile.Version('VERSION_1', None, [], [
- symbolfile.Symbol('foo', ['introduced=14']),
+ symbolfile.Symbol('foo', [Tag('introduced=14')]),
])
generator.write_version(version)
self.assertEqual('', src_file.getvalue())
self.assertEqual('', version_file.getvalue())
version = symbolfile.Version('VERSION_1', None, [], [
- symbolfile.Symbol('foo', ['llndk']),
+ symbolfile.Symbol('foo', [Tag('llndk')]),
])
generator.write_version(version)
self.assertEqual('', src_file.getvalue())
self.assertEqual('', version_file.getvalue())
version = symbolfile.Version('VERSION_1', None, [], [
- symbolfile.Symbol('foo', ['apex']),
+ symbolfile.Symbol('foo', [Tag('apex')]),
])
generator.write_version(version)
self.assertEqual('', src_file.getvalue())
self.assertEqual('', version_file.getvalue())
- def test_write(self):
+ def test_write(self) -> None:
src_file = io.StringIO()
version_file = io.StringIO()
- generator = ndkstubgen.Generator(src_file, version_file, 'arm', 9,
- False, False)
+ generator = ndkstubgen.Generator(src_file, version_file, Arch('arm'),
+ 9, False, False)
versions = [
symbolfile.Version('VERSION_1', None, [], [
symbolfile.Symbol('foo', []),
- symbolfile.Symbol('bar', ['var']),
- symbolfile.Symbol('woodly', ['weak']),
- symbolfile.Symbol('doodly', ['weak', 'var']),
+ symbolfile.Symbol('bar', [Tag('var')]),
+ symbolfile.Symbol('woodly', [Tag('weak')]),
+ symbolfile.Symbol('doodly',
+ [Tag('weak'), Tag('var')]),
]),
symbolfile.Version('VERSION_2', 'VERSION_1', [], [
symbolfile.Symbol('baz', []),
]),
symbolfile.Version('VERSION_3', 'VERSION_1', [], [
- symbolfile.Symbol('qux', ['versioned=14']),
+ symbolfile.Symbol('qux', [Tag('versioned=14')]),
]),
]
@@ -141,7 +143,7 @@
class IntegrationTest(unittest.TestCase):
- def test_integration(self):
+ def test_integration(self) -> None:
api_map = {
'O': 9000,
'P': 9001,
@@ -178,14 +180,14 @@
wobble;
} VERSION_4;
"""))
- parser = symbolfile.SymbolFileParser(input_file, api_map, 'arm', 9,
- False, False)
+ parser = symbolfile.SymbolFileParser(input_file, api_map, Arch('arm'),
+ 9, False, False)
versions = parser.parse()
src_file = io.StringIO()
version_file = io.StringIO()
- generator = ndkstubgen.Generator(src_file, version_file, 'arm', 9,
- False, False)
+ generator = ndkstubgen.Generator(src_file, version_file, Arch('arm'),
+ 9, False, False)
generator.write(versions)
expected_src = textwrap.dedent("""\
@@ -213,7 +215,7 @@
""")
self.assertEqual(expected_version, version_file.getvalue())
- def test_integration_future_api(self):
+ def test_integration_future_api(self) -> None:
api_map = {
'O': 9000,
'P': 9001,
@@ -230,14 +232,14 @@
*;
};
"""))
- parser = symbolfile.SymbolFileParser(input_file, api_map, 'arm', 9001,
- False, False)
+ parser = symbolfile.SymbolFileParser(input_file, api_map, Arch('arm'),
+ 9001, False, False)
versions = parser.parse()
src_file = io.StringIO()
version_file = io.StringIO()
- generator = ndkstubgen.Generator(src_file, version_file, 'arm', 9001,
- False, False)
+ generator = ndkstubgen.Generator(src_file, version_file, Arch('arm'),
+ 9001, False, False)
generator.write(versions)
expected_src = textwrap.dedent("""\
@@ -255,7 +257,7 @@
""")
self.assertEqual(expected_version, version_file.getvalue())
- def test_multiple_definition(self):
+ def test_multiple_definition(self) -> None:
input_file = io.StringIO(textwrap.dedent("""\
VERSION_1 {
global:
@@ -280,8 +282,8 @@
} VERSION_2;
"""))
- parser = symbolfile.SymbolFileParser(input_file, {}, 'arm', 16, False,
- False)
+ parser = symbolfile.SymbolFileParser(input_file, {}, Arch('arm'), 16,
+ False, False)
with self.assertRaises(
symbolfile.MultiplyDefinedSymbolError) as ex_context:
@@ -289,7 +291,7 @@
self.assertEqual(['bar', 'foo'],
ex_context.exception.multiply_defined_symbols)
- def test_integration_with_apex(self):
+ def test_integration_with_apex(self) -> None:
api_map = {
'O': 9000,
'P': 9001,
@@ -328,14 +330,14 @@
wobble;
} VERSION_4;
"""))
- parser = symbolfile.SymbolFileParser(input_file, api_map, 'arm', 9,
- False, True)
+ parser = symbolfile.SymbolFileParser(input_file, api_map, Arch('arm'),
+ 9, False, True)
versions = parser.parse()
src_file = io.StringIO()
version_file = io.StringIO()
- generator = ndkstubgen.Generator(src_file, version_file, 'arm', 9,
- False, True)
+ generator = ndkstubgen.Generator(src_file, version_file, Arch('arm'),
+ 9, False, True)
generator.write(versions)
expected_src = textwrap.dedent("""\
@@ -369,7 +371,8 @@
""")
self.assertEqual(expected_version, version_file.getvalue())
-def main():
+
+def main() -> None:
suite = unittest.TestLoader().loadTestsFromName(__name__)
unittest.TextTestRunner(verbosity=3).run(suite)