version.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. """The `version` module holds the version information for Pydantic."""
  2. from typing import Tuple
  3. __all__ = 'VERSION', 'version_info'
  4. VERSION = '2.4.2'
  5. """The version of Pydantic."""
  6. def version_short() -> str:
  7. """Return the `major.minor` part of Pydantic version.
  8. It returns '2.1' if Pydantic version is '2.1.1'.
  9. """
  10. return '.'.join(VERSION.split('.')[:2])
  11. def version_info() -> str:
  12. """Return complete version information for Pydantic and its dependencies."""
  13. import platform
  14. import sys
  15. from pathlib import Path
  16. import pydantic_core._pydantic_core as pdc
  17. if sys.version_info >= (3, 8):
  18. import importlib.metadata as importlib_metadata
  19. else:
  20. import importlib_metadata
  21. # get data about packages that are closely related to pydantic, use pydantic or often conflict with pydantic
  22. package_names = {
  23. 'email-validator',
  24. 'fastapi',
  25. 'mypy',
  26. 'pydantic-extra-types',
  27. 'pydantic-settings',
  28. 'pyright',
  29. 'typing_extensions',
  30. }
  31. related_packages = []
  32. for dist in importlib_metadata.distributions():
  33. name = dist.metadata['Name']
  34. if name in package_names:
  35. related_packages.append(f'{name}-{dist.version}')
  36. info = {
  37. 'pydantic version': VERSION,
  38. 'pydantic-core version': pdc.__version__,
  39. 'pydantic-core build': getattr(pdc, 'build_info', None) or pdc.build_profile,
  40. 'install path': Path(__file__).resolve().parent,
  41. 'python version': sys.version,
  42. 'platform': platform.platform(),
  43. 'related packages': ' '.join(related_packages),
  44. }
  45. return '\n'.join('{:>30} {}'.format(k + ':', str(v).replace('\n', ' ')) for k, v in info.items())
  46. def parse_mypy_version(version: str) -> Tuple[int, ...]:
  47. """Parse mypy string version to tuple of ints.
  48. This function is included here rather than the mypy plugin file because the mypy plugin file cannot be imported
  49. outside a mypy run.
  50. It parses normal version like `0.930` and dev version
  51. like `0.940+dev.04cac4b5d911c4f9529e6ce86a27b44f28846f5d.dirty`.
  52. Args:
  53. version: The mypy version string.
  54. Returns:
  55. A tuple of ints. e.g. (0, 930).
  56. """
  57. return tuple(map(int, version.partition('+')[0].split('.')))