diff --git a/comtypes/tools/codegenerator/namespaces.py b/comtypes/tools/codegenerator/namespaces.py index be198e9b..d84fe087 100644 --- a/comtypes/tools/codegenerator/namespaces.py +++ b/comtypes/tools/codegenerator/namespaces.py @@ -1,12 +1,19 @@ import textwrap +from typing import Optional, Union as _UnionT from typing import Dict, List, Set, Tuple +from typing import Mapping, Sequence class ImportedNamespaces(object): - def __init__(self): - self.data = {} - - def add(self, name1, name2=None, symbols=None): + def __init__(self) -> None: + self.data: Dict[str, Optional[str]] = {} + + def add( + self, + name1: str, + name2: Optional[str] = None, + symbols: Optional[Mapping[str, str]] = None, + ) -> None: """Adds a namespace will be imported. Examples: @@ -43,7 +50,7 @@ def add(self, name1, name2=None, symbols=None): from_, import_ = name1, name2 self.data[import_] = from_ - def __contains__(self, item): + def __contains__(self, item: _UnionT[str, Tuple[str, str]]) -> bool: """Returns item has already added. Examples: @@ -77,7 +84,7 @@ def get_symbols(self) -> Set[str]: names.add(key) return names - def _make_line(self, from_, imports): + def _make_line(self, from_: str, imports: Sequence[str]) -> str: import_ = ", ".join(imports) code = "from %s import %s" % (from_, import_) if len(code) <= 80: @@ -89,16 +96,16 @@ def _make_line(self, from_, imports): code = "from %s import (\n%s\n)" % (from_, import_) return code - def getvalue(self): - ns = {} - lines = [] + def getvalue(self) -> str: + ns: Dict[str, Optional[Set[str]]] = {} + lines: List[str] = [] for key, val in self.data.items(): if val is None: ns[key] = val elif key == "*": lines.append("from %s import *" % val) else: - ns.setdefault(val, set()).add(key) + ns.setdefault(val, set()).add(key) # type: ignore for key, val in ns.items(): if val is None: lines.append("import %s" % key) @@ -109,10 +116,10 @@ def getvalue(self): class DeclaredNamespaces(object): - def __init__(self): - self.data = {} + def __init__(self) -> None: + self.data: Dict[Tuple[str, str], Optional[str]] = {} - def add(self, alias, definition, comment=None): + def add(self, alias: str, definition: str, comment: Optional[str] = None) -> None: """Adds a namespace will be declared. Examples: @@ -134,7 +141,7 @@ def get_symbols(self) -> Set[str]: names.add(alias) return names - def getvalue(self): + def getvalue(self) -> str: lines = [] for (alias, definition), comment in self.data.items(): code = "%s = %s" % (alias, definition) @@ -145,7 +152,7 @@ def getvalue(self): class EnumerationNamespaces(object): - def __init__(self): + def __init__(self) -> None: self.data: Dict[str, List[Tuple[str, int]]] = {} def add(self, enum_name: str, member_name: str, value: int) -> None: