Skip to content

Commit

Permalink
Update ImportedNamespaces.
Browse files Browse the repository at this point in the history
  • Loading branch information
junkmd committed Sep 23, 2024
1 parent 21cc411 commit 1fcf943
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions comtypes/tools/codegenerator/namespaces.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import textwrap
from typing import Optional
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:
Expand Down Expand Up @@ -44,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:
Expand Down Expand Up @@ -78,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:
Expand All @@ -90,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)
Expand Down

0 comments on commit 1fcf943

Please sign in to comment.