You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

85 lines
2.6 KiB

  1. """The `version` module holds the version information for Pydantic."""
  2. from __future__ import annotations as _annotations
  3. from pydantic_core import __version__ as __pydantic_core_version__
  4. __all__ = 'VERSION', 'version_info'
  5. VERSION = '2.11.7'
  6. """The version of Pydantic."""
  7. def version_short() -> str:
  8. """Return the `major.minor` part of Pydantic version.
  9. It returns '2.1' if Pydantic version is '2.1.1'.
  10. """
  11. return '.'.join(VERSION.split('.')[:2])
  12. def version_info() -> str:
  13. """Return complete version information for Pydantic and its dependencies."""
  14. import importlib.metadata
  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 = Path(__file__).parents[1].resolve()
  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. 'python version': sys.version,
  44. 'platform': platform.platform(),
  45. 'related packages': ' '.join(related_packages),
  46. 'commit': most_recent_commit,
  47. }
  48. return '\n'.join('{:>30} {}'.format(k + ':', str(v).replace('\n', ' ')) for k, v in info.items())
  49. def check_pydantic_core_version() -> bool:
  50. """Check that the installed `pydantic-core` dependency is compatible."""
  51. # Keep this in sync with the version constraint in the `pyproject.toml` dependencies:
  52. return __pydantic_core_version__ == '2.33.2'
  53. def parse_mypy_version(version: str) -> tuple[int, int, int]:
  54. """Parse `mypy` string version to a 3-tuple of ints.
  55. It parses normal version like `1.11.0` and extra info followed by a `+` sign
  56. like `1.11.0+dev.d6d9d8cd4f27c52edac1f537e236ec48a01e54cb.dirty`.
  57. Args:
  58. version: The mypy version string.
  59. Returns:
  60. A triple of ints, e.g. `(1, 11, 0)`.
  61. """
  62. return tuple(map(int, version.partition('+')[0].split('.'))) # pyright: ignore[reportReturnType]