Static representation of a namespace dictionary. This is used for module, class and function namespaces.
| 5048 | |
| 5049 | |
| 5050 | class SymbolTable(dict[str, SymbolTableNode]): |
| 5051 | """Static representation of a namespace dictionary. |
| 5052 | |
| 5053 | This is used for module, class and function namespaces. |
| 5054 | """ |
| 5055 | |
| 5056 | __slots__ = () |
| 5057 | |
| 5058 | def __str__(self) -> str: |
| 5059 | a: list[str] = [] |
| 5060 | for key, value in self.items(): |
| 5061 | # Filter out the implicit import of builtins. |
| 5062 | if isinstance(value, SymbolTableNode): |
| 5063 | if ( |
| 5064 | value.fullname != "builtins" |
| 5065 | and (value.fullname or "").split(".")[-1] not in implicit_module_attrs |
| 5066 | ): |
| 5067 | a.append(" " + str(key) + " : " + str(value)) |
| 5068 | else: |
| 5069 | # Used in debugging: |
| 5070 | a.append(" <invalid item>") # type: ignore[unreachable] |
| 5071 | a = sorted(a) |
| 5072 | a.insert(0, "SymbolTable(") |
| 5073 | a[-1] += ")" |
| 5074 | return "\n".join(a) |
| 5075 | |
| 5076 | def copy(self) -> SymbolTable: |
| 5077 | return SymbolTable([(key, node.copy()) for key, node in self.items()]) |
| 5078 | |
| 5079 | def serialize(self, fullname: str) -> JsonDict: |
| 5080 | data: JsonDict = {".class": "SymbolTable"} |
| 5081 | for key, value in self.items(): |
| 5082 | # Skip __builtins__: it's a reference to the builtins |
| 5083 | # module that gets added to every module by |
| 5084 | # SemanticAnalyzerPass2.visit_file(), but it shouldn't be |
| 5085 | # accessed by users of the module. |
| 5086 | if key == "__builtins__" or value.no_serialize: |
| 5087 | continue |
| 5088 | data[key] = value.serialize(fullname, key) |
| 5089 | return data |
| 5090 | |
| 5091 | @classmethod |
| 5092 | def deserialize(cls, data: JsonDict) -> SymbolTable: |
| 5093 | assert data[".class"] == "SymbolTable" |
| 5094 | st = SymbolTable() |
| 5095 | for key, value in data.items(): |
| 5096 | if key != ".class": |
| 5097 | st[key] = SymbolTableNode.deserialize(value) |
| 5098 | return st |
| 5099 | |
| 5100 | def write(self, data: WriteBuffer, fullname: str) -> None: |
| 5101 | size = 0 |
| 5102 | for key, value in self.items(): |
| 5103 | # Skip __builtins__: it's a reference to the builtins |
| 5104 | # module that gets added to every module by |
| 5105 | # SemanticAnalyzerPass2.visit_file(), but it shouldn't be |
| 5106 | # accessed by users of the module. |
| 5107 | if key == "__builtins__" or value.no_serialize: |
no outgoing calls
no test coverage detected
searching dependent graphs…