codegen.py: add type hints
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Thu, 3 Sep 2020 16:09:18 +0000 (12:09 -0400)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Thu, 3 Sep 2020 16:09:18 +0000 (12:09 -0400)
Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
barectf/codegen.py

index f0d375829e6ecadeb5740edd2106f00b42f92a7d..025dfa9b7b553232c1925aeec9eddf206aa09257 100644 (file)
@@ -25,6 +25,7 @@ import barectf.tsdl182gen as barectf_tsdl182gen
 import barectf.template as barectf_template
 import barectf.config as barectf_config
 import barectf.cgen as barectf_cgen
+from typing import List, Optional
 
 
 # A file generated by a `CodeGenerator` object.
@@ -32,16 +33,16 @@ import barectf.cgen as barectf_cgen
 # A generated file has a name (influenced by the configuration's
 # file name prefix option) and contents.
 class _GeneratedFile:
-    def __init__(self, name, contents):
+    def __init__(self, name: str, contents: str):
         self._name = name
         self._contents = contents
 
     @property
-    def name(self):
+    def name(self) -> str:
         return self._name
 
     @property
-    def contents(self):
+    def contents(self) -> str:
         return self._contents
 
 
@@ -52,23 +53,23 @@ class _GeneratedFile:
 # A code generator can generate the TSDL `metadata` file and C source
 # and header files.
 class CodeGenerator:
-    def __init__(self, configuration):
+    def __init__(self, configuration: barectf_config.Configuration):
         self._config = configuration
         self._file_name_prefix = configuration.options.code_generation_options.file_name_prefix
         self._c_code_gen = barectf_cgen._CodeGen(configuration)
-        self._c_headers = None
-        self._c_sources = None
-        self._metadata_stream = None
+        self._c_headers: Optional[List[_GeneratedFile]] = None
+        self._c_sources: Optional[List[_GeneratedFile]] = None
+        self._metadata_stream: Optional[_GeneratedFile] = None
 
     @property
-    def _barectf_header_name(self):
+    def _barectf_header_name(self) -> str:
         return f'{self._file_name_prefix}.h'
 
     @property
-    def _bitfield_header_name(self):
+    def _bitfield_header_name(self) -> str:
         return f'{self._file_name_prefix}-bitfield.h'
 
-    def generate_c_headers(self):
+    def generate_c_headers(self) -> List[_GeneratedFile]:
         if self._c_headers is None:
             self._c_headers = [
                 _GeneratedFile(self._barectf_header_name, self._c_code_gen.gen_header()),
@@ -77,7 +78,7 @@ class CodeGenerator:
 
         return self._c_headers
 
-    def generate_c_sources(self):
+    def generate_c_sources(self) -> List[_GeneratedFile]:
         if self._c_sources is None:
             self._c_sources = [
                 _GeneratedFile(f'{self._file_name_prefix}.c',
@@ -87,7 +88,7 @@ class CodeGenerator:
 
         return self._c_sources
 
-    def generate_metadata_stream(self):
+    def generate_metadata_stream(self) -> _GeneratedFile:
         if self._metadata_stream is None:
             self._metadata_stream = _GeneratedFile('metadata',
                                                    barectf_tsdl182gen._from_config(self._config))
This page took 0.024744 seconds and 4 git commands to generate.