pyflyby 1.10.4__cp311-cp311-macosx_11_0_arm64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. pyflyby/__init__.py +61 -0
  2. pyflyby/__main__.py +9 -0
  3. pyflyby/_autoimp.py +2228 -0
  4. pyflyby/_cmdline.py +591 -0
  5. pyflyby/_comms.py +221 -0
  6. pyflyby/_dbg.py +1383 -0
  7. pyflyby/_dynimp.py +154 -0
  8. pyflyby/_fast_iter_modules.cpython-311-darwin.so +0 -0
  9. pyflyby/_file.py +771 -0
  10. pyflyby/_flags.py +230 -0
  11. pyflyby/_format.py +186 -0
  12. pyflyby/_idents.py +227 -0
  13. pyflyby/_import_sorting.py +165 -0
  14. pyflyby/_importclns.py +658 -0
  15. pyflyby/_importdb.py +535 -0
  16. pyflyby/_imports2s.py +643 -0
  17. pyflyby/_importstmt.py +723 -0
  18. pyflyby/_interactive.py +2113 -0
  19. pyflyby/_livepatch.py +793 -0
  20. pyflyby/_log.py +107 -0
  21. pyflyby/_modules.py +646 -0
  22. pyflyby/_parse.py +1396 -0
  23. pyflyby/_py.py +2165 -0
  24. pyflyby/_saveframe.py +1145 -0
  25. pyflyby/_saveframe_reader.py +471 -0
  26. pyflyby/_util.py +458 -0
  27. pyflyby/_version.py +8 -0
  28. pyflyby/autoimport.py +20 -0
  29. pyflyby/etc/pyflyby/canonical.py +10 -0
  30. pyflyby/etc/pyflyby/common.py +27 -0
  31. pyflyby/etc/pyflyby/forget.py +10 -0
  32. pyflyby/etc/pyflyby/mandatory.py +10 -0
  33. pyflyby/etc/pyflyby/numpy.py +156 -0
  34. pyflyby/etc/pyflyby/std.py +335 -0
  35. pyflyby/importdb.py +19 -0
  36. pyflyby/libexec/pyflyby/colordiff +34 -0
  37. pyflyby/libexec/pyflyby/diff-colorize +148 -0
  38. pyflyby/share/emacs/site-lisp/pyflyby.el +112 -0
  39. pyflyby-1.10.4.data/scripts/collect-exports +76 -0
  40. pyflyby-1.10.4.data/scripts/collect-imports +58 -0
  41. pyflyby-1.10.4.data/scripts/find-import +38 -0
  42. pyflyby-1.10.4.data/scripts/prune-broken-imports +34 -0
  43. pyflyby-1.10.4.data/scripts/pyflyby-diff +34 -0
  44. pyflyby-1.10.4.data/scripts/reformat-imports +27 -0
  45. pyflyby-1.10.4.data/scripts/replace-star-imports +37 -0
  46. pyflyby-1.10.4.data/scripts/saveframe +299 -0
  47. pyflyby-1.10.4.data/scripts/tidy-imports +170 -0
  48. pyflyby-1.10.4.data/scripts/transform-imports +47 -0
  49. pyflyby-1.10.4.dist-info/METADATA +605 -0
  50. pyflyby-1.10.4.dist-info/RECORD +53 -0
  51. pyflyby-1.10.4.dist-info/WHEEL +6 -0
  52. pyflyby-1.10.4.dist-info/entry_points.txt +4 -0
  53. pyflyby-1.10.4.dist-info/licenses/LICENSE.txt +19 -0
