pyflyby 1.9.4__py3-none-any.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.

Potentially problematic release.


This version of pyflyby might be problematic. Click here for more details.

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