__init__.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  2. # For details: https://github.com/pylint-dev/pylint/blob/main/LICENSE
  3. # Copyright (c) https://github.com/pylint-dev/pylint/blob/main/CONTRIBUTORS.txt
  4. from __future__ import annotations
  5. __all__ = [
  6. "__version__",
  7. "version",
  8. "modify_sys_path",
  9. "run_pylint",
  10. "run_epylint",
  11. "run_symilar",
  12. "run_pyreverse",
  13. ]
  14. import os
  15. import sys
  16. import warnings
  17. from collections.abc import Sequence
  18. from typing import NoReturn
  19. from pylint.__pkginfo__ import __version__
  20. # pylint: disable=import-outside-toplevel
  21. def run_pylint(argv: Sequence[str] | None = None) -> None:
  22. """Run pylint.
  23. argv can be a sequence of strings normally supplied as arguments on the command line
  24. """
  25. from pylint.lint import Run as PylintRun
  26. try:
  27. PylintRun(argv or sys.argv[1:])
  28. except KeyboardInterrupt:
  29. sys.exit(1)
  30. def _run_pylint_config(argv: Sequence[str] | None = None) -> None:
  31. """Run pylint-config.
  32. argv can be a sequence of strings normally supplied as arguments on the command line
  33. """
  34. from pylint.lint.run import _PylintConfigRun
  35. _PylintConfigRun(argv or sys.argv[1:])
  36. def run_epylint(argv: Sequence[str] | None = None) -> NoReturn:
  37. """Run epylint.
  38. argv can be a list of strings normally supplied as arguments on the command line
  39. """
  40. from pylint.epylint import Run as EpylintRun
  41. warnings.warn(
  42. "'run_epylint' will be removed in pylint 3.0, use "
  43. "https://github.com/emacsorphanage/pylint instead.",
  44. DeprecationWarning,
  45. stacklevel=1,
  46. )
  47. EpylintRun(argv)
  48. def run_pyreverse(argv: Sequence[str] | None = None) -> NoReturn:
  49. """Run pyreverse.
  50. argv can be a sequence of strings normally supplied as arguments on the command line
  51. """
  52. from pylint.pyreverse.main import Run as PyreverseRun
  53. PyreverseRun(argv or sys.argv[1:])
  54. def run_symilar(argv: Sequence[str] | None = None) -> NoReturn:
  55. """Run symilar.
  56. argv can be a sequence of strings normally supplied as arguments on the command line
  57. """
  58. from pylint.checkers.similar import Run as SimilarRun
  59. SimilarRun(argv or sys.argv[1:])
  60. def modify_sys_path() -> None:
  61. """Modify sys path for execution as Python module.
  62. Strip out the current working directory from sys.path.
  63. Having the working directory in `sys.path` means that `pylint` might
  64. inadvertently import user code from modules having the same name as
  65. stdlib or pylint's own modules.
  66. CPython issue: https://bugs.python.org/issue33053
  67. - Remove the first entry. This will always be either "" or the working directory
  68. - Remove the working directory from the second and third entries
  69. if PYTHONPATH includes a ":" at the beginning or the end.
  70. https://github.com/pylint-dev/pylint/issues/3636
  71. Don't remove it if PYTHONPATH contains the cwd or '.' as the entry will
  72. only be added once.
  73. - Don't remove the working directory from the rest. It will be included
  74. if pylint is installed in an editable configuration (as the last item).
  75. https://github.com/pylint-dev/pylint/issues/4161
  76. """
  77. cwd = os.getcwd()
  78. if sys.path[0] in ("", ".", cwd):
  79. sys.path.pop(0)
  80. env_pythonpath = os.environ.get("PYTHONPATH", "")
  81. if env_pythonpath.startswith(":") and env_pythonpath not in (f":{cwd}", ":."):
  82. sys.path.pop(0)
  83. elif env_pythonpath.endswith(":") and env_pythonpath not in (f"{cwd}:", ".:"):
  84. sys.path.pop(1)
  85. version = __version__