testargs.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. """Ensure the argparse parser and Options class are in sync.
  2. In particular, verify that the argparse defaults are the same as the Options
  3. defaults, and that argparse doesn't assign any new members to the Options
  4. object it creates.
  5. """
  6. from __future__ import annotations
  7. import argparse
  8. import sys
  9. from mypy.main import infer_python_executable, process_options
  10. from mypy.options import Options
  11. from mypy.test.helpers import Suite, assert_equal
  12. class ArgSuite(Suite):
  13. def test_coherence(self) -> None:
  14. options = Options()
  15. _, parsed_options = process_options([], require_targets=False)
  16. # FIX: test this too. Requires changing working dir to avoid finding 'setup.cfg'
  17. options.config_file = parsed_options.config_file
  18. assert_equal(options.snapshot(), parsed_options.snapshot())
  19. def test_executable_inference(self) -> None:
  20. """Test the --python-executable flag with --python-version"""
  21. sys_ver_str = "{ver.major}.{ver.minor}".format(ver=sys.version_info)
  22. base = ["file.py"] # dummy file
  23. # test inference given one (infer the other)
  24. matching_version = base + [f"--python-version={sys_ver_str}"]
  25. _, options = process_options(matching_version)
  26. assert options.python_version == sys.version_info[:2]
  27. assert options.python_executable == sys.executable
  28. matching_version = base + [f"--python-executable={sys.executable}"]
  29. _, options = process_options(matching_version)
  30. assert options.python_version == sys.version_info[:2]
  31. assert options.python_executable == sys.executable
  32. # test inference given both
  33. matching_version = base + [
  34. f"--python-version={sys_ver_str}",
  35. f"--python-executable={sys.executable}",
  36. ]
  37. _, options = process_options(matching_version)
  38. assert options.python_version == sys.version_info[:2]
  39. assert options.python_executable == sys.executable
  40. # test that --no-site-packages will disable executable inference
  41. matching_version = base + [f"--python-version={sys_ver_str}", "--no-site-packages"]
  42. _, options = process_options(matching_version)
  43. assert options.python_version == sys.version_info[:2]
  44. assert options.python_executable is None
  45. # Test setting python_version/executable from config file
  46. special_opts = argparse.Namespace()
  47. special_opts.python_executable = None
  48. special_opts.python_version = None
  49. special_opts.no_executable = None
  50. # first test inferring executable from version
  51. options = Options()
  52. options.python_executable = None
  53. options.python_version = sys.version_info[:2]
  54. infer_python_executable(options, special_opts)
  55. assert options.python_version == sys.version_info[:2]
  56. assert options.python_executable == sys.executable
  57. # then test inferring version from executable
  58. options = Options()
  59. options.python_executable = sys.executable
  60. infer_python_executable(options, special_opts)
  61. assert options.python_version == sys.version_info[:2]
  62. assert options.python_executable == sys.executable