1"""The `version` module holds the version information for Pydantic."""
2
3from __future__ import annotations as _annotations
4
5from pydantic_core import __version__ as __pydantic_core_version__
6
7__all__ = 'VERSION', 'version_info'
8
9VERSION = '2.11.7'
10"""The version of Pydantic."""
11
12
13def version_short() -> str:
14 """Return the `major.minor` part of Pydantic version.
15
16 It returns '2.1' if Pydantic version is '2.1.1'.
17 """
18 return '.'.join(VERSION.split('.')[:2])
19
20
21def version_info() -> str:
22 """Return complete version information for Pydantic and its dependencies."""
23 import importlib.metadata
24 import platform
25 import sys
26 from pathlib import Path
27
28 import pydantic_core._pydantic_core as pdc
29
30 from ._internal import _git as git
31
32 # get data about packages that are closely related to pydantic, use pydantic or often conflict with pydantic
33 package_names = {
34 'email-validator',
35 'fastapi',
36 'mypy',
37 'pydantic-extra-types',
38 'pydantic-settings',
39 'pyright',
40 'typing_extensions',
41 }
42 related_packages = []
43
44 for dist in importlib.metadata.distributions():
45 name = dist.metadata['Name']
46 if name in package_names:
47 related_packages.append(f'{name}-{dist.version}')
48
49 pydantic_dir = Path(__file__).parents[1].resolve()
50 most_recent_commit = (
51 git.git_revision(pydantic_dir) if git.is_git_repo(pydantic_dir) and git.have_git() else 'unknown'
52 )
53
54 info = {
55 'pydantic version': VERSION,
56 'pydantic-core version': pdc.__version__,
57 'pydantic-core build': getattr(pdc, 'build_info', None) or pdc.build_profile,
58 'python version': sys.version,
59 'platform': platform.platform(),
60 'related packages': ' '.join(related_packages),
61 'commit': most_recent_commit,
62 }
63 return '\n'.join('{:>30} {}'.format(k + ':', str(v).replace('\n', ' ')) for k, v in info.items())
64
65
66def check_pydantic_core_version() -> bool:
67 """Check that the installed `pydantic-core` dependency is compatible."""
68 # Keep this in sync with the version constraint in the `pyproject.toml` dependencies:
69 return __pydantic_core_version__ == '2.33.2'
70
71
72def parse_mypy_version(version: str) -> tuple[int, int, int]:
73 """Parse `mypy` string version to a 3-tuple of ints.
74
75 It parses normal version like `1.11.0` and extra info followed by a `+` sign
76 like `1.11.0+dev.d6d9d8cd4f27c52edac1f537e236ec48a01e54cb.dirty`.
77
78 Args:
79 version: The mypy version string.
80
81 Returns:
82 A triple of ints, e.g. `(1, 11, 0)`.
83 """
84 return tuple(map(int, version.partition('+')[0].split('.'))) # pyright: ignore[reportReturnType]