def test_scan_for_import_issues_dictcomp_missing_1(): code = dedent(""" y1 = y2 = 1234 {(x1,y1,z1): (x2,y2,z2) for x1,x2 in []} """) missing, unused = scan_for_import_issues(code) assert unused == [] assert missing == [(3, DottedIdentifier('z1')), (3, DottedIdentifier('z2'))]
def test_scan_for_import_issues_comprehension_attribute_complex_1(): code = dedent(""" dd = [] [(aa,bb) for (bb, cc.cx, dd.dx) in []] """) missing, unused = scan_for_import_issues(code, parse_docstrings=True) assert missing == [(3, DottedIdentifier('aa')), (3, DottedIdentifier('cc.cx'))] assert unused == []
def test_scan_for_import_issues_del_in_func_then_use_1(): code = dedent(""" def f1(): x1 = 123 del x1 print(x1); print(x2) """) missing, unused = scan_for_import_issues(code) assert missing == [(5, DottedIdentifier('x1')), (5, DottedIdentifier('x2'))] assert unused == []
def test_scan_for_import_issues_star_import_local_1(): code = dedent(""" import x1, y1 def f1(): from m2 import * x2 def f2(): x3 x1, x4 """) missing, unused = scan_for_import_issues(code, parse_docstrings=True) assert missing == [(7, DottedIdentifier('x3')), (8, DottedIdentifier('x4'))] assert unused == [(2, Import('import y1'))]
def containing(cls, identifier): """ Try to find the module that defines a name such as C{a.b.c} by trying to import C{a}, C{a.b}, and C{a.b.c}. @return: The name of the 'deepest' module (most commonly it would be C{a.b} in this example). @rtype: L{Module} """ # In the code below we catch "Exception" rather than just ImportError # or AttributeError since importing and __getattr__ing can raise other # exceptions. identifier = DottedIdentifier(identifier) try: module = ModuleHandle(identifier[:1]) result = module.module except Exception as e: raise ImportError(e) for part, prefix in zip(identifier, prefixes(identifier))[1:]: try: result = getattr(result, str(part)) except Exception: try: module = cls(prefix) result = module.module except Exception as e: raise ImportError(e) else: if isinstance(result, types.ModuleType): module = cls(result) logger.debug("Imported %r to get %r", module, identifier) return module
def test_scan_for_import_issues_comprehension_subscript_attribute_missing_1(): code = dedent(""" [123 for xx[0].yy in []] """) missing, unused = scan_for_import_issues(code, parse_docstrings=True) assert missing == [(2, DottedIdentifier('xx'))] assert unused == []
def from_this_module(name): # TODO: could do this more robustly by parsing the AST and # looking for STOREs (definitions/assignments/etc). x = getattr(module, name) m = getattr(x, "__module__", None) if not m: return False return DottedIdentifier(m).startswith(self.name)
def check_xref(self, identifier, container): """ Check that ``identifier`` cross-references a proper symbol. Look in modules that we weren't explicitly asked to look in, if needed. """ if identifier in builtins.__dict__: return True def check_container(): if self.expanded_docindex.find(identifier, container) is not None: return True if isinstance(container, RoutineDoc): tcontainer = self.expanded_docindex.get_vardoc( container.canonical_name) doc = self.expanded_docindex.find(identifier, tcontainer) while (doc is not None and tcontainer not in (None, UNKNOWN) and tcontainer.overrides not in (None, UNKNOWN)): tcontainer = tcontainer.overrides doc = self.expanded_docindex.find(identifier, tcontainer) return doc is not None return False def check_defining_module(x): if x is None: return False defining_module_name = remove_epydoc_sym_suffix(str( x.defining_module.canonical_name)) if defining_module_name in ASSUME_MODULES_OK: return True if self.expanded_docindex.add_module(defining_module_name): if check_container(): return True return False if check_container(): return True if (isinstance(container, RoutineDoc) and identifier in container.all_args()): return True if check_defining_module(container): return True # If the user has imported foo.bar.baz as baz and now uses # ``baz.quux``, we need to add the module foo.bar.baz. for prefix in reversed(list(prefixes( DottedIdentifier(remove_epydoc_sym_suffix(identifier))))): if check_defining_module( self.docindex.find(str(prefix), container)): return True try: module = ModuleHandle.containing(identifier) except ImportError: pass else: if str(module.name) in ASSUME_MODULES_OK: return True if self.expanded_docindex.add_module(module): if check_container(): return True return False
def _from_modulename(cls, modulename): modulename = DottedIdentifier(modulename) try: return cls._cls_cache[modulename] except KeyError: pass self = object.__new__(cls) self.name = modulename cls._cls_cache[modulename] = self return self
def test_scan_for_import_issues_star_import_1(): code = dedent(""" import x1, y1 x1, x2 from m2 import * x3 """) missing, unused = scan_for_import_issues(code, parse_docstrings=True) assert missing == [(3, DottedIdentifier('x2'))] assert unused == [(2, Import('import y1'))]
def test_ModuleHandle_from_module_1(): m = ModuleHandle(logging.handlers) assert m == ModuleHandle("logging.handlers") assert m.name == DottedIdentifier("logging.handlers")
def test_ModuleHandle_dotted_1(): m = ModuleHandle("logging.handlers") assert m.name == DottedIdentifier("logging.handlers")
def test_ModuleHandle_1(): m = ModuleHandle("sys") assert m.name == DottedIdentifier("sys")