pyflyby/_autoimp.py ADDED
@@ -0,0 +1,2228 @@
1
+ # pyflyby/_autoimp.py.
2
+ # Copyright (C) 2011, 2012, 2013, 2014, 2015, 2018, 2019, 2024 Karl Chen.
3
+ # License: MIT http://opensource.org/licenses/MIT
4
+
5
+
6
+
7
+ from __future__ import annotations, print_function
8
+
9
+ import ast
10
+ import builtins
11
+ from collections.abc import Sequence
12
+ import contextlib
13
+ import copy
14
+
15
+ from pyflyby._file import FileText, Filename
16
+ from pyflyby._flags import CompilerFlags
17
+ from pyflyby._idents import (BadDottedIdentifierError,
18
+ DottedIdentifier, brace_identifiers)
19
+ from pyflyby._importdb import ImportDB
20
+ from pyflyby._importstmt import Import
21
+ from pyflyby._log import logger
22
+ from pyflyby._modules import ModuleHandle
23
+ from pyflyby._parse import (MatchAs, PythonBlock, _is_ast_str,
24
+ infer_compile_mode)
25
+
26
+ import sys
27
+ import types
28
+ from typing import (Any, Dict, List, Optional, Set, Tuple,
29
+ Union)
30
+
31
+
32
+ if sys.version_info >= (3, 13):
33
+ def f():
34
+ return sys._getframe().f_locals
35
+
36
+ FrameLocalsProxyType = type(f())
37
+ else:
38
+ FrameLocalsProxyType = dict
39
+
40
+ if sys.version_info >= (3, 12):
41
+ ATTRIBUTE_NAME = "value"
42
+ else:
43
+ ATTRIBUTE_NAME = "s"
44
+
45
+ if sys.version_info > (3, 11):
46
+ LOAD_SHIFT = 1
47
+ else:
48
+ LOAD_SHIFT = 0
49
+
50
+
51
+ if sys.version_info >= (3, 10):
52
+ from types import NoneType, EllipsisType
53
+ else:
54
+ NoneType = type(None)
55
+ EllipsisType = type(Ellipsis)
56
+
57
+
58
+ class _ClassScope(dict):
59
+ pass
60
+
61
+
62
+ def __repr__(self):
63
+ return "_ClassScope(" + repr(super()) + ")"
64
+
65
+
66
+ _builtins2 = {"__file__": None}
67
+
68
+
69
+ class ScopeStack(Sequence):
70
+ """
71
+ A stack of namespace scopes, as a tuple of ``dict`` s.
72
+
73
+ Each entry is a ``dict``.
74
+
75
+ Ordered from most-global to most-local.
76
+ Builtins are always included.
77
+ Duplicates are removed.
78
+ """
79
+
80
+ _cached_has_star_import = False
81
+
82
+ def __init__(self, arg, _class_delayed=None):
83
+ """
84
+ Interpret argument as a ``ScopeStack``.
85
+
86
+ :type arg:
87
+ ``ScopeStack``, ``dict``, ``list`` of ``dict``
88
+ :param arg:
89
+ Input namespaces
90
+ :rtype:
91
+ ``ScopeStack``
92
+ """
93
+ if isinstance(arg, ScopeStack):
94
+ scopes = list(arg._tup)
95
+ elif isinstance(arg, dict):
96
+ scopes = [arg]
97
+ elif isinstance(arg, (tuple, list)):
98
+ scopes = list(arg)
99
+ else:
100
+ raise TypeError(
101
+ "ScopeStack: expected a sequence of dicts; got a %s"
102
+ % (type(arg).__name__,))
103
+ if not len(scopes):
104
+ raise TypeError("ScopeStack: no scopes given")
105
+ if not all(isinstance(scope, (dict, FrameLocalsProxyType)) for scope in scopes):
106
+ raise TypeError("ScopeStack: Expected list of dict or FrameLocalsProxy objects; got a sequence of %r"
107
+ % ([type(x).__name__ for x in scopes]))
108
+ scopes = [builtins.__dict__, _builtins2] + scopes
109
+ result = []
110
+ seen = set()
111
+ # Keep only unique items, checking uniqueness by object identity.
112
+ for scope in scopes:
113
+ if id(scope) in seen:
114
+ continue
115
+ seen.add(id(scope))
116
+ result.append(scope)
117
+ tup = tuple(result)
118
+ self._tup = tup
119
+
120
+ # class name definitions scope may need to be delayed.
121
+ # so we store them separately, and if they are present in methods def, we can readd them
122
+ if _class_delayed is None:
123
+ _class_delayed = {}
124
+ self._class_delayed = _class_delayed
125
+
126
+ def __contains__(self, item):
127
+ if isinstance(item, DottedIdentifier):
128
+ item = item.name
129
+ if isinstance(item, str):
130
+ for sub in self:
131
+ if item in sub.keys():
132
+ return True
133
+
134
+ return False
135
+
136
+ def __getitem__(self, item):
137
+ if isinstance(item, slice):
138
+ return self.__class__(self._tup[item])
139
+ return self._tup[item]
140
+
141
+ def __len__(self):
142
+ return len(self._tup)
143
+
144
+ def _with_new_scope(
145
+ self,
146
+ *,
147
+ include_class_scopes: bool,
148
+ new_class_scope: bool,
149
+ unhide_classdef: bool,
150
+ ) -> ScopeStack:
151
+ """
152
+ Return a new ``ScopeStack`` with an additional empty scope.
153
+
154
+ :param include_class_scopes:
155
+ Whether to include previous scopes that are meant for ClassDefs.
156
+ :param new_class_scope:
157
+ Whether the new scope is for a ClassDef.
158
+ :param unhide_classdef:
159
+ Unhide class definitiion scope (when we enter a method)
160
+ :rtype:
161
+ ``ScopeStack``
162
+ """
163
+ if include_class_scopes:
164
+ scopes = tuple(self)
165
+ else:
166
+ scopes = tuple(s for s in self if not isinstance(s, _ClassScope))
167
+ new_scope: Union[_ClassScope, Dict[str, Any]]
168
+ if new_class_scope:
169
+ new_scope = _ClassScope()
170
+ else:
171
+ new_scope = {}
172
+ cls = type(self)
173
+ if unhide_classdef and self._class_delayed:
174
+ scopes = tuple([self._class_delayed]) + scopes
175
+ result = cls(scopes + (new_scope,), _class_delayed=self._class_delayed)
176
+ return result
177
+
178
+ def clone_top(self):
179
+ """
180
+ Return a new ``ScopeStack`` referencing the same namespaces as ``self``,
181
+ but cloning the topmost namespace (and aliasing the others).
182
+ """
183
+ scopes = list(self)
184
+ scopes[-1] = copy.copy(scopes[-1])
185
+ cls = type(self)
186
+ return cls(scopes)
187
+
188
+ def merged_to_two(self):
189
+ """
190
+ Return a 2-tuple of dicts.
191
+
192
+ These can be used for functions that take a ``globals`` and ``locals``
193
+ argument, such as ``eval``.
194
+
195
+ If there is only one entry, then return it twice.
196
+
197
+ If there are more than two entries, then create a new dict that merges
198
+ the more-global ones. The most-local stack will alias the dict from
199
+ the existing ScopeStack.
200
+
201
+ :rtype:
202
+ ``tuple`` of (``dict``, ``dict``)
203
+ """
204
+ assert len(self) >= 1
205
+ if len(self) == 1:
206
+ return (self[0], self[0])
207
+ if len(self) == 2:
208
+ return tuple(self)
209
+ d = {}
210
+ for scope in self[:-1]:
211
+ d.update(scope)
212
+ # Return as a 2-tuple. We don't cast the result to ScopeStack because
213
+ # it may add __builtins__ again, creating something of length 3.
214
+ return (d, self[-1])
215
+
216
+
217
+ def has_star_import(self) -> bool:
218
+ """
219
+ Return whether there are any star-imports in this ScopeStack.
220
+ Only relevant in AST-based static analysis mode.
221
+ """
222
+ if self._cached_has_star_import:
223
+ return True
224
+ if any('*' in scope for scope in self):
225
+ # There was a star import. Cache that fact before returning. We
226
+ # can cache a positive result because a star import can't be undone.
227
+ self._cached_has_star_import = True
228
+ return True
229
+ else:
230
+ # There was no star import yet. We can't cache that fact because
231
+ # there might be a star import later.
232
+ return False
233
+
234
+ def __repr__(self):
235
+ scopes_reprs = [
236
+ "{:2}".format(i) + " : " + repr(namespace)
237
+ for i, namespace in enumerate(self)
238
+ ][1:]
239
+
240
+ return (
241
+ "<{class_name} object at 0x{hex_id} with namespaces: [\n".format(
242
+ class_name=self.__class__.__name__, hex_id=id(self)
243
+ )
244
+ + " 0 : {builtins namespace elided.}\n"
245
+ + "\n".join(scopes_reprs)
246
+ + "\n]>"
247
+ )
248
+
249
+
250
+ def symbol_needs_import(fullname, namespaces):
251
+ """
252
+ Return whether ``fullname`` is a symbol that needs to be imported, given
253
+ the current namespace scopes.
254
+
255
+ A symbol needs importing if it is not previously imported or otherwise
256
+ assigned. ``namespaces`` normally includes builtins and globals as well as
257
+ symbols imported/assigned locally within the scope.
258
+
259
+ If the user requested "foo.bar.baz", and we see that "foo.bar" exists
260
+ and is not a module, we assume nothing under foo.bar needs import.
261
+ This is intentional because (1) the import would not match what is
262
+ already in the namespace, and (2) we don't want to do call
263
+ getattr(foo.bar, "baz"), since that could invoke code that is slow or
264
+ has side effects.
265
+
266
+ :type fullname:
267
+ ``DottedIdentifier``
268
+ :param fullname:
269
+ Fully-qualified symbol name, e.g. "os.path.join".
270
+ :type namespaces:
271
+ ``list`` of ``dict``
272
+ :param namespaces:
273
+ Stack of namespaces to search for existing items.
274
+ :rtype:
275
+ ``bool``
276
+ :return:
277
+ ``True`` if ``fullname`` needs import, else ``False``
278
+ """
279
+ namespaces = ScopeStack(namespaces)
280
+ fullname = DottedIdentifier(fullname)
281
+ partial_names = fullname.prefixes[::-1]
282
+ # Iterate over local scopes.
283
+ for ns_idx, ns in reversed(list(enumerate(namespaces))):
284
+ # Iterate over partial names: "foo.bar.baz.quux", "foo.bar.baz", ...
285
+ for partial_name in partial_names:
286
+ # Check if this partial name was imported/assigned in this
287
+ # scope. In the common case, there will only be one namespace
288
+ # in the namespace stack, i.e. the user globals.
289
+ try:
290
+ var = ns[str(partial_name)]
291
+ except KeyError:
292
+ continue
293
+ # If we're doing static analysis where we also care about which
294
+ # imports are unused, then mark the used ones now.
295
+ if isinstance(var, _UseChecker):
296
+ var.used = True
297
+ # Suppose the user accessed fullname="foo.bar.baz.quux" and
298
+ # suppose we see "foo.bar" was imported (or otherwise assigned) in
299
+ # the scope vars (most commonly this means it was imported
300
+ # globally). Let's check if foo.bar already has a "baz".
301
+ prefix_len = len(partial_name.parts)
302
+ suffix_parts = fullname.parts[prefix_len:]
303
+ pname = str(partial_name)
304
+ for part in suffix_parts:
305
+ # Check if the var so far is a module -- in fact that it's
306
+ # *the* module of a given name. That is, for var ==
307
+ # foo.bar.baz, check if var is sys.modules['foo.bar.baz']. We
308
+ # used to just check if isinstance(foo.bar.baz, ModuleType).
309
+ # However, that naive check is wrong for these situations:
310
+ # - A module that contains an import of anything other than a
311
+ # submodule with its exact name. For example, suppose
312
+ # foo.bar contains 'import sqlalchemy'.
313
+ # foo.bar.sqlalchemy is of ModuleType, but that doesn't
314
+ # mean that we could import foo.bar.sqlalchemy.orm.
315
+ # Similar case if foo.bar contains 'from . import baz as
316
+ # baz2'. Mistaking these doesn't break much, but might as
317
+ # well avoid an unnecessary import attempt.
318
+ # - A "proxy module". Suppose foo.bar replaces itself with
319
+ # an object with a __getattr__, using
320
+ # 'sys.modules[__name__] = ...' Submodules are still
321
+ # importable, but sys.modules['foo.bar'] would not be of
322
+ # type ModuleType.
323
+ if var is not sys.modules.get(pname, object()):
324
+ # The variable is not a module. (If this came from a
325
+ # local assignment then ``var`` will just be "None"
326
+ # here to indicate we know it was assigned but don't
327
+ # know about its type.) Thus nothing under it needs
328
+ # import.
329
+ logger.debug("symbol_needs_import(%r): %s is in namespace %d (under %r) and not a global module, so it doesn't need import", fullname, pname, ns_idx, partial_name)
330
+ return False
331
+ try:
332
+ var = getattr(var, part)
333
+ except AttributeError:
334
+ # We saw that "foo.bar" is imported, and is a module, but
335
+ # it does not have a "baz" attribute. Thus, as far as we
336
+ # know so far, foo.bar.baz requires import. But continue
337
+ # on to the next scope.
338
+ logger.debug("symbol_needs_import(%r): %s is a module in namespace %d (under %r), but has no %r attribute", fullname, pname, ns_idx, partial_name, part)
339
+ break # continue outer loop
340
+ pname = "%s.%s" % (pname, part)
341
+ else:
342
+ # We saw that "foo.bar" is imported, and checked that
343
+ # foo.bar has an attribute "baz", which has an
344
+ # attribute "quux" - so foo.bar.baz.quux does not need
345
+ # to be imported.
346
+ assert pname == str(fullname)
347
+ logger.debug("symbol_needs_import(%r): found it in namespace %d (under %r), so it doesn't need import", fullname, ns_idx, partial_name)
348
+ return False
349
+ # We didn't find any scope that defined the name. Therefore it needs
350
+ # import.
351
+ logger.debug(
352
+ "symbol_needs_import(%r): no match found in namespaces %s; it needs import",
353
+ fullname,
354
+ namespaces,
355
+ )
356
+ return True
357
+
358
+
359
+ class _UseChecker:
360
+ """
361
+ An object that can check whether it was used.
362
+ """
363
+
364
+ used: bool = False
365
+ name: str
366
+ source: str
367
+ lineno: int
368
+
369
+ def __init__(self, name: str, source: str, lineno: int):
370
+ self.name = name
371
+ self.source = source # generally an Import
372
+ self.lineno = lineno
373
+ logger.debug("Create _UseChecker : %r", self)
374
+
375
+ def __repr__(self):
376
+ return f"<{type(self).__name__}: name:{self.name!r} source:{self.source!r} lineno:{self.lineno} used:{self.used}>"
377
+
378
+
379
+ class _MissingImportFinder:
380
+ """
381
+ A helper class to be used only by `_find_missing_imports_in_ast`.
382
+
383
+ This class visits every AST node and collects symbols that require
384
+ importing. A symbol requires importing if it is not already imported or
385
+ otherwise defined/assigned in this scope.
386
+
387
+ For attributes like "foo.bar.baz", we need to be more sophisticated:
388
+
389
+ Suppose the user imports "foo.bar" and then accesses "foo.bar.baz.quux".
390
+ Baz may be already available just by importing foo.bar, or it may require
391
+ further import. We decide as follows. If foo.bar is not a module, then
392
+ we assume whatever's under it can't be imported. If foo.bar is a module
393
+ but does not have a 'baz' attribute, then it does require import.
394
+
395
+ """
396
+
397
+ scopestack: ScopeStack
398
+ _lineno: Optional[int]
399
+ missing_imports: List[Tuple[Optional[int], DottedIdentifier]]
400
+ parse_docstrings: bool
401
+ unused_imports: Optional[List[Tuple[int, str]]]
402
+ _deferred_load_checks: list[tuple[str, ScopeStack, Optional[int]]]
403
+
404
+ def __init__(self, scopestack, *, find_unused_imports:bool, parse_docstrings:bool):
405
+ """
406
+ Construct the AST visitor.
407
+
408
+ :type scopestack:
409
+ `ScopeStack`
410
+ :param scopestack:
411
+ Initial scope stack.
412
+ """
413
+ # Create a stack of namespaces. The caller should pass in a list that
414
+ # includes the globals dictionary. ScopeStack() will make sure this
415
+ # includes builtins.
416
+ _scopestack = ScopeStack(scopestack)
417
+
418
+ # Add an empty namespace to the stack. This facilitates adding stuff
419
+ # to scopestack[-1] without ever modifying user globals.
420
+ self.scopestack = _scopestack._with_new_scope(
421
+ include_class_scopes=False, new_class_scope=False, unhide_classdef=False
422
+ )
423
+
424
+ # Create data structure to hold the result.
425
+ # missing_imports is a list of (lineno, DottedIdentifier) tuples.
426
+ self.missing_imports = []
427
+
428
+ # unused_imports is a list of (lineno, Import) tuples, if enabled.
429
+ self.unused_imports = [] if find_unused_imports else None
430
+
431
+ self.parse_docstrings = parse_docstrings
432
+
433
+ # Function bodies that we need to check after defining names in this
434
+ # function scope.
435
+ self._deferred_load_checks = []
436
+
437
+ # Whether we're currently in a FunctionDef.
438
+ self._in_FunctionDef = False
439
+ # Current lineno.
440
+ self._lineno = None
441
+ self._in_class_def = 0
442
+
443
+ def find_missing_imports(self, node):
444
+ self._scan_node(node)
445
+ return sorted(set(imp for lineno,imp in self.missing_imports))
446
+
447
+ def _scan_node(self, node):
448
+ oldscopestack = self.scopestack
449
+ myglobals = self.scopestack[-1]
450
+ try:
451
+ self.visit(node)
452
+ self._finish_deferred_load_checks()
453
+ assert self.scopestack is oldscopestack
454
+ assert self.scopestack[-1] is myglobals
455
+ finally:
456
+ self.scopestack = oldscopestack
457
+
458
+ def scan_for_import_issues(self, codeblock: PythonBlock):
459
+ assert isinstance(codeblock, PythonBlock)
460
+ # See global `scan_for_import_issues`
461
+ if not isinstance(codeblock, PythonBlock):
462
+ codeblock = PythonBlock(codeblock)
463
+ node = codeblock.ast_node
464
+ self._scan_node(node)
465
+ # Get missing imports now, before handling docstrings. We don't want
466
+ # references in doctests to be noted as missing-imports. For now we
467
+ # just let the code accumulate into self.missing_imports and ignore
468
+ # the result.
469
+ logger.debug("unused: %r", self.unused_imports)
470
+ missing_imports = sorted(self.missing_imports)
471
+ if self.parse_docstrings and self.unused_imports is not None:
472
+ doctest_blocks = codeblock.get_doctests()
473
+ # Parse each doctest. Don't report missing imports in doctests,
474
+ # but do treat existing imports as 'used' if they are used in
475
+ # doctests. The linenos are currently wrong, but we don't use
476
+ # them so it's not important to fix.
477
+ for block in doctest_blocks:
478
+ # There are doctests. Parse them.
479
+ # Doctest blocks inherit the global scope after parsing all
480
+ # non-doctest code, and each doctest block individually creates a new
481
+ # scope (not shared between doctest blocks).
482
+ # TODO: Theoretically we should clone the entire scopestack,
483
+ # not just add a new scope, in case the doctest uses 'global'.
484
+ # Currently we don't support the 'global' keyword anyway so
485
+ # this doesn't matter yet, and it's uncommon to use 'global'
486
+ # in a doctest, so this is low priority to fix.
487
+ with self._NewScopeCtx(check_unused_imports=False):
488
+ self._scan_node(block.ast_node)
489
+ # Find literal brace identifiers like "... `Foo` ...".
490
+ # TODO: Do this inline: (1) faster; (2) can use proper scope of vars
491
+ # Once we do that, use _check_load() with new args
492
+ # check_missing_imports=False, check_unused_imports=True
493
+ literal_brace_identifiers = set(
494
+ iden
495
+ for f in codeblock.string_literals()
496
+ for iden in brace_identifiers(getattr(f, ATTRIBUTE_NAME))
497
+ )
498
+ if literal_brace_identifiers:
499
+ for ident in literal_brace_identifiers:
500
+ try:
501
+ ident = DottedIdentifier(ident)
502
+ except BadDottedIdentifierError:
503
+ continue
504
+ symbol_needs_import(ident, self.scopestack)
505
+ self._scan_unused_imports()
506
+ logger.debug("missing: %s, unused: %s", missing_imports, self.unused_imports)
507
+ return missing_imports, self.unused_imports
508
+
509
+ def visit(self, node):
510
+ """
511
+ Visit a node.
512
+
513
+ :type node:
514
+ ``ast.AST`` or ``list`` of ``ast.AST``
515
+ """
516
+ # Modification of ast.NodeVisitor.visit(). Support list inputs.
517
+ logger.debug("_MissingImportFinder.visit(%r)", node)
518
+ lineno = getattr(node, 'lineno', None)
519
+ if lineno:
520
+ self._lineno = lineno
521
+ if isinstance(node, list):
522
+ for item in node:
523
+ self.visit(item)
524
+ elif isinstance(node, ast.AST):
525
+ method = 'visit_' + node.__class__.__name__
526
+ if not hasattr(self, method):
527
+ logger.debug(
528
+ "_MissingImportFinder has no method %r, using generic_visit", method
529
+ )
530
+ if hasattr(self, method):
531
+ visitor = getattr(self, method)
532
+ else:
533
+ logger.debug("No method `%s`, using `generic_visit`", method)
534
+ visitor = self.generic_visit
535
+ return visitor(node)
536
+ else:
537
+ raise TypeError("unexpected %s" % (type(node).__name__,))
538
+
539
+ def generic_visit(self, node):
540
+ """
541
+ Generic visitor that visits all of the node's field values, in the
542
+ order declared by ``node._fields``.
543
+
544
+ Called if no explicit visitor function exists for a node.
545
+ """
546
+ # Modification of ast.NodeVisitor.generic_visit: recurse to visit()
547
+ # even for lists, and be more explicit about type checking.
548
+ for field, value in ast.iter_fields(node):
549
+ if isinstance(value, ast.AST):
550
+ self.visit(value)
551
+ elif isinstance(value, list):
552
+ if all(isinstance(v, str) for v in value):
553
+ pass
554
+ elif all(isinstance(v, ast.AST) for v in value):
555
+ self.visit(value)
556
+ else:
557
+ raise TypeError(
558
+ "unexpected %s" % (", ".join(type(v).__name__ for v in value))
559
+ )
560
+ elif isinstance(
561
+ value, (int, float, complex, str, NoneType, bytes, EllipsisType)
562
+ ):
563
+ pass
564
+ else:
565
+ raise TypeError(
566
+ "unexpected %s for %s.%s"
567
+ % (type(value).__name__, type(node).__name__, field))
568
+
569
+
570
+ @contextlib.contextmanager
571
+ def _NewScopeCtx(
572
+ self,
573
+ include_class_scopes=False,
574
+ new_class_scope=False,
575
+ unhide_classdef=False,
576
+ check_unused_imports=True,
577
+ ):
578
+ """
579
+ Context manager that temporarily pushes a new empty namespace onto the
580
+ stack of namespaces.
581
+ """
582
+ prev_scopestack = self.scopestack
583
+ new_scopestack = prev_scopestack._with_new_scope(
584
+ include_class_scopes=include_class_scopes,
585
+ new_class_scope=new_class_scope,
586
+ unhide_classdef=unhide_classdef,
587
+ )
588
+ self.scopestack = new_scopestack
589
+ try:
590
+ yield
591
+ finally:
592
+ logger.debug("throwing last scope from scopestack: %r", new_scopestack[-1])
593
+ for name, use_checker in new_scopestack[-1].items():
594
+ if use_checker and use_checker.used == False and check_unused_imports:
595
+ logger.debug(
596
+ "unused checker %r scopestack_depth %r",
597
+ use_checker,
598
+ len(self.scopestack),
599
+ )
600
+ self.unused_imports.append((use_checker.lineno, use_checker.source))
601
+ assert self.scopestack is new_scopestack
602
+ self.scopestack = prev_scopestack
603
+
604
+ @contextlib.contextmanager
605
+ def _UpScopeCtx(self):
606
+ """
607
+ Context manager that temporarily moves up one in the scope stack
608
+ """
609
+ if len(self.scopestack) < 2:
610
+ raise ValueError("There must be at least two scopes on the stack to move up a scope.")
611
+ prev_scopestack = self.scopestack
612
+ new_scopestack = prev_scopestack[:-1]
613
+ try:
614
+ self.scopestack = new_scopestack
615
+ yield
616
+ finally:
617
+ assert self.scopestack is new_scopestack
618
+ self.scopestack = prev_scopestack
619
+
620
+ def visit_Assign(self, node):
621
+ # Visit an assignment statement (lhs = rhs). This implementation of
622
+ # visit_Assign is just like the generic one, but we make sure we visit
623
+ # node.value (RHS of assignment operator), then node.targets (LHS of
624
+ # assignment operator). The default would have been to visit LHS,
625
+ # then RHS. The reason we need to visit RHS first is the following.
626
+ # If the code is 'foo = foo + 1', we want to first process the Load
627
+ # for foo (RHS) before we process the Store for foo (LHS). If we
628
+ # visited LHS then RHS, we would have a bug in the following sample
629
+ # code:
630
+ # from bar import foo # L1
631
+ # foo = foo + 1 # L2
632
+ # The good RHS-then-LHS visit-order would see the Load('foo') on L2,
633
+ # understand that it got used before the Store('foo') overwrote it.
634
+ # The bad LHS-then-RHS visit-order would visit Store('foo') on L2, and
635
+ # think that foo was never referenced before it was overwritten, and
636
+ # therefore think that the 'import foo' on L1 could be removed.
637
+ self.visit(node.value)
638
+ self.visit(node.targets)
639
+ self._visit__all__(node)
640
+
641
+ def _visit__all__(self, node):
642
+ if self._in_FunctionDef:
643
+ return
644
+ if (len(node.targets) == 1 and isinstance(node.targets[0], ast.Name)
645
+ and node.targets[0].id == '__all__'):
646
+ if not isinstance(node.value, (ast.List, ast.Tuple)):
647
+ logger.warning("Don't know how to handle __all__ as (%s)" % node.value)
648
+ return
649
+ if not all(_is_ast_str(e) for e in node.value.elts):
650
+ logger.warning("Don't know how to handle __all__ with list elements other than str")
651
+ return
652
+ for e in node.value.elts:
653
+ if sys.version_info > (3,10):
654
+ self._visit_Load_defered_global(e.value)
655
+ else:
656
+ self._visit_Load_defered_global(e.s)
657
+
658
+ def visit_ClassDef(self, node):
659
+ logger.debug("visit_ClassDef(%r)", node)
660
+ if sys.version_info > (3,12):
661
+ # we don't visit type_params, so autoimport won't work yet for type annotations
662
+ assert node._fields == ('name', 'bases', 'keywords', 'body', 'decorator_list', 'type_params'), node._fields
663
+ else:
664
+ assert node._fields == ('name', 'bases', 'keywords', 'body', 'decorator_list'), node._fields
665
+ self.visit(node.bases)
666
+ self.visit(node.decorator_list)
667
+ # The class's name is only visible to others (not to the body to the
668
+ # class), but is accessible in the methods themselves. See https://github.com/deshaw/pyflyby/issues/147
669
+ self.visit(node.keywords)
670
+
671
+ # we only care about the first defined class,
672
+ # we don't detect issues with nested classes.
673
+ if self._in_class_def == 0:
674
+ self.scopestack._class_delayed[node.name] = None
675
+ with self._NewScopeCtx(new_class_scope=True):
676
+ self._in_class_def += 1
677
+ self._visit_Store(node.name)
678
+ self.visit(node.body)
679
+ self._in_class_def -= 1
680
+ assert self._in_class_def >= 0
681
+ self._remove_from_missing_imports(node.name)
682
+ self._visit_Store(node.name)
683
+
684
+ def visit_AsyncFunctionDef(self, node):
685
+ return self.visit_FunctionDef(node)
686
+
687
+ def visit_FunctionDef(self, node):
688
+ # Visit a function definition.
689
+ # - Visit args and decorator list normally.
690
+ # - Visit function body in a special mode where we defer checking
691
+ # loads until later, and don't load names from the parent ClassDef
692
+ # scope.
693
+ # - Store the name in the current scope (but not visibly to
694
+ # args/decorator_list).
695
+ if sys.version_info > (3, 12):
696
+ # we don't visit type_params, so autoimport won't work yet for type annotations
697
+ assert node._fields == ('name', 'args', 'body', 'decorator_list', 'returns', 'type_comment', 'type_params'), node._fields
698
+ else:
699
+ assert node._fields == ('name', 'args', 'body', 'decorator_list', 'returns', 'type_comment'), node._fields
700
+ with self._NewScopeCtx(include_class_scopes=True):
701
+ # we want `__class__` to only be defined in
702
+ # methods and not class body
703
+ if self._in_class_def:
704
+ self.scopestack[-1]["__class__"] = None # we just need to to be defined
705
+ self.visit(node.decorator_list)
706
+ self.visit(node.args)
707
+ if node.returns:
708
+ self.visit(node.returns)
709
+ self._visit_typecomment(node.type_comment)
710
+ old_in_FunctionDef = self._in_FunctionDef
711
+ self._in_FunctionDef = True
712
+ with self._NewScopeCtx(unhide_classdef=True):
713
+ if not self._in_class_def:
714
+ self._visit_Store(node.name)
715
+ self.visit(node.body)
716
+ self._in_FunctionDef = old_in_FunctionDef
717
+ self._visit_Store(node.name)
718
+
719
+ def visit_Lambda(self, node):
720
+ # Like FunctionDef, but without the decorator_list or name.
721
+ assert node._fields == ('args', 'body'), node._fields
722
+ with self._NewScopeCtx(include_class_scopes=True):
723
+ self.visit(node.args)
724
+ old_in_FunctionDef = self._in_FunctionDef
725
+ self._in_FunctionDef = True
726
+ with self._NewScopeCtx():
727
+ self.visit(node.body)
728
+ self._in_FunctionDef = old_in_FunctionDef
729
+
730
+ def _visit_typecomment(self, typecomment: str) -> None:
731
+ """
732
+ Warning, when a type comment the node is a string, not an ast node.
733
+ We also get two types of type comments:
734
+
735
+
736
+ The signature one just after a function definition
737
+
738
+ def foo(a):
739
+ # type: int -> None
740
+ pass
741
+
742
+ And the variable annotation ones:
743
+
744
+ def foo(a #type: int
745
+ ):
746
+ pass
747
+
748
+ ast parse "func_type" mode only support the first one.
749
+
750
+ """
751
+ if typecomment is None:
752
+ return
753
+ node: Union[ast.Module, ast.FunctionType]
754
+ if '->' in typecomment:
755
+ node = ast.parse(typecomment, mode='func_type')
756
+ else:
757
+ node = ast.parse(typecomment)
758
+
759
+ self.visit(node)
760
+
761
+ def visit_arguments(self, node) -> None:
762
+ assert node._fields == ('posonlyargs', 'args', 'vararg', 'kwonlyargs', 'kw_defaults', 'kwarg', 'defaults'), node._fields
763
+ # Argument/parameter list. Note that the defaults should be
764
+ # considered "Load"s from the upper scope, and the argument names are
765
+ # "Store"s in the function scope.
766
+
767
+ # E.g. consider:
768
+ # def f(x=y, y=x): pass
769
+ # Both x and y should be considered undefined (unless they were indeed
770
+ # defined before the def).
771
+ # We assume visit_arguments is always called from a _NewScopeCtx
772
+ # context
773
+ with self._UpScopeCtx():
774
+ self.visit(node.defaults)
775
+ for i in node.kw_defaults:
776
+ if i:
777
+ self.visit(i)
778
+ # Store arg names.
779
+ self.visit(node.args)
780
+ self.visit(node.kwonlyargs)
781
+ self.visit(node.posonlyargs)
782
+ # may be None.
783
+ if node.vararg:
784
+ self.visit(node.vararg)
785
+ else:
786
+ self._visit_Store(node.vararg)
787
+ if node.kwarg:
788
+ self.visit(node.kwarg)
789
+ else:
790
+ self._visit_Store(node.kwarg)
791
+
792
+ def visit_ExceptHandler(self, node) -> None:
793
+ assert node._fields == ('type', 'name', 'body')
794
+ if node.type:
795
+ self.visit(node.type)
796
+ if node.name:
797
+ self._visit_Store(node.name)
798
+ self.visit(node.body)
799
+
800
+ def visit_Dict(self, node):
801
+ assert node._fields == ('keys', 'values')
802
+ # In Python 3, keys can be None, indicating a ** expression
803
+ for key in node.keys:
804
+ if key:
805
+ self.visit(key)
806
+ self.visit(node.values)
807
+
808
+ def visit_comprehension(self, node):
809
+ # Visit a "comprehension" node, which is a component of list
810
+ # comprehensions and generator expressions.
811
+ self.visit(node.iter)
812
+ def visit_target(target):
813
+ if isinstance(target, ast.Name):
814
+ self._visit_Store(target.id)
815
+ elif isinstance(target, (ast.Tuple, ast.List)):
816
+ for elt in target.elts:
817
+ visit_target(elt)
818
+ else:
819
+ # Unusual stuff like:
820
+ # [f(x) for x[0] in mylist]
821
+ # [f(x) for x.foo in mylist]
822
+ # [f(x) for x.foo[0].foo in mylist]
823
+ self.visit(target)
824
+ visit_target(node.target)
825
+ self.visit(node.ifs)
826
+
827
+ def visit_ListComp(self, node):
828
+ # Visit a list comprehension node.
829
+ # This is basically the same as the generic visit, except that we
830
+ # visit the comprehension node(s) before the elt node.
831
+ # (generic_visit() would visit the elt first, because that comes first
832
+ # in ListComp._fields).
833
+ # For Python2, we intentionally don't enter a new scope here, because
834
+ # a list comprehensive _does_ leak variables out of its scope (unlike
835
+ # generator expressions).
836
+ # For Python3, we do need to enter a new scope here.
837
+ with self._NewScopeCtx(include_class_scopes=True):
838
+ self.visit(node.generators)
839
+ self.visit(node.elt)
840
+
841
+ def visit_DictComp(self, node):
842
+ # Visit a dict comprehension node.
843
+ # This is similar to the generic visit, except:
844
+ # - We visit the comprehension node(s) before the elt node.
845
+ # - We create a new scope for the variables.
846
+ # We do enter a new scope. A dict comprehension
847
+ # does _not_ leak variables out of its scope (unlike py2 list
848
+ # comprehensions).
849
+ with self._NewScopeCtx(include_class_scopes=True):
850
+ self.visit(node.generators)
851
+ self.visit(node.key)
852
+ self.visit(node.value)
853
+
854
+ def visit_SetComp(self, node):
855
+ # Visit a set comprehension node.
856
+ # We do enter a new scope. A set comprehension
857
+ # does _not_ leak variables out of its scope (unlike py2 list
858
+ # comprehensions).
859
+ with self._NewScopeCtx(include_class_scopes=True):
860
+ self.visit(node.generators)
861
+ self.visit(node.elt)
862
+
863
+ def visit_GeneratorExp(self, node):
864
+ # Visit a generator expression node.
865
+ # We do enter a new scope. A generator
866
+ # expression does _not_ leak variables out of its scope (unlike py2
867
+ # list comprehensions).
868
+ with self._NewScopeCtx(include_class_scopes=True):
869
+ self.visit(node.generators)
870
+ self.visit(node.elt)
871
+
872
+ def visit_ImportFrom(self, node):
873
+ modulename = "." * node.level + (node.module or "")
874
+ logger.debug("visit_ImportFrom(%r, ...)", modulename)
875
+ for alias_node in node.names:
876
+ self.visit_alias(alias_node, modulename)
877
+
878
+ def visit_alias(self, node, modulename=None):
879
+ # Visit an import alias node.
880
+ # TODO: Currently we treat 'import foo' the same as if the user did
881
+ # 'foo = 123', i.e. we treat it as a black box (non-module). This is
882
+ # to avoid actually importing it yet. But this means we won't know
883
+ # whether foo.bar is available so we won't auto-import it. Maybe we
884
+ # should give up on not importing it and just import it in a scratch
885
+ # namespace, so we can check.
886
+ self._visit_StoreImport(node, modulename)
887
+ self.generic_visit(node)
888
+
889
+ if sys.version_info >= (3,10):
890
+ def visit_match_case(self, node:ast.match_case):
891
+ logger.debug("visit_match_case(%r)", node)
892
+ return self.generic_visit(node)
893
+
894
+ def visit_Match(self, node:ast.Match):
895
+ logger.debug("visit_Match(%r)", node)
896
+ return self.generic_visit(node)
897
+
898
+ def visit_MatchMapping(self, node:ast.MatchMapping):
899
+ logger.debug("visit_MatchMapping(%r)", node)
900
+ return self.generic_visit(node)
901
+
902
+ def visit_MatchAs(self, node:MatchAs):
903
+ logger.debug("visit_MatchAs(%r)", node)
904
+ if node.name is None:
905
+ return
906
+ isinstance(node.name, str), node.name
907
+ return self._visit_Store(node.name)
908
+
909
+ def visit_Call(self, node:ast.Call):
910
+ logger.debug("visit_Call(%r)", node)
911
+ return self.generic_visit(node)
912
+
913
+ def visit_Pass(self, node:ast.Pass):
914
+ logger.debug("visit_Pass(%r)", node)
915
+ return self.generic_visit(node)
916
+
917
+ def visit_Constant(self, node:ast.Constant):
918
+ logger.debug("visit_Constant(%r)", node)
919
+ return self.generic_visit(node)
920
+
921
+ def visit_Module(self, node:ast.Module):
922
+ logger.debug("visit_Module(%r)", node)
923
+ return self.generic_visit(node)
924
+
925
+ def visit_Expr(self, node:ast.Expr):
926
+ logger.debug("visit_Expr(%r)", node)
927
+ return self.generic_visit(node)
928
+
929
+
930
+ def visit_Name(self, node):
931
+ logger.debug("visit_Name(%r)", node.id)
932
+ self._visit_fullname(node.id, node.ctx)
933
+
934
+ def visit_arg(self, node):
935
+ assert node._fields == ('arg', 'annotation', 'type_comment'), node._fields
936
+ if node.annotation:
937
+ self.visit(node.annotation)
938
+ # Treat it like a Name node would from Python 2
939
+ self._visit_fullname(node.arg, ast.Param())
940
+ self._visit_typecomment(node.type_comment)
941
+
942
+ def visit_Attribute(self, node):
943
+ name_revparts = []
944
+ n = node
945
+ while isinstance(n, ast.Attribute):
946
+ name_revparts.append(n.attr)
947
+ n = n.value
948
+ if not isinstance(n, ast.Name):
949
+ # Attribute of a non-symbol, e.g. (a+b).c
950
+ # We do nothing about "c", but we do recurse on (a+b) since those
951
+ # may have symbols we care about.
952
+ self.generic_visit(node)
953
+ return
954
+ name_revparts.append(n.id)
955
+ name_parts = name_revparts[::-1]
956
+ fullname = ".".join(name_parts)
957
+ logger.debug("visit_Attribute(%r): fullname=%r, ctx=%r", node.attr, fullname, node.ctx)
958
+ self._visit_fullname(fullname, node.ctx)
959
+
960
+ def _visit_fullname(self, fullname, ctx):
961
+ if isinstance(ctx, (ast.Store, ast.Param)):
962
+ self._visit_Store(fullname)
963
+ elif isinstance(ctx, ast.Load):
964
+ self._visit_Load(fullname)
965
+
966
+ def _visit_StoreImport(self, node, modulename):
967
+ name = node.asname or node.name
968
+ logger.debug("_visit_StoreImport(asname=%r, name=%r)", node.asname, node.name)
969
+ is_star = node.name == "*"
970
+ if is_star:
971
+ logger.debug("Got star import: line %s: 'from %s import *'",
972
+ self._lineno, modulename)
973
+ if not node.asname and not is_star:
974
+ # Handle leading prefixes so we don't think they're unused
975
+ for prefix in DottedIdentifier(node.name).prefixes[:-1]:
976
+ self._visit_Store(str(prefix), None)
977
+ if self.unused_imports is None or is_star or modulename == "__future__":
978
+ value = None
979
+ else:
980
+ imp = Import.from_split((modulename, node.name, name))
981
+ logger.debug("_visit_StoreImport(): imp = %r", imp)
982
+ # Keep track of whether we've used this import.
983
+ value = _UseChecker(name, imp, self._lineno)
984
+ self._visit_Store(name, value)
985
+
986
+ def _visit_Store(self, fullname: str, value: Optional[_UseChecker] = None):
987
+ """
988
+ Visit a Store action, check for unused import
989
+ and add current value to the last scope.
990
+ """
991
+ assert isinstance(value, (_UseChecker, type(None)))
992
+ logger.debug("_visit_Store(%r)", fullname)
993
+ if fullname is None:
994
+ return
995
+ scope = self.scopestack[-1]
996
+ if isinstance(fullname, ast.arg):
997
+ fullname = fullname.arg
998
+ if self.unused_imports is not None:
999
+ if fullname != '*':
1000
+ # If we're storing "foo.bar.baz = 123", then "foo" and
1001
+ # "foo.bar" have now been used and the import should not be
1002
+ # removed.
1003
+ for ancestor in DottedIdentifier(fullname).prefixes[:-1]:
1004
+ if symbol_needs_import(ancestor, self.scopestack):
1005
+ m = (self._lineno, DottedIdentifier(fullname, scope_info=self._get_scope_info()))
1006
+ if m not in self.missing_imports:
1007
+ self.missing_imports.append(m)
1008
+ # If we're redefining something, and it has not been used, then
1009
+ # record it as unused.
1010
+ oldvalue = scope.get(fullname)
1011
+ if isinstance(oldvalue, _UseChecker) and not oldvalue.used:
1012
+ logger.debug("Adding to unused %s", oldvalue)
1013
+ self.unused_imports.append((oldvalue.lineno, oldvalue.source))
1014
+ scope[fullname] = value
1015
+
1016
+ def _remove_from_missing_imports(self, fullname):
1017
+ for missing_import in self.missing_imports:
1018
+ # If it was defined inside a class method, then it wouldn't have been added to
1019
+ # the missing imports anyways (except in that case of annotations)
1020
+ # See the following tests:
1021
+ # - tests.test_autoimp.test_method_reference_current_class
1022
+ # - tests.test_autoimp.test_find_missing_imports_class_name_1
1023
+ # - tests.test_autoimp.test_scan_for_import_issues_class_defined_after_use
1024
+ scopestack = missing_import[1].scope_info['scopestack']
1025
+ in_class_scope = isinstance(scopestack[-1], _ClassScope)
1026
+ inside_class = missing_import[1].scope_info.get('_in_class_def')
1027
+ if missing_import[1].startswith(fullname):
1028
+ if in_class_scope or not inside_class:
1029
+ self.missing_imports.remove(missing_import)
1030
+
1031
+ def _get_scope_info(self):
1032
+ return {
1033
+ "scopestack": self.scopestack,
1034
+ "_in_class_def": self._in_class_def,
1035
+ }
1036
+
1037
+ def visit_Delete(self, node):
1038
+ scope = self.scopestack[-1]
1039
+ for target in node.targets:
1040
+ if isinstance(target, ast.Name):
1041
+ # 'del foo'
1042
+ if target.id not in scope:
1043
+ # 'del x' without 'x' in current scope. Should we warn?
1044
+ continue
1045
+ del scope[target.id]
1046
+ elif isinstance(target, ast.Attribute):
1047
+ # 'del foo.bar.baz', 'del foo().bar', etc
1048
+ # We ignore the 'del ...bar' part and just visit the
1049
+ # left-hand-side of the delattr. We need to do this explicitly
1050
+ # instead of relying on a generic_visit on ``node`` itself.
1051
+ # Reason: We want visit_Attribute to process a getattr for
1052
+ # 'foo.bar'.
1053
+ self.visit(target.value)
1054
+ else:
1055
+ # 'del foo.bar[123]' (ast.Subscript), etc.
1056
+ # We can generically-visit the entire target node here.
1057
+ self.visit(target)
1058
+ # Don't call generic_visit(node) here. Reason: We already visit the
1059
+ # parts above, if relevant.
1060
+
1061
+ def _visit_Load_defered_global(self, fullname:str):
1062
+ """
1063
+ Some things will be resolved in global scope later.
1064
+ """
1065
+ assert isinstance(fullname, str), fullname
1066
+ logger.debug("_visit_Load_defered_global(%r)", fullname)
1067
+ if symbol_needs_import(fullname, self.scopestack):
1068
+ data = (fullname, self.scopestack, self._lineno)
1069
+ self._deferred_load_checks.append(data)
1070
+
1071
+
1072
+ def _visit_Load_defered(self, fullname):
1073
+ logger.debug("_visit_Load_defered(%r)", fullname)
1074
+ if symbol_needs_import(fullname, self.scopestack):
1075
+ data = (fullname, self.scopestack.clone_top(), self._lineno)
1076
+ self._deferred_load_checks.append(data)
1077
+
1078
+ def _visit_Load_immediate(self, fullname):
1079
+ logger.debug("_visit_Load_immediate(%r)", fullname)
1080
+ self._check_load(fullname, self.scopestack, self._lineno)
1081
+
1082
+
1083
+
1084
+ def _visit_Load(self, fullname):
1085
+ logger.debug("_visit_Load(%r)", fullname)
1086
+ if self._in_FunctionDef:
1087
+ self._visit_Load_defered(fullname)
1088
+ # We're in a FunctionDef. We need to defer checking whether this
1089
+ # references undefined names. The reason is that globals (or
1090
+ # stores in a parent function scope) may be stored later.
1091
+ # For example, bar() is defined later after the body of foo(), but
1092
+ # still available to foo() when it is called:
1093
+ # def foo():
1094
+ # return bar()
1095
+ # def bar():
1096
+ # return 42
1097
+ # foo()
1098
+ # To support this, we clone the top of the scope stack and alias
1099
+ # the other scopes in the stack. Later stores in the same scope
1100
+ # shouldn't count, e.g. x should be considered undefined in the
1101
+ # following example:
1102
+ # def foo():
1103
+ # print x
1104
+ # x = 1
1105
+ # On the other hand, we intentionally alias the other scopes
1106
+ # rather than cloning them, because the point is to allow them to
1107
+ # be modified until we do the check at the end.
1108
+ self._visit_Load_defered(fullname)
1109
+
1110
+ else:
1111
+ # We're not in a FunctionDef. Deferring would give us the same
1112
+ # result; we do the check now to avoid the overhead of cloning the
1113
+ # stack.
1114
+ self._visit_Load_immediate(fullname)
1115
+
1116
+ def _check_load(self, fullname, scopestack, lineno):
1117
+ """
1118
+ Check if the symbol needs import. (As a side effect, if the object
1119
+ is a _UseChecker, this will mark it as used.
1120
+
1121
+ TODO: It would be
1122
+ better to refactor symbol_needs_import so that it just returns the
1123
+ object it found, and we mark it as used here.)
1124
+ """
1125
+ fullname = DottedIdentifier(fullname, scope_info=self._get_scope_info())
1126
+ if symbol_needs_import(fullname, scopestack) and not scopestack.has_star_import():
1127
+ if (lineno, fullname) not in self.missing_imports:
1128
+ self.missing_imports.append((lineno, fullname))
1129
+
1130
+ def _finish_deferred_load_checks(self):
1131
+ for fullname, scopestack, lineno in self._deferred_load_checks:
1132
+ self._check_load(fullname, scopestack, lineno)
1133
+ self._deferred_load_checks = []
1134
+
1135
+ def _scan_unused_imports(self):
1136
+ # If requested, then check which of our imports were unused.
1137
+ # For now we only scan the top level. If we wanted to support
1138
+ # non-global unused-import checking, then we should check this
1139
+ # whenever popping a scopestack.
1140
+ unused_imports = self.unused_imports
1141
+ if unused_imports is None:
1142
+ return
1143
+ scope = self.scopestack[-1]
1144
+ for name, value in scope.items():
1145
+ if not isinstance(value, _UseChecker):
1146
+ continue
1147
+ if value.used:
1148
+ continue
1149
+ logger.debug("Also Adding to usunsed import: %s ", value)
1150
+ unused_imports.append(( value.lineno, value.source ))
1151
+ unused_imports.sort()
1152
+
1153
+
1154
+ def scan_for_import_issues(
1155
+ codeblock: PythonBlock,
1156
+ find_unused_imports: bool = True,
1157
+ parse_docstrings: bool = False,
1158
+ ):
1159
+ """
1160
+ Find missing and unused imports, by lineno.
1161
+
1162
+ >>> arg = "import numpy, aa.bb as cc\\nnumpy.arange(x)\\narange(x)"
1163
+ >>> missing, unused = scan_for_import_issues(arg)
1164
+ >>> missing
1165
+ [(2, DottedIdentifier('x')), (3, DottedIdentifier('arange')), (3, DottedIdentifier('x'))]
1166
+ >>> unused
1167
+ [(1, Import('from aa import bb as cc'))]
1168
+
1169
+ :type codeblock:
1170
+ ``PythonBlock``
1171
+ :type namespaces:
1172
+ ``dict`` or ``list`` of ``dict``
1173
+ :param parse_docstrings:
1174
+ Whether to parse docstrings.
1175
+ Compare the following examples. When parse_docstrings=True, 'bar' is
1176
+ not considered unused because there is a string that references it in
1177
+ braces::
1178
+
1179
+ >>> scan_for_import_issues("import foo as bar, baz\\n'{bar}'\\n")
1180
+ ([], [(1, Import('import baz')), (1, Import('import foo as bar'))])
1181
+ >>> scan_for_import_issues("import foo as bar, baz\\n'{bar}'\\n", parse_docstrings=True)
1182
+ ([], [(1, Import('import baz'))])
1183
+
1184
+ """
1185
+ logger.debug("global scan_for_import_issues()")
1186
+ if not isinstance(codeblock, PythonBlock):
1187
+ codeblock = PythonBlock(codeblock)
1188
+ namespaces = ScopeStack([{}])
1189
+ finder = _MissingImportFinder(namespaces,
1190
+ find_unused_imports=find_unused_imports,
1191
+ parse_docstrings=parse_docstrings)
1192
+ return finder.scan_for_import_issues(codeblock)
1193
+
1194
+
1195
+ def _find_missing_imports_in_ast(node, namespaces):
1196
+ """
1197
+ Find missing imports in an AST node.
1198
+ Helper function to `find_missing_imports`.
1199
+
1200
+ >>> node = ast.parse("import numpy; numpy.arange(x) + arange(x)")
1201
+ >>> _find_missing_imports_in_ast(node, [{}])
1202
+ [DottedIdentifier('arange'), DottedIdentifier('x')]
1203
+
1204
+ :type node:
1205
+ ``ast.AST``
1206
+ :type namespaces:
1207
+ ``dict`` or ``list`` of ``dict``
1208
+ :rtype:
1209
+ ``list`` of ``DottedIdentifier``
1210
+ """
1211
+ if not isinstance(node, ast.AST):
1212
+ raise TypeError
1213
+ # Traverse the abstract syntax tree.
1214
+ if logger.debug_enabled:
1215
+ logger.debug("ast=%s", ast.dump(node))
1216
+ return _MissingImportFinder(
1217
+ namespaces,
1218
+ find_unused_imports=False,
1219
+ parse_docstrings=False).find_missing_imports(node)
1220
+
1221
+ # TODO: maybe we should replace _find_missing_imports_in_ast with
1222
+ # _find_missing_imports_in_code(compile(node)). The method of parsing opcodes
1223
+ # is simpler, because Python takes care of the scoping issue for us and we
1224
+ # don't have to worry about locals. It does, however, depend on CPython
1225
+ # implementation details, whereas the AST is well-defined by the language.
1226
+
1227
+
1228
+ def _find_missing_imports_in_code(co, namespaces):
1229
+ """
1230
+ Find missing imports in a code object.
1231
+ Helper function to `find_missing_imports`.
1232
+
1233
+ >>> f = lambda: foo.bar(x) + baz(y)
1234
+ >>> [str(m) for m in _find_missing_imports_in_code(f.__code__, [{}])]
1235
+ ['baz', 'foo.bar', 'x', 'y']
1236
+
1237
+ >>> f = lambda x: (lambda: x+y)
1238
+ >>> _find_missing_imports_in_code(f.__code__, [{}])
1239
+ [DottedIdentifier('y')]
1240
+
1241
+ :type co:
1242
+ ``types.CodeType``
1243
+ :type namespaces:
1244
+ ``dict`` or ``list`` of ``dict``
1245
+ :rtype:
1246
+ ``list`` of ``str``
1247
+ """
1248
+ loads_without_stores = set()
1249
+ _find_loads_without_stores_in_code(co, loads_without_stores)
1250
+ missing_imports = [
1251
+ DottedIdentifier(fullname) for fullname in sorted(loads_without_stores)
1252
+ if symbol_needs_import(fullname, namespaces)
1253
+ ]
1254
+ return missing_imports
1255
+
1256
+
1257
+ def _find_loads_without_stores_in_code(co, loads_without_stores):
1258
+ """
1259
+ Find global LOADs without corresponding STOREs, by disassembling code.
1260
+ Recursive helper for `_find_missing_imports_in_code`.
1261
+
1262
+ :type co:
1263
+ ``types.CodeType``
1264
+ :param co:
1265
+ Code object, e.g. ``function.__code__``
1266
+ :type loads_without_stores:
1267
+ ``set``
1268
+ :param loads_without_stores:
1269
+ Mutable set to which we add loads without stores.
1270
+ :return:
1271
+ ``None``
1272
+ """
1273
+ if not isinstance(co, types.CodeType):
1274
+ raise TypeError(
1275
+ "_find_loads_without_stores_in_code(): expected a CodeType; got a %s"
1276
+ % (type(co).__name__,))
1277
+ # Initialize local constants for fast access.
1278
+ from opcode import EXTENDED_ARG, opmap
1279
+
1280
+ LOAD_ATTR = opmap['LOAD_ATTR']
1281
+ # LOAD_METHOD is _supposed_ to be removed in 3.12 but still present in opmap
1282
+ # if sys.version_info < (3, 12):
1283
+ LOAD_METHOD = opmap['LOAD_METHOD']
1284
+ # endif
1285
+ LOAD_GLOBAL = opmap['LOAD_GLOBAL']
1286
+ LOAD_NAME = opmap['LOAD_NAME']
1287
+ STORE_ATTR = opmap['STORE_ATTR']
1288
+ STORE_GLOBAL = opmap['STORE_GLOBAL']
1289
+ STORE_NAME = opmap['STORE_NAME']
1290
+
1291
+ if sys.version_info > (3, 11):
1292
+ CACHE = opmap["CACHE"]
1293
+ else:
1294
+ CACHE = object()
1295
+ # Keep track of the partial name so far that started with a LOAD_GLOBAL.
1296
+ # If ``pending`` is not None, then it is a list representing the name
1297
+ # components we've seen so far.
1298
+ pending = None
1299
+ # Disassemble the code. Look for LOADs and STOREs. This code is based on
1300
+ # ``dis.disassemble``.
1301
+ #
1302
+ # Scenarios:
1303
+ #
1304
+ # * Function-level load a toplevel global
1305
+ # def f():
1306
+ # aa
1307
+ # => LOAD_GLOBAL; other (not LOAD_ATTR or STORE_ATTR)
1308
+ # * Function-level load an attribute of global
1309
+ # def f():
1310
+ # aa.bb.cc
1311
+ # => LOAD_GLOBAL; LOAD_ATTR; LOAD_ATTR; other
1312
+ # * Function-level store a toplevel global
1313
+ # def f():
1314
+ # global aa
1315
+ # aa = 42
1316
+ # => STORE_GLOBAL
1317
+ # * Function-level store an attribute of global
1318
+ # def f():
1319
+ # aa.bb.cc = 42
1320
+ # => LOAD_GLOBAL, LOAD_ATTR, STORE_ATTR
1321
+ # * Function-level load a local
1322
+ # def f():
1323
+ # aa = 42
1324
+ # return aa
1325
+ # => LOAD_FAST or LOAD_NAME
1326
+ # * Function-level store a local
1327
+ # def f():
1328
+ # aa = 42
1329
+ # => STORE_FAST or STORE_NAME
1330
+ # * Function-level load an attribute of a local
1331
+ # def f():
1332
+ # aa = 42
1333
+ # return aa.bb.cc
1334
+ # => LOAD_FAST; LOAD_ATTR; LOAD_ATTR
1335
+ # * Function-level store an attribute of a local
1336
+ # def f():
1337
+ # aa == 42
1338
+ # aa.bb.cc = 99
1339
+ # => LOAD_FAST; LOAD_ATTR; STORE_ATTR
1340
+ # * Function-level load an attribute of an expression other than a name
1341
+ # def f():
1342
+ # foo().bb.cc
1343
+ # => [CALL_FUNCTION, etc]; LOAD_ATTR; LOAD_ATTR
1344
+ # * Function-level store an attribute of an expression other than a name
1345
+ # def f():
1346
+ # foo().bb.cc = 42
1347
+ # => [CALL_FUNCTION, etc]; LOAD_ATTR; STORE_ATTR
1348
+ # * Function-level import
1349
+ # def f():
1350
+ # import aa.bb.cc
1351
+ # => IMPORT_NAME "aa.bb.cc", STORE_FAST "aa"
1352
+ # * Module-level load of a top-level global
1353
+ # aa
1354
+ # => LOAD_NAME
1355
+ # * Module-level store of a top-level global
1356
+ # aa = 42
1357
+ # => STORE_NAME
1358
+ # * Module-level load of an attribute of a global
1359
+ # aa.bb.cc
1360
+ # => LOAD_NAME, LOAD_ATTR, LOAD_ATTR
1361
+ # * Module-level store of an attribute of a global
1362
+ # aa.bb.cc = 42
1363
+ # => LOAD_NAME, LOAD_ATTR, STORE_ATTR
1364
+ # * Module-level import
1365
+ # import aa.bb.cc
1366
+ # IMPORT_NAME "aa.bb.cc", STORE_NAME "aa"
1367
+ # * Closure
1368
+ # def f():
1369
+ # aa = 42
1370
+ # return lambda: aa
1371
+ # f: STORE_DEREF, LOAD_CLOSURE, MAKE_CLOSURE
1372
+ # g = f(): LOAD_DEREF
1373
+ bytecode = co.co_code
1374
+ n = len(bytecode)
1375
+ i = 0
1376
+ extended_arg = 0
1377
+ stores = set()
1378
+ loads_after_label = set()
1379
+ loads_before_label_without_stores = set()
1380
+ # Find the earliest target of a backward jump.
1381
+ earliest_backjump_label = _find_earliest_backjump_label(bytecode)
1382
+ # Loop through bytecode.
1383
+ while i < n:
1384
+ op = bytecode[i]
1385
+ i += 1
1386
+ if op == CACHE:
1387
+ continue
1388
+ if take_arg(op):
1389
+ oparg = bytecode[i] | extended_arg
1390
+ extended_arg = 0
1391
+ if op == EXTENDED_ARG:
1392
+ extended_arg = (oparg << 8)
1393
+ continue
1394
+ i += 1
1395
+
1396
+ if pending is not None:
1397
+ if op == STORE_ATTR:
1398
+ # {LOAD_GLOBAL|LOAD_NAME} {LOAD_ATTR}* {STORE_ATTR}
1399
+ pending.append(co.co_names[oparg])
1400
+ fullname = ".".join(pending)
1401
+ pending = None
1402
+ stores.add(fullname)
1403
+ continue
1404
+ if op in [LOAD_ATTR, LOAD_METHOD]:
1405
+ if sys.version_info >= (3,12):
1406
+ # from the docs:
1407
+ #
1408
+ # If the low bit of namei is not set, this replaces
1409
+ # STACK[-1] with getattr(STACK[-1], co_names[namei>>1]).
1410
+ #
1411
+ # If the low bit of namei is set, this will attempt to load
1412
+ # a method named co_names[namei>>1] from the STACK[-1]
1413
+ # object. STACK[-1] is popped. This bytecode distinguishes
1414
+ # two cases: if STACK[-1] has a method with the correct
1415
+ # name, the bytecode pushes the unbound method and
1416
+ # STACK[-1]. STACK[-1] will be used as the first argument
1417
+ # (self) by CALL when calling the unbound method. Otherwise,
1418
+ # NULL and the object returned by the attribute lookup are
1419
+ # pushed.
1420
+ #
1421
+ # Changed in version 3.12: If the low bit of namei is set,
1422
+ # then a NULL or self is pushed to the stack before the
1423
+ # attribute or unbound method respectively.
1424
+ #
1425
+ # Implication for Pyflyby
1426
+ #
1427
+ # In our case I think it means we are always looking at
1428
+ # oparg>>1 as the name of the names we need to load,
1429
+ # Though we don't keep track of the stack, and so we may get
1430
+ # wrong results ?
1431
+ #
1432
+ # In any case this seem to match what load_method was doing
1433
+ # before.
1434
+ pending.append(co.co_names[oparg>>1])
1435
+ else:
1436
+ # {LOAD_GLOBAL|LOAD_NAME} {LOAD_ATTR}* so far;
1437
+ # possibly more LOAD_ATTR/STORE_ATTR will follow
1438
+ pending.append(co.co_names[oparg])
1439
+ continue
1440
+ # {LOAD_GLOBAL|LOAD_NAME} {LOAD_ATTR}* (and no more
1441
+ # LOAD_ATTR/STORE_ATTR)
1442
+ fullname = ".".join(pending)
1443
+ pending = None
1444
+ if i >= earliest_backjump_label:
1445
+ loads_after_label.add(fullname)
1446
+ elif fullname not in stores:
1447
+ loads_before_label_without_stores.add(fullname)
1448
+ # Fall through.
1449
+
1450
+ if op is LOAD_GLOBAL:
1451
+ # Starting with 3.11, the low bit is used to tell whether to
1452
+ # push an extra null on the stack, so we need to >> 1
1453
+ # >> 0 does nothing
1454
+ pending = [co.co_names[oparg >> LOAD_SHIFT]]
1455
+ continue
1456
+ if op is LOAD_NAME:
1457
+ pending = [co.co_names[oparg]]
1458
+ continue
1459
+
1460
+ if op in [STORE_GLOBAL, STORE_NAME]:
1461
+ stores.add(co.co_names[oparg])
1462
+ continue
1463
+
1464
+ # We don't need to worry about: LOAD_FAST, STORE_FAST, LOAD_CLOSURE,
1465
+ # LOAD_DEREF, STORE_DEREF. LOAD_FAST and STORE_FAST refer to local
1466
+ # variables; LOAD_CLOSURE, LOAD_DEREF, and STORE_DEREF relate to
1467
+ # closure variables. In both cases we know these are not missing
1468
+ # imports. It's convenient that these are separate opcodes, because
1469
+ # then we don't need to deal with them manually.
1470
+
1471
+ # Record which variables we saw that were loaded in this module without a
1472
+ # corresponding store. We handle two cases.
1473
+ #
1474
+ # 1. Load-before-store; no loops (i.e. no backward jumps).
1475
+ # Example A::
1476
+ # foo.bar()
1477
+ # import foo
1478
+ # In the above example A, "foo" was used before it was imported. We
1479
+ # consider it a candidate for auto-import.
1480
+ # Example B:
1481
+ # if condition1(): # L1
1482
+ # import foo1 # L2
1483
+ # foo1.bar() + foo2.bar() # L3
1484
+ # import foo2 # L4
1485
+ # In the above example B, "foo2" was used before it was imported; the
1486
+ # fact that there is a jump target at L3 is irrelevant because it is
1487
+ # the target of a forward jump; there is no way that foo2 can be
1488
+ # imported (L4) before foo2 is used (L3).
1489
+ # On the other hand, we don't know whether condition1 will be true,
1490
+ # so we assume L2 will be executed and therefore don't consider the
1491
+ # use of "foo1" at L3 to be problematic.
1492
+ #
1493
+ # 2. Load-before-store; with loops (backward jumps). Example:
1494
+ # for i in range(10):
1495
+ # if i > 0:
1496
+ # print x
1497
+ # else:
1498
+ # x = "hello"
1499
+ # In the above example, "x" is actually always stored before load,
1500
+ # even though in a linear reading of the bytecode we would see the
1501
+ # store before any loads.
1502
+ #
1503
+ # It would be impossible to perfectly follow conditional code, because
1504
+ # code could be arbitrarily complicated and would require a flow control
1505
+ # analysis that solves the halting problem. We do the best we can and
1506
+ # handle case 1 as a common case.
1507
+ #
1508
+ # Case 1: If we haven't seen a label, then we know that any load
1509
+ # before a preceding store is definitely too early.
1510
+ # Case 2: If we have seen a label, then we consider any preceding
1511
+ # or subsequent store to potentially match the load.
1512
+ loads_without_stores.update( loads_before_label_without_stores ) # case 1
1513
+ loads_without_stores.update( loads_after_label - stores ) # case 2
1514
+
1515
+ # The ``pending`` variable should have been reset at this point, because a
1516
+ # function should always end with a RETURN_VALUE opcode and therefore not
1517
+ # end in a LOAD_ATTR.
1518
+ assert pending is None
1519
+
1520
+ # Recurse on inner function definitions, lambdas, generators, etc.
1521
+ for arg in co.co_consts:
1522
+ if isinstance(arg, types.CodeType):
1523
+ _find_loads_without_stores_in_code(arg, loads_without_stores)
1524
+
1525
+ if sys.version_info >= (3,12):
1526
+ from dis import hasarg
1527
+ def take_arg(op):
1528
+ return op in hasarg
1529
+ else:
1530
+ def take_arg(op):
1531
+ from opcode import HAVE_ARGUMENT
1532
+ return op >= HAVE_ARGUMENT
1533
+
1534
+ def _find_earliest_backjump_label(bytecode):
1535
+ """
1536
+ Find the earliest target of a backward jump.
1537
+
1538
+ These normally represent loops.
1539
+
1540
+ For example, given the source code::
1541
+
1542
+ >>> def f():
1543
+ ... if foo1():
1544
+ ... foo2()
1545
+ ... else:
1546
+ ... foo3()
1547
+ ... foo4()
1548
+ ... while foo5(): # L7
1549
+ ... foo6()
1550
+
1551
+ The earliest target of a backward jump would be the 'while' loop at L7, at
1552
+ bytecode offset 38::
1553
+
1554
+ >>> _find_earliest_backjump_label(f.__code__.co_code) # doctest: +SKIP
1555
+ 38
1556
+
1557
+ Note that in this example there are earlier targets of jumps at bytecode
1558
+ offsets 20 and 28, but those are targets of _forward_ jumps, and the
1559
+ clients of this function care about the earliest _backward_ jump.
1560
+
1561
+ If there are no backward jumps, return an offset that points after the end
1562
+ of the bytecode.
1563
+
1564
+ :type bytecode:
1565
+ ``bytes``
1566
+ :param bytecode:
1567
+ Compiled bytecode, e.g. ``function.__code__.co_code``.
1568
+ :rtype:
1569
+ ``int``
1570
+ :return:
1571
+ The earliest target of a backward jump, as an offset into the bytecode.
1572
+ """
1573
+ # Code based on dis.findlabels().
1574
+ from opcode import hasjrel, hasjabs
1575
+ if not isinstance(bytecode, bytes):
1576
+ raise TypeError
1577
+ n = len(bytecode)
1578
+ earliest_backjump_label = n
1579
+ i = 0
1580
+ while i < n:
1581
+ op = bytecode[i]
1582
+ i += 1
1583
+ if not take_arg(op):
1584
+ continue
1585
+ if i+1 >= len(bytecode):
1586
+ break
1587
+ oparg = bytecode[i] + bytecode[i+1]*256
1588
+ i += 2
1589
+ label = None
1590
+ if op in hasjrel:
1591
+ label = i+oparg
1592
+ elif op in hasjabs:
1593
+ label = oparg
1594
+ else:
1595
+ # No label
1596
+ continue
1597
+ if label >= i:
1598
+ # Label is a forward jump
1599
+ continue
1600
+ # Found a backjump label. Keep track of the earliest one.
1601
+ earliest_backjump_label = min(earliest_backjump_label, label)
1602
+ return earliest_backjump_label
1603
+
1604
+
1605
+ def find_missing_imports(arg, namespaces):
1606
+ """
1607
+ Find symbols in the given code that require import.
1608
+
1609
+ We consider a symbol to require import if we see an access ("Load" in AST
1610
+ terminology) without an import or assignment ("Store" in AST terminology)
1611
+ in the same lexical scope.
1612
+
1613
+ For example, if we use an empty list of namespaces, then "os.path.join" is
1614
+ a symbol that requires import::
1615
+
1616
+ >>> [str(m) for m in find_missing_imports("os.path.join", namespaces=[{}])]
1617
+ ['os.path.join']
1618
+
1619
+ But if the global namespace already has the "os" module imported, then we
1620
+ know that ``os`` has a "path" attribute, which has a "join" attribute, so
1621
+ nothing needs import::
1622
+
1623
+ >>> import os
1624
+ >>> find_missing_imports("os.path.join", namespaces=[{"os":os}])
1625
+ []
1626
+
1627
+ Builtins are always included::
1628
+
1629
+ >>> [str(m) for m in find_missing_imports("os, sys, eval", [{"os": os}])]
1630
+ ['sys']
1631
+
1632
+ All symbols that are not defined are included::
1633
+
1634
+ >>> [str(m) for m in find_missing_imports("numpy.arange(x) + arange(y)", [{"y": 3}])]
1635
+ ['arange', 'numpy.arange', 'x']
1636
+
1637
+ If something is imported/assigned/etc within the scope, then we assume it
1638
+ doesn't require importing::
1639
+
1640
+ >>> [str(m) for m in find_missing_imports("import numpy; numpy.arange(x) + arange(x)", [{}])]
1641
+ ['arange', 'x']
1642
+
1643
+ >>> [str(m) for m in find_missing_imports("from numpy import pi; numpy.pi + pi + x", [{}])]
1644
+ ['numpy.pi', 'x']
1645
+
1646
+ >>> [str(m) for m in find_missing_imports("for x in range(3): print(numpy.arange(x))", [{}])]
1647
+ ['numpy.arange']
1648
+
1649
+ >>> [str(m) for m in find_missing_imports("foo1 = func(); foo1.bar + foo2.bar", [{}])]
1650
+ ['foo2.bar', 'func']
1651
+
1652
+ >>> [str(m) for m in find_missing_imports("a.b.y = 1; a.b.x, a.b.y, a.b.z", [{}])]
1653
+ ['a.b.x', 'a.b.z']
1654
+
1655
+ find_missing_imports() parses the AST, so it understands scoping. In the
1656
+ following example, ``x`` is never undefined::
1657
+
1658
+ >>> find_missing_imports("(lambda x: x*x)(7)", [{}])
1659
+ []
1660
+
1661
+ but this example, ``x`` is undefined at global scope::
1662
+
1663
+ >>> [str(m) for m in find_missing_imports("(lambda x: x*x)(7) + x", [{}])]
1664
+ ['x']
1665
+
1666
+ >>> # Python 3
1667
+ >>> [str(m) for m in find_missing_imports("[x+y+z for x,y in [(1,2)]], y", [{}])]
1668
+ ['y', 'z']
1669
+
1670
+ >>> [str(m) for m in find_missing_imports("(x+y+z for x,y in [(1,2)]), y", [{}])]
1671
+ ['y', 'z']
1672
+
1673
+ Only fully-qualified names starting at top-level are included::
1674
+
1675
+ >>> [str(m) for m in find_missing_imports("( ( a . b ) . x ) . y + ( c + d ) . x . y", [{}])]
1676
+ ['a.b.x.y', 'c', 'd']
1677
+
1678
+ :type arg:
1679
+ ``str``, ``ast.AST``, `PythonBlock`, ``callable``, or ``types.CodeType``
1680
+ :param arg:
1681
+ Python code, either as source text, a parsed AST, or compiled code; can
1682
+ be as simple as a single qualified name, or as complex as an entire
1683
+ module text.
1684
+ :type namespaces:
1685
+ ``dict`` or ``list`` of ``dict``
1686
+ :param namespaces:
1687
+ Stack of namespaces of symbols that exist per scope.
1688
+ :rtype:
1689
+ ``list`` of ``DottedIdentifier``
1690
+ """
1691
+ namespaces = ScopeStack(namespaces)
1692
+ if isinstance(arg, (DottedIdentifier, str)):
1693
+ try:
1694
+ arg = DottedIdentifier(arg)
1695
+ except BadDottedIdentifierError:
1696
+ pass
1697
+ else:
1698
+ # The string is a single identifier. Check directly whether it
1699
+ # needs import. This is an optimization to not bother parsing an
1700
+ # AST.
1701
+ if symbol_needs_import(arg, namespaces):
1702
+ return [arg]
1703
+ else:
1704
+ return []
1705
+ # Parse the string into an AST.
1706
+ node = ast.parse(arg, type_comments=True) # may raise SyntaxError
1707
+ # Get missing imports from AST.
1708
+ return _find_missing_imports_in_ast(node, namespaces)
1709
+ elif isinstance(arg, PythonBlock):
1710
+ return _find_missing_imports_in_ast(arg.ast_node, namespaces)
1711
+ elif isinstance(arg, ast.AST):
1712
+ return _find_missing_imports_in_ast(arg, namespaces)
1713
+ elif isinstance(arg, types.CodeType):
1714
+ return _find_missing_imports_in_code(arg, namespaces)
1715
+ elif callable(arg):
1716
+ # Find the code object.
1717
+ try:
1718
+ co = arg.__code__
1719
+ except AttributeError:
1720
+ # User-defined callable
1721
+ try:
1722
+ co = arg.__call__.__code__
1723
+ except AttributeError:
1724
+ # Built-in function; no auto importing needed.
1725
+ return []
1726
+ # Get missing imports from code object.
1727
+ return _find_missing_imports_in_code(co, namespaces)
1728
+ else:
1729
+ raise TypeError(
1730
+ "find_missing_imports(): expected a string, AST node, or code object; got a %s"
1731
+ % (type(arg).__name__,))
1732
+
1733
+
1734
+ def get_known_import(fullname, db=None):
1735
+ """
1736
+ Get the deepest known import.
1737
+
1738
+ For example, suppose:
1739
+
1740
+ - The user accessed "foo.bar.baz",
1741
+ - We know imports for "foo", "foo.bar", and "foo.bar.quux".
1742
+
1743
+ Then we return "import foo.bar".
1744
+
1745
+ :type fullname:
1746
+ `DottedIdentifier`
1747
+ :param fullname:
1748
+ Fully-qualified name, such as "scipy.interpolate"
1749
+ """
1750
+ # Get the import database.
1751
+ db = ImportDB.interpret_arg(db, target_filename=".")
1752
+ fullname = DottedIdentifier(fullname)
1753
+ # Look for the "deepest" import we know about. Suppose the user
1754
+ # accessed "foo.bar.baz". If we have an auto-import for "foo.bar",
1755
+ # then import that. (Presumably, the auto-import for "foo", if it
1756
+ # exists, refers to the same foo.)
1757
+ for partial_name in fullname.prefixes[::-1]:
1758
+ try:
1759
+ result = db.by_fullname_or_import_as[str(partial_name)]
1760
+ logger.debug("get_known_import(%r): found %r", fullname, result)
1761
+ return result
1762
+ except KeyError:
1763
+ logger.debug("get_known_import(%r): no known import for %r", fullname, partial_name)
1764
+ pass
1765
+ logger.debug("get_known_import(%r): found nothing", fullname)
1766
+ return None
1767
+
1768
+
1769
+ _IMPORT_FAILED:Set[Any] = set()
1770
+ """
1771
+ Set of imports we've already attempted and failed.
1772
+ """
1773
+
1774
+
1775
+ def clear_failed_imports_cache():
1776
+ """
1777
+ Clear the cache of previously failed imports.
1778
+ """
1779
+ if _IMPORT_FAILED:
1780
+ logger.debug("Clearing all %d entries from cache of failed imports",
1781
+ len(_IMPORT_FAILED))
1782
+ _IMPORT_FAILED.clear()
1783
+
1784
+
1785
+ def _try_import(imp, namespace):
1786
+ """
1787
+ Try to execute an import. Import the result into the namespace
1788
+ ``namespace``.
1789
+
1790
+ Print to stdout what we're about to do.
1791
+
1792
+ Only import into ``namespace`` if we won't clobber an existing definition.
1793
+
1794
+ :type imp:
1795
+ ``Import`` or ``str``
1796
+ :param imp:
1797
+ The import to execute, e.g. "from numpy import arange"
1798
+ :type namespace:
1799
+ ``dict``
1800
+ :param namespace:
1801
+ Namespace to import into.
1802
+ :return:
1803
+ ``True`` on success, ``False`` on failure
1804
+ """
1805
+ # TODO: generalize "imp" to any python statement whose toplevel is a
1806
+ # single Store (most importantly import and assignment, but could also
1807
+ # include def & cdef). For things other than imports, we would want to
1808
+ # first run handle_auto_imports() on the code.
1809
+ imp = Import(imp)
1810
+ if imp in _IMPORT_FAILED:
1811
+ logger.debug("Not attempting previously failed %r", imp)
1812
+ return False
1813
+ impas = imp.import_as
1814
+ name0 = impas.split(".", 1)[0]
1815
+ stmt = str(imp)
1816
+ logger.info(stmt)
1817
+ # Do the import in a temporary namespace, then copy it to ``namespace``
1818
+ # manually. We do this instead of just importing directly into
1819
+ # ``namespace`` for the following reason: Suppose the user wants "foo.bar",
1820
+ # but "foo" already exists in the global namespace. In order to import
1821
+ # "foo.bar" we need to import its parent module "foo". We only want to do
1822
+ # the "foo.bar" import if what we import as "foo" is the same as the
1823
+ # preexisting "foo". OTOH, we _don't_ want to do the "foo.bar" import if
1824
+ # the user had for some reason done "import fool as foo". So we (1)
1825
+ # import into a scratch namespace, (2) check that the top-level matches,
1826
+ # then (3) copy into the user's namespace if it didn't already exist.
1827
+ scratch_namespace = {}
1828
+ try:
1829
+ exec(stmt, scratch_namespace)
1830
+ imported = scratch_namespace[name0]
1831
+ except Exception as e:
1832
+ logger.warning("Error attempting to %r: %s: %s", stmt, type(e).__name__, e,
1833
+ exc_info=True)
1834
+ _IMPORT_FAILED.add(imp)
1835
+ return False
1836
+ try:
1837
+ preexisting = namespace[name0]
1838
+ except KeyError:
1839
+ # The top-level symbol didn't previously exist in the user's global
1840
+ # namespace. Add it.
1841
+ namespace[name0] = imported
1842
+ else:
1843
+ # The top-level symbol already existed in the user's global namespace.
1844
+ # Check that it matched.
1845
+ if preexisting is not imported:
1846
+ logger.info(" => Failed: pre-existing %r (%r) differs from imported %r",
1847
+ name0, preexisting, name0)
1848
+ return False
1849
+ return True
1850
+
1851
+
1852
+ def auto_import_symbol(fullname, namespaces, db=None, autoimported=None, post_import_hook=None):
1853
+ """
1854
+ Try to auto-import a single name.
1855
+
1856
+ :type fullname:
1857
+ ``str``
1858
+ :param fullname:
1859
+ Fully-qualified module name, e.g. "sqlalchemy.orm".
1860
+ :type namespaces:
1861
+ ``list`` of ``dict``, e.g. [globals()].
1862
+ :param namespaces:
1863
+ Namespaces to check. Namespace[-1] is the namespace to import into.
1864
+ :type db:
1865
+ `ImportDB`
1866
+ :param db:
1867
+ Import database to use.
1868
+ :param autoimported:
1869
+ If not ``None``, then a dictionary of identifiers already attempted.
1870
+ ``auto_import`` will not attempt to auto-import symbols already in this
1871
+ dictionary, and will add attempted symbols to this dictionary, with
1872
+ value ``True`` if the autoimport succeeded, or ``False`` if the autoimport
1873
+ did not succeed.
1874
+ :rtype:
1875
+ ``bool``
1876
+ :param post_import_hook:
1877
+ A callable that is invoked if an import was successfully made.
1878
+ It is invoked with the `Import` object representing the successful import
1879
+ :type post_import_hook:
1880
+ ``callable``
1881
+ :return:
1882
+ ``True`` if the symbol was already in the namespace, or the auto-import
1883
+ succeeded; ``False`` if the auto-import failed.
1884
+ """
1885
+ namespaces = ScopeStack(namespaces)
1886
+ if not symbol_needs_import(fullname, namespaces):
1887
+ return True
1888
+ if autoimported is None:
1889
+ autoimported = {}
1890
+ if DottedIdentifier(fullname) in autoimported:
1891
+ logger.debug("auto_import_symbol(%r): already attempted", fullname)
1892
+ return False
1893
+ # See whether there's a known import for this name. This is mainly
1894
+ # important for things like "from numpy import arange". Imports such as
1895
+ # "import sqlalchemy.orm" will also be handled by this, although it's less
1896
+ # important, since we're going to attempt that import anyway if it looks
1897
+ # like a "sqlalchemy" package is importable.
1898
+ imports = get_known_import(fullname, db=db)
1899
+ # successful_import will store last successfully executed import statement
1900
+ # to be passed to post_import_hook
1901
+ successful_import = None
1902
+ logger.debug("auto_import_symbol(%r): get_known_import() => %r",
1903
+ fullname, imports)
1904
+ if imports is None:
1905
+ # No known imports.
1906
+ pass
1907
+ else:
1908
+ assert len(imports) >= 1
1909
+ if len(imports) > 1:
1910
+ # Doh, multiple imports.
1911
+ logger.info("Multiple candidate imports for %s. Please pick one:", fullname)
1912
+ for imp in imports:
1913
+ logger.info(" %s", imp)
1914
+ autoimported[DottedIdentifier(fullname)] = False
1915
+ return False
1916
+ imp, = imports
1917
+ if symbol_needs_import(imp.import_as, namespaces=namespaces):
1918
+ # We're ready for some real action. The input code references a
1919
+ # name/attribute that (a) is not locally assigned, (b) is not a
1920
+ # global, (c) is not yet imported, (d) is a known auto-import, (e)
1921
+ # has only one definition
1922
+ # TODO: label which known_imports file the autoimport came from
1923
+ if not _try_import(imp, namespaces[-1]):
1924
+ # Failed; don't do anything else.
1925
+ autoimported[DottedIdentifier(fullname)] = False
1926
+ return False
1927
+ # Succeeded.
1928
+ successful_import = imp
1929
+ autoimported[DottedIdentifier(imp.import_as)] = True
1930
+ if imp.import_as == fullname:
1931
+ if post_import_hook:
1932
+ post_import_hook(imp)
1933
+ # We got what we wanted, so nothing more to do.
1934
+ return True
1935
+ if imp.import_as != imp.fullname:
1936
+ if post_import_hook:
1937
+ post_import_hook(imp)
1938
+ # This is not just an 'import foo.bar'; rather, it's a 'import
1939
+ # foo.bar as baz' or 'from foo import bar'. So don't go any
1940
+ # further.
1941
+ return True
1942
+ # Fall through.
1943
+ # We haven't yet imported what we want. Either there was no entry in the
1944
+ # known imports database, or it wasn't "complete" (e.g. the user wanted
1945
+ # "foo.bar.baz", and the known imports database only knew about "import
1946
+ # foo.bar"). For each component that may need importing, check if the
1947
+ # loader thinks it should be importable, and if so import it.
1948
+ for pmodule in ModuleHandle(fullname).ancestors:
1949
+ if not symbol_needs_import(pmodule.name, namespaces):
1950
+ continue
1951
+ pmodule_name = DottedIdentifier(pmodule.name)
1952
+ if pmodule_name in autoimported:
1953
+ if not autoimported[pmodule_name]:
1954
+ logger.debug("auto_import_symbol(%r): stopping because "
1955
+ "already previously failed to autoimport %s",
1956
+ fullname, pmodule_name)
1957
+ return False
1958
+ if not pmodule.exists:
1959
+ logger.debug("auto_import_symbol(%r): %r doesn't exist according to pkgutil",
1960
+ fullname, pmodule)
1961
+ autoimported[pmodule_name] = False
1962
+ return False
1963
+ imp_stmt = "import %s" % pmodule_name
1964
+ result = _try_import(imp_stmt, namespaces[-1])
1965
+ autoimported[pmodule_name] = result
1966
+ if not result:
1967
+ return False
1968
+ else:
1969
+ successful_import = Import(imp_stmt)
1970
+ if post_import_hook and successful_import:
1971
+ post_import_hook(successful_import)
1972
+ return True
1973
+
1974
+
1975
+ def auto_import(arg, namespaces, db=None, autoimported=None, post_import_hook=None, *, extra_db=None):
1976
+ """
1977
+ Parse ``arg`` for symbols that need to be imported and automatically import
1978
+ them.
1979
+
1980
+ :type arg:
1981
+ ``str``, ``ast.AST``, `PythonBlock`, ``callable``, or ``types.CodeType``
1982
+ :param arg:
1983
+ Python code, either as source text, a parsed AST, or compiled code; can
1984
+ be as simple as a single qualified name, or as complex as an entire
1985
+ module text.
1986
+ :type namespaces:
1987
+ ``dict`` or ``list`` of ``dict``
1988
+ :param namespaces:
1989
+ Namespaces to check. Namespace[-1] is the namespace to import into.
1990
+ :type db:
1991
+ `ImportDB`
1992
+ :param db:
1993
+ Import database to use.
1994
+ :type autoimported:
1995
+ ``dict``
1996
+ :param autoimported:
1997
+ If not ``None``, then a dictionary of identifiers already attempted.
1998
+ ``auto_import`` will not attempt to auto-import symbols already in this
1999
+ dictionary, and will add attempted symbols to this dictionary, with
2000
+ value ``True`` if the autoimport succeeded, or ``False`` if the autoimport
2001
+ did not succeed.
2002
+ :rtype:
2003
+ ``bool``
2004
+ :param post_import_hook:
2005
+ A callable invoked on each successful import. This is passed to
2006
+ `auto_import_symbol`
2007
+ :type post_import_hook:
2008
+ ``callable``
2009
+ :return:
2010
+ ``True`` if all symbols are already in the namespace or successfully
2011
+ auto-imported; ``False`` if any auto-imports failed.
2012
+ """
2013
+ namespaces = ScopeStack(namespaces)
2014
+ if isinstance(arg, PythonBlock):
2015
+ filename = arg.filename
2016
+ else:
2017
+ filename = "."
2018
+ try:
2019
+ fullnames = find_missing_imports(arg, namespaces)
2020
+ except SyntaxError:
2021
+ logger.debug("syntax error parsing %r", arg)
2022
+ return False
2023
+ logger.debug("Missing imports: %r", fullnames)
2024
+ if not fullnames:
2025
+ return True
2026
+ if autoimported is None:
2027
+ autoimported = {}
2028
+ db = ImportDB.interpret_arg(db, target_filename=filename)
2029
+ if extra_db:
2030
+ db = db|extra_db
2031
+ ok = True
2032
+ for fullname in fullnames:
2033
+ ok &= auto_import_symbol(fullname, namespaces, db, autoimported, post_import_hook=post_import_hook)
2034
+ return ok
2035
+
2036
+
2037
+ def auto_eval(arg, filename=None, mode=None,
2038
+ flags=None, auto_flags=True, globals=None, locals=None,
2039
+ db=None):
2040
+ """
2041
+ Evaluate/execute the given code, automatically importing as needed.
2042
+
2043
+ ``auto_eval`` will default the compilation ``mode`` to "eval" if possible::
2044
+
2045
+ >>> auto_eval("b64decode('aGVsbG8=')") + b"!"
2046
+ [PYFLYBY] from base64 import b64decode
2047
+ b'hello!'
2048
+
2049
+ ``auto_eval`` will default the compilation ``mode`` to "exec" if the input
2050
+ is not a single expression::
2051
+
2052
+ >>> auto_eval("if True: print(b64decode('aGVsbG8=').decode('utf-8'))")
2053
+ [PYFLYBY] from base64 import b64decode
2054
+ hello
2055
+
2056
+ This is roughly equivalent to "auto_import(arg); eval(arg)", but handles
2057
+ details better and more efficiently.
2058
+
2059
+ :type arg:
2060
+ ``str``, ``ast.AST``, ``code``, `Filename`, `FileText`, `PythonBlock`
2061
+ :param arg:
2062
+ Code to evaluate.
2063
+ :type filename:
2064
+ ``str``
2065
+ :param filename:
2066
+ Filename for compilation error messages. If ``None``, defaults to
2067
+ ``arg.filename`` if relevant, else ``"<stdin>"``.
2068
+ :type mode:
2069
+ ``str``
2070
+ :param mode:
2071
+ Compilation mode: ``None``, "exec", "single", or "eval". "exec",
2072
+ "single", and "eval" work as the built-in ``compile`` function do.
2073
+ If ``None``, then default to "eval" if the input is a string with a
2074
+ single expression, else "exec".
2075
+ :type flags:
2076
+ ``CompilerFlags`` or convertible (``int``, ``list`` of ``str``, etc.)
2077
+ :param flags:
2078
+ Compilation feature flags, e.g. ["division", "with_statement"]. If
2079
+ ``None``, defaults to no flags. Does not inherit flags from parent
2080
+ scope.
2081
+ :type auto_flags:
2082
+ ``bool``
2083
+ :param auto_flags:
2084
+ Whether to try other flags if ``flags`` causes SyntaxError.
2085
+ :type globals:
2086
+ ``dict``
2087
+ :param globals:
2088
+ Globals for evaluation. If ``None``, use an empty dictionary.
2089
+ :type locals:
2090
+ ``dict``
2091
+ :param locals:
2092
+ Locals for evaluation. If ``None``, use ``globals``.
2093
+ :type db:
2094
+ `ImportDB`
2095
+ :param db:
2096
+ Import database to use.
2097
+ :return:
2098
+ Result of evaluation (for mode="eval")
2099
+ """
2100
+ if isinstance(flags, int):
2101
+ assert isinstance(flags, CompilerFlags)
2102
+ if isinstance(arg, (str, Filename, FileText, PythonBlock)):
2103
+ block = PythonBlock(arg, filename=filename, flags=flags,
2104
+ auto_flags=auto_flags)
2105
+ flags = block.flags
2106
+ filename = block.filename
2107
+ arg = block.parse(mode=mode)
2108
+ elif isinstance(arg, (ast.AST, types.CodeType)):
2109
+ pass
2110
+ else:
2111
+ raise TypeError(
2112
+ "auto_eval(): expected some form of code; got a %s"
2113
+ % (type(arg).__name__,))
2114
+ # Canonicalize other args.
2115
+ if filename:
2116
+ filename = Filename(filename)
2117
+ else:
2118
+ filename = None
2119
+ if globals is None:
2120
+ globals = {}
2121
+ if locals is None:
2122
+ locals = globals
2123
+ db = ImportDB.interpret_arg(db, target_filename=filename)
2124
+ namespaces = [globals, locals]
2125
+ # Import as needed.
2126
+ auto_import(arg, namespaces, db)
2127
+ # Compile from AST to code object.
2128
+ if isinstance(arg, types.CodeType):
2129
+ code = arg
2130
+ else:
2131
+ # Infer mode from ast object.
2132
+ mode = infer_compile_mode(arg)
2133
+ # Compile ast node => code object. This step is necessary because
2134
+ # eval() doesn't work on AST objects. We don't need to pass ``flags``
2135
+ # to compile() because flags are irrelevant when we already have an
2136
+ # AST node.
2137
+ code = compile(arg, str(filename or "<unknown>"), mode)
2138
+ # Evaluate/execute.
2139
+ return eval(code, globals, locals)
2140
+
2141
+
2142
+ class LoadSymbolError(Exception):
2143
+
2144
+ def __str__(self):
2145
+ r = ": ".join(map(str, self.args))
2146
+ e = getattr(self, "__cause__", None)
2147
+ if e:
2148
+ r += ": %s: %s" % (type(e).__name__, e)
2149
+ return r
2150
+
2151
+
2152
+ def load_symbol(fullname, namespaces, autoimport=False, db=None,
2153
+ autoimported=None):
2154
+ """
2155
+ Load the symbol ``fullname``.
2156
+
2157
+ >>> import os
2158
+ >>> load_symbol("os.path.join.__name__", {"os": os})
2159
+ 'join'
2160
+
2161
+ >>> load_symbol("os.path.join.asdf", {"os": os})
2162
+ Traceback (most recent call last):
2163
+ ...
2164
+ pyflyby._autoimp.LoadSymbolError: os.path.join.asdf: AttributeError: 'function' object has no attribute 'asdf'
2165
+
2166
+ >>> load_symbol("os.path.join", {})
2167
+ Traceback (most recent call last):
2168
+ ...
2169
+ pyflyby._autoimp.LoadSymbolError: os.path.join: NameError: os
2170
+
2171
+ :type fullname:
2172
+ ``str``
2173
+ :param fullname:
2174
+ Fully-qualified symbol name, e.g. "os.path.join".
2175
+ :type namespaces:
2176
+ ``dict`` or ``list`` of ``dict``
2177
+ :param namespaces:
2178
+ Namespaces to check.
2179
+ :param autoimport:
2180
+ If ``False`` (default), the symbol must already be imported.
2181
+ If ``True``, then auto-import the symbol first.
2182
+ :type db:
2183
+ `ImportDB`
2184
+ :param db:
2185
+ Import database to use when ``autoimport=True``.
2186
+ :param autoimported:
2187
+ If not ``None``, then a dictionary of identifiers already attempted.
2188
+ ``auto_import`` will not attempt to auto-import symbols already in this
2189
+ dictionary, and will add attempted symbols to this dictionary, with
2190
+ value ``True`` if the autoimport succeeded, or ``False`` if the autoimport
2191
+ did not succeed.
2192
+ :return:
2193
+ Object.
2194
+ :raise LoadSymbolError:
2195
+ Object was not found or there was another exception.
2196
+ """
2197
+ namespaces = ScopeStack(namespaces)
2198
+ if autoimport:
2199
+ # Auto-import the symbol first.
2200
+ # We do the lookup as a separate step after auto-import. (An
2201
+ # alternative design could be to have auto_import_symbol() return the
2202
+ # symbol if possible. We don't do that because most users of
2203
+ # auto_import_symbol() don't need to follow down arbitrary (possibly
2204
+ # non-module) attributes.)
2205
+ auto_import_symbol(fullname, namespaces, db, autoimported=autoimported)
2206
+ name_parts = fullname.split(".")
2207
+ name0 = name_parts[0]
2208
+ for namespace in namespaces:
2209
+ try:
2210
+ obj = namespace[name0]
2211
+ except KeyError:
2212
+ pass
2213
+ else:
2214
+ for n in name_parts[1:]:
2215
+ try:
2216
+ # Do the getattr. This may raise AttributeError or
2217
+ # other exception.
2218
+ obj = getattr(obj, n)
2219
+ except Exception as e:
2220
+ e2 = LoadSymbolError(fullname)
2221
+ e2.__cause__ = e
2222
+ raise e2
2223
+ return obj
2224
+ else:
2225
+ # Not found in any namespace.
2226
+ e2 = LoadSymbolError(fullname)
2227
+ e2.__cause__ = NameError(name0)
2228
+ raise e2