Look up an unqualified (no dots) name in all active namespaces. Note that the result may contain a PlaceholderNode. The caller may want to defer in that case. Generate an error if the name is not defined unless suppress_errors is true or the current namespace is inc
(
self, name: str, ctx: Context, suppress_errors: bool = False
)
| 6554 | self.cur_mod_node.module_refs.add(fullname) |
| 6555 | |
| 6556 | def _lookup( |
| 6557 | self, name: str, ctx: Context, suppress_errors: bool = False |
| 6558 | ) -> SymbolTableNode | None: |
| 6559 | """Look up an unqualified (no dots) name in all active namespaces. |
| 6560 | |
| 6561 | Note that the result may contain a PlaceholderNode. The caller may |
| 6562 | want to defer in that case. |
| 6563 | |
| 6564 | Generate an error if the name is not defined unless suppress_errors |
| 6565 | is true or the current namespace is incomplete. In the latter case |
| 6566 | defer. |
| 6567 | """ |
| 6568 | implicit_name = False |
| 6569 | # 1a. Name declared using 'global x' takes precedence |
| 6570 | if name in self.global_decls[-1]: |
| 6571 | if name in self.globals: |
| 6572 | return self.globals[name] |
| 6573 | if not suppress_errors: |
| 6574 | self.name_not_defined(name, ctx) |
| 6575 | return None |
| 6576 | # 1b. Name declared using 'nonlocal x' takes precedence |
| 6577 | if name in self.nonlocal_decls[-1]: |
| 6578 | for table in reversed(self.locals[:-1]): |
| 6579 | if table is not None and name in table: |
| 6580 | return table[name] |
| 6581 | if not suppress_errors: |
| 6582 | self.name_not_defined(name, ctx) |
| 6583 | return None |
| 6584 | # 2a. Class attributes (if within class definition) |
| 6585 | if self.type and not self.is_func_scope() and name in self.type.names: |
| 6586 | node = self.type.names[name] |
| 6587 | if not node.implicit: |
| 6588 | if self.is_active_symbol_in_class_body(node.node): |
| 6589 | return node |
| 6590 | else: |
| 6591 | # Defined through self.x assignment |
| 6592 | implicit_name = True |
| 6593 | implicit_node = node |
| 6594 | # 2b. Class attributes __qualname__ and __module__ |
| 6595 | if self.type and not self.is_func_scope() and name in {"__qualname__", "__module__"}: |
| 6596 | v = Var(name, self.str_type()) |
| 6597 | v._fullname = self.qualified_name(name) |
| 6598 | return SymbolTableNode(MDEF, v) |
| 6599 | # 3. Local (function) scopes |
| 6600 | for table in reversed(self.locals): |
| 6601 | if table is not None and name in table: |
| 6602 | return table[name] |
| 6603 | |
| 6604 | # 4. Current file global scope |
| 6605 | if name in self.globals: |
| 6606 | return self.globals[name] |
| 6607 | # 5. Builtins |
| 6608 | b = self.globals.get("__builtins__", None) |
| 6609 | if b: |
| 6610 | assert isinstance(b.node, MypyFile) |
| 6611 | table = b.node.names |
| 6612 | if name in table: |
| 6613 | if len(name) > 1 and name[0] == "_" and name[1] != "_": |
no test coverage detected