Skip to content

Commit

Permalink
Add short_version and use it in links (#7115)
Browse files Browse the repository at this point in the history
  • Loading branch information
hramezani committed Aug 14, 2023
1 parent b903d72 commit d70abd8
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 13 deletions.
4 changes: 2 additions & 2 deletions pydantic/_internal/_generate_schema.py
Expand Up @@ -41,7 +41,7 @@
from ..errors import PydanticSchemaGenerationError, PydanticUndefinedAnnotation, PydanticUserError
from ..fields import AliasChoices, AliasPath, FieldInfo
from ..json_schema import JsonSchemaValue
from ..version import VERSION
from ..version import version_short
from ..warnings import PydanticDeprecatedSince20
from . import _decorators, _discriminated_union, _known_annotated_metadata, _typing_extra
from ._annotated_handlers import GetCoreSchemaHandler, GetJsonSchemaHandler
Expand Down Expand Up @@ -249,7 +249,7 @@ def _add_custom_serialization_from_json_encoders(
continue

warnings.warn(
f'`json_encoders` is deprecated. See https://docs.pydantic.dev/{VERSION}/usage/serialization/#custom-serializers for alternatives',
f'`json_encoders` is deprecated. See https://docs.pydantic.dev/{version_short()}/usage/serialization/#custom-serializers for alternatives',
PydanticDeprecatedSince20,
)

Expand Down
4 changes: 2 additions & 2 deletions pydantic/_migration.py
Expand Up @@ -3,7 +3,7 @@
from typing import Any, Callable, Dict

from ._internal._validators import import_string
from .version import VERSION
from .version import version_short

MOVED_IN_V2 = {
'pydantic.utils:version_info': 'pydantic.version:version_info',
Expand Down Expand Up @@ -288,7 +288,7 @@ def wrapper(name: str) -> object:
if import_path == 'pydantic:BaseSettings':
raise PydanticImportError(
'`BaseSettings` has been moved to the `pydantic-settings` package. '
f'See https://docs.pydantic.dev/{".".join(VERSION.split(".")[:2])}/migration/#basesettings-has-moved-to-pydantic-settings '
f'See https://docs.pydantic.dev/{version_short()}/migration/#basesettings-has-moved-to-pydantic-settings '
'for more details.'
)
if import_path in REMOVED_IN_V2:
Expand Down
4 changes: 2 additions & 2 deletions pydantic/errors.py
Expand Up @@ -6,7 +6,7 @@
from typing_extensions import Literal, Self

from ._migration import getattr_migration
from .version import VERSION
from .version import version_short

__all__ = (
'PydanticUserError',
Expand All @@ -20,7 +20,7 @@
# We use this URL to allow for future flexibility about how we host the docs, while allowing for Pydantic
# code in the while with "old" URLs to still work.
# 'u' refers to "user errors" - e.g. errors caused by developers using pydantic, as opposed to validation errors.
DEV_ERROR_DOCS_URL = f'https://errors.pydantic.dev/{VERSION}/u/'
DEV_ERROR_DOCS_URL = f'https://errors.pydantic.dev/{version_short()}/u/'
PydanticErrorCodes = Literal[
'class-not-fully-defined',
'custom-json-schema',
Expand Down
8 changes: 8 additions & 0 deletions pydantic/version.py
Expand Up @@ -7,6 +7,14 @@
"""The version of Pydantic."""


def version_short() -> str:
"""Return the major.minor part of Pydantic version.
It return '2.1' if Pydantic version is '2.1.1'.
"""
return '.'.join(VERSION.split('.')[:2])


def version_info() -> str:
"""Return complete version information for Pydantic and its dependencies."""
import platform
Expand Down
4 changes: 2 additions & 2 deletions pydantic/warnings.py
@@ -1,7 +1,7 @@
"""Pydantic-specific warnings."""
from __future__ import annotations as _annotations

from .version import VERSION
from .version import version_short

__all__ = 'PydanticDeprecatedSince20', 'PydanticDeprecationWarning'

Expand Down Expand Up @@ -36,7 +36,7 @@ def __str__(self) -> str:
f' to be removed in V{self.expected_removal[0]}.{self.expected_removal[1]}.'
)
if self.since == (2, 0):
message += f' See Pydantic V2 Migration Guide at https://errors.pydantic.dev/{VERSION}/migration/'
message += f' See Pydantic V2 Migration Guide at https://errors.pydantic.dev/{version_short()}/migration/'
return message


Expand Down
4 changes: 2 additions & 2 deletions tests/test_errors.py
Expand Up @@ -3,7 +3,7 @@
import pytest

from pydantic import BaseModel, PydanticUserError, ValidationError
from pydantic.version import VERSION
from pydantic.version import version_short


def test_user_error_url():
Expand All @@ -13,7 +13,7 @@ def test_user_error_url():
# insert_assert(str(exc_info.value))
assert str(exc_info.value) == (
'Pydantic models should inherit from BaseModel, BaseModel cannot be instantiated directly\n\n'
f'For further information visit https://errors.pydantic.dev/{VERSION}/u/base-model-instantiated'
f'For further information visit https://errors.pydantic.dev/{version_short()}/u/base-model-instantiated'
)


Expand Down
10 changes: 9 additions & 1 deletion tests/test_version.py
@@ -1,9 +1,11 @@
import re
from unittest.mock import patch

import pytest
from packaging.version import parse as parse_version

import pydantic
from pydantic.version import version_info
from pydantic.version import version_info, version_short


def test_version_info():
Expand All @@ -23,3 +25,9 @@ def test_version_attribute_is_present():

def test_version_attribute_is_a_string():
assert isinstance(pydantic.__version__, str)


@pytest.mark.parametrize('version,expected', (('2.1', '2.1'), ('2.1.0', '2.1')))
def test_version_short(version, expected):
with patch('pydantic.version.VERSION', version):
assert version_short() == expected
4 changes: 2 additions & 2 deletions tests/test_warnings.py
@@ -1,5 +1,5 @@
from pydantic import PydanticDeprecatedSince20, PydanticDeprecationWarning
from pydantic.version import VERSION
from pydantic.version import version_short


def test_pydantic_deprecation_warning():
Expand Down Expand Up @@ -28,7 +28,7 @@ def test_pydantic_deprecation_warning_2_0_migration_guide_link():

assert (
str(warning)
== f'Warning message. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/{VERSION}/migration/'
== f'Warning message. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/{version_short()}/migration/'
)


Expand Down

0 comments on commit d70abd8

Please sign in to comment.