Skip to content

Commit

Permalink
Add back Python 3.8 support (#249)
Browse files Browse the repository at this point in the history
  • Loading branch information
Viicos authored Dec 20, 2024
1 parent 6ba4cc3 commit 6a08c3a
Show file tree
Hide file tree
Showing 13 changed files with 30 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ['3.9', '3.10', '3.11', '3.12', '3.13']
python-version: ['3.8', '3.9', '3.10', '3.11', '3.12', '3.13']

env:
UV_PYTHON: ${{ matrix.python-version }}
Expand Down
8 changes: 4 additions & 4 deletions pydantic_extra_types/color.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@
import math
import re
from colorsys import hls_to_rgb, rgb_to_hls
from typing import Any, Callable, Literal, Union, cast
from typing import Any, Callable, Literal, Tuple, Union, cast

from pydantic import GetJsonSchemaHandler
from pydantic._internal import _repr
from pydantic.json_schema import JsonSchemaValue
from pydantic_core import CoreSchema, PydanticCustomError, core_schema

ColorTuple = Union[tuple[int, int, int], tuple[int, int, int, float]]
ColorTuple = Union[Tuple[int, int, int], Tuple[int, int, int, float]]
ColorType = Union[ColorTuple, str, 'Color']
HslColorTuple = Union[tuple[float, float, float], tuple[float, float, float, float]]
HslColorTuple = Union[Tuple[float, float, float], Tuple[float, float, float, float]]


class RGBA:
Expand Down Expand Up @@ -115,7 +115,7 @@ def as_named(self, *, fallback: bool = False) -> str:
"""
if self._rgba.alpha is not None:
return self.as_hex()
rgb = cast(tuple[int, int, int], self.as_rgb_tuple())
rgb = cast('tuple[int, int, int]', self.as_rgb_tuple())

if rgb in COLORS_BY_VALUE:
return COLORS_BY_VALUE[rgb]
Expand Down
8 changes: 5 additions & 3 deletions pydantic_extra_types/coordinate.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
[`Coordinate`][pydantic_extra_types.coordinate.Coordinate] data types.
"""

from __future__ import annotations

from dataclasses import dataclass
from typing import Any, ClassVar
from typing import Any, ClassVar, Tuple

from pydantic import GetCoreSchemaHandler
from pydantic._internal import _repr
Expand Down Expand Up @@ -89,7 +91,7 @@ class Location(BaseModel):
```
"""

_NULL_ISLAND: ClassVar[tuple[float, float]] = (0.0, 0.0)
_NULL_ISLAND: ClassVar[Tuple[float, float]] = (0.0, 0.0)

latitude: Latitude
longitude: Longitude
Expand All @@ -100,7 +102,7 @@ def __get_pydantic_core_schema__(cls, source: type[Any], handler: GetCoreSchemaH
core_schema.no_info_wrap_validator_function(cls._parse_str, core_schema.str_schema()),
core_schema.no_info_wrap_validator_function(
cls._parse_tuple,
handler.generate_schema(tuple[float, float]),
handler.generate_schema(Tuple[float, float]),
),
handler(source),
]
Expand Down
3 changes: 1 addition & 2 deletions pydantic_extra_types/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from __future__ import annotations

import re
from collections.abc import Mapping
from typing import Any

from pydantic import GetCoreSchemaHandler
Expand Down Expand Up @@ -54,5 +53,5 @@ def __get_pydantic_core_schema__(cls, source_type: Any, handler: GetCoreSchemaHa
@classmethod
def __get_pydantic_json_schema__(
cls, schema: core_schema.CoreSchema, handler: GetCoreSchemaHandler
) -> Mapping[str, Any]:
) -> dict[str, Any]:
return handler(schema)
8 changes: 5 additions & 3 deletions pydantic_extra_types/pendulum_dt.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
CoreSchema implementation. This allows Pydantic to validate the DateTime object.
"""

from __future__ import annotations

try:
from pendulum import Date as _Date
from pendulum import DateTime as _DateTime
Expand Down Expand Up @@ -63,7 +65,7 @@ def __get_pydantic_core_schema__(cls, source: type[Any], handler: GetCoreSchemaH
return core_schema.no_info_wrap_validator_function(cls._validate, core_schema.datetime_schema())

@classmethod
def _validate(cls, value: Any, handler: core_schema.ValidatorFunctionWrapHandler) -> 'DateTime':
def _validate(cls, value: Any, handler: core_schema.ValidatorFunctionWrapHandler) -> DateTime:
"""Validate the datetime object and return it.
Args:
Expand Down Expand Up @@ -128,7 +130,7 @@ def __get_pydantic_core_schema__(cls, source: type[Any], handler: GetCoreSchemaH
return core_schema.no_info_wrap_validator_function(cls._validate, core_schema.date_schema())

@classmethod
def _validate(cls, value: Any, handler: core_schema.ValidatorFunctionWrapHandler) -> 'Date':
def _validate(cls, value: Any, handler: core_schema.ValidatorFunctionWrapHandler) -> Date:
"""Validate the date object and return it.
Args:
Expand Down Expand Up @@ -187,7 +189,7 @@ def __get_pydantic_core_schema__(cls, source: type[Any], handler: GetCoreSchemaH
return core_schema.no_info_wrap_validator_function(cls._validate, core_schema.timedelta_schema())

@classmethod
def _validate(cls, value: Any, handler: core_schema.ValidatorFunctionWrapHandler) -> 'Duration':
def _validate(cls, value: Any, handler: core_schema.ValidatorFunctionWrapHandler) -> Duration:
"""Validate the Duration object and return it.
Args:
Expand Down
8 changes: 4 additions & 4 deletions pydantic_extra_types/phone_numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from collections.abc import Sequence
from dataclasses import dataclass
from functools import partial
from typing import Any, ClassVar, Optional
from typing import Any, ClassVar

from pydantic import GetCoreSchemaHandler, GetJsonSchemaHandler
from pydantic_core import PydanticCustomError, core_schema
Expand Down Expand Up @@ -107,9 +107,9 @@ class SomeModel(BaseModel):
us_number: USNumberType
"""

default_region: Optional[str] = None
default_region: str | None = None
number_format: str = 'RFC3966'
supported_regions: Optional[Sequence[str]] = None
supported_regions: Sequence[str] | None = None

def __post_init__(self) -> None:
if self.default_region and self.default_region not in phonenumbers.SUPPORTED_REGIONS:
Expand All @@ -131,7 +131,7 @@ def __post_init__(self) -> None:
def _parse(
region: str | None,
number_format: str,
supported_regions: Optional[Sequence[str]],
supported_regions: Sequence[str] | None,
phone_number: Any,
) -> str:
if not phone_number:
Expand Down
4 changes: 3 additions & 1 deletion pydantic_extra_types/routing_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
[`ABARoutingNumber`][pydantic_extra_types.routing_number.ABARoutingNumber] data type.
"""

from __future__ import annotations

from typing import Any, ClassVar

from pydantic import GetCoreSchemaHandler
Expand Down Expand Up @@ -54,7 +56,7 @@ def __get_pydantic_core_schema__(
)

@classmethod
def _validate(cls, __input_value: str, _: core_schema.ValidationInfo) -> 'ABARoutingNumber':
def _validate(cls, __input_value: str, _: core_schema.ValidationInfo) -> ABARoutingNumber:
return cls(__input_value)

@classmethod
Expand Down
3 changes: 2 additions & 1 deletion pydantic_extra_types/semver.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
"""

import warnings
from typing import Annotated, Any, Callable
from typing import Any, Callable

from pydantic import GetJsonSchemaHandler
from pydantic.json_schema import JsonSchemaValue
from pydantic_core import core_schema
from semver import Version
from typing_extensions import Annotated

warnings.warn(
'Use from pydantic_extra_types.semver import SemanticVersion instead. Will be removed in 3.0.0.', DeprecationWarning
Expand Down
2 changes: 1 addition & 1 deletion pydantic_extra_types/timezone_name.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def get_timezones() -> set[str]:
class TimeZoneNameSettings(type):
def __new__(cls, name: str, bases: tuple[type, ...], dct: dict[str, Any], **kwargs: Any) -> type[TimeZoneName]:
dct['strict'] = kwargs.pop('strict', True)
return cast(type[TimeZoneName], super().__new__(cls, name, bases, dct))
return cast('type[TimeZoneName]', super().__new__(cls, name, bases, dct))

def __init__(cls, name: str, bases: tuple[type, ...], dct: dict[str, Any], **kwargs: Any) -> None:
super().__init__(name, bases, dct)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ keep-runtime-typing = true

[tool.ruff]
line-length = 120
target-version = "py39"
target-version = 'py38'

[tool.ruff.lint]
extend-select = [
Expand Down
7 changes: 1 addition & 6 deletions tests/test_json_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@
import pycountry
import pytest
from pydantic import BaseModel

try:
from typing import Annotated
except ImportError:
# Python 3.8
from typing import Annotated
from typing_extensions import Annotated

import pydantic_extra_types
from pydantic_extra_types import epoch
Expand Down
8 changes: 1 addition & 7 deletions tests/test_phone_numbers_validator.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
from typing import Any, Optional, Union

try:
from typing import Annotated
except ImportError:
# Python 3.8
from typing import Annotated


import phonenumbers
import pytest
from phonenumbers import PhoneNumber
from pydantic import BaseModel, TypeAdapter, ValidationError
from typing_extensions import Annotated

from pydantic_extra_types.phone_numbers import PhoneNumberValidator

Expand Down
2 changes: 1 addition & 1 deletion uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6a08c3a

Please sign in to comment.