test_module_imports.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. """
  2. Checks that SymPy does not contain indirect imports.
  3. An indirect import is importing a symbol from a module that itself imported the
  4. symbol from elsewhere. Such a constellation makes it harder to diagnose
  5. inter-module dependencies and import order problems, and is therefore strongly
  6. discouraged.
  7. (Indirect imports from end-user code is fine and in fact a best practice.)
  8. Implementation note: Forcing Python into actually unloading already-imported
  9. submodules is a tricky and partly undocumented process. To avoid these issues,
  10. the actual diagnostic code is in bin/diagnose_imports, which is run as a
  11. separate, pristine Python process.
  12. """
  13. import subprocess
  14. import sys
  15. from os.path import abspath, dirname, join, normpath
  16. import inspect
  17. from sympy.testing.pytest import XFAIL
  18. @XFAIL
  19. def test_module_imports_are_direct():
  20. my_filename = abspath(inspect.getfile(inspect.currentframe()))
  21. my_dirname = dirname(my_filename)
  22. diagnose_imports_filename = join(my_dirname, 'diagnose_imports.py')
  23. diagnose_imports_filename = normpath(diagnose_imports_filename)
  24. process = subprocess.Popen(
  25. [
  26. sys.executable,
  27. normpath(diagnose_imports_filename),
  28. '--problems',
  29. '--by-importer'
  30. ],
  31. stdout=subprocess.PIPE,
  32. stderr=subprocess.STDOUT,
  33. bufsize=-1)
  34. output, _ = process.communicate()
  35. assert output == '', "There are import problems:\n" + output.decode()