version.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. """The `version` module holds the version information for Pydantic."""
  2. from __future__ import annotations as _annotations
  3. __all__ = 'VERSION', 'version_info'
  4. VERSION = '2.10.6'
  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 importlib.metadata as importlib_metadata
  14. import os
  15. import platform
  16. import sys
  17. from pathlib import Path
  18. import pydantic_core._pydantic_core as pdc
  19. from ._internal import _git as git
  20. # get data about packages that are closely related to pydantic, use pydantic or often conflict with pydantic
  21. package_names = {
  22. 'email-validator',
  23. 'fastapi',
  24. 'mypy',
  25. 'pydantic-extra-types',
  26. 'pydantic-settings',
  27. 'pyright',
  28. 'typing_extensions',
  29. }
  30. related_packages = []
  31. for dist in importlib_metadata.distributions():
  32. name = dist.metadata['Name']
  33. if name in package_names:
  34. related_packages.append(f'{name}-{dist.version}')
  35. pydantic_dir = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
  36. most_recent_commit = (
  37. git.git_revision(pydantic_dir) if git.is_git_repo(pydantic_dir) and git.have_git() else 'unknown'
  38. )
  39. info = {
  40. 'pydantic version': VERSION,
  41. 'pydantic-core version': pdc.__version__,
  42. 'pydantic-core build': getattr(pdc, 'build_info', None) or pdc.build_profile,
  43. 'install path': Path(__file__).resolve().parent,
  44. 'python version': sys.version,
  45. 'platform': platform.platform(),
  46. 'related packages': ' '.join(related_packages),
  47. 'commit': most_recent_commit,
  48. }
  49. return '\n'.join('{:>30} {}'.format(k + ':', str(v).replace('\n', ' ')) for k, v in info.items())
  50. def parse_mypy_version(version: str) -> tuple[int, int, int]:
  51. """Parse `mypy` string version to a 3-tuple of ints.
  52. It parses normal version like `1.11.0` and extra info followed by a `+` sign
  53. like `1.11.0+dev.d6d9d8cd4f27c52edac1f537e236ec48a01e54cb.dirty`.
  54. Args:
  55. version: The mypy version string.
  56. Returns:
  57. A triple of ints, e.g. `(1, 11, 0)`.
  58. """
  59. return tuple(map(int, version.partition('+')[0].split('.'))) # pyright: ignore[reportReturnType]