Skip to content

Commit

Permalink
Merge pull request #72 from callowayproject/update_pydantic
Browse files Browse the repository at this point in the history
Updated dependency from Pydantic 1 to 2.
  • Loading branch information
coordt authored Sep 25, 2023
2 parents 46be61b + 123dd69 commit 946e49c
Show file tree
Hide file tree
Showing 16 changed files with 161 additions and 53 deletions.
17 changes: 7 additions & 10 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: 'v0.0.285'
rev: 'v0.0.290'
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
exclude: test.*
- repo: https://github.com/psf/black
rev: 23.7.0
rev: 23.9.1
hooks:
- id: black
- repo: https://github.com/pre-commit/pre-commit-hooks
Expand Down Expand Up @@ -44,21 +44,18 @@ repos:
- id: mypy
args: [--no-strict-optional, --ignore-missing-imports]
additional_dependencies: ["pydantic<2.0", "toml", "types-all"]
- repo: https://github.com/terrencepreilly/darglint
rev: v1.8.1
- repo: https://github.com/jsh9/pydoclint
rev: 0.3.2
hooks:
- id: darglint
exclude: test.*|cli\.py
- id: pydoclint
args:
- -v 2
- "--message-template={path}:{line} in `{obj}`:\n {msg_id}: {msg}"
- --strictness=short
- "--config=pyproject.toml"
- repo: https://github.com/econchick/interrogate
rev: 1.5.0 # or master if you're bold
hooks:
- id: interrogate
exclude: test.*
- repo: https://github.com/python-jsonschema/check-jsonschema
rev: 0.24.1
rev: 0.26.3
hooks:
- id: check-azure-pipelines
39 changes: 19 additions & 20 deletions bumpversion/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
from bumpversion.scm import SCMInfo
from bumpversion.version_part import VersionConfig

from pydantic import BaseModel, BaseSettings, Field
from pydantic import BaseModel, Field
from pydantic_settings import BaseSettings, SettingsConfigDict

from bumpversion.exceptions import ConfigurationError

Expand All @@ -26,32 +27,32 @@
class VersionPartConfig(BaseModel):
"""Configuration of a part of the version."""

values: Optional[list] # Optional. Numeric is used if missing or no items in list
optional_value: Optional[str] # Optional.
values: Optional[list] = None # Optional. Numeric is used if missing or no items in list
optional_value: Optional[str] = None # Optional.
# Defaults to first value. 0 in the case of numeric. Empty string means nothing is optional.
first_value: Optional[str] # Optional. Defaults to first value in values
first_value: Union[str, int, None] = None # Optional. Defaults to first value in values
independent: bool = False


class FileConfig(BaseModel):
"""Search and replace file config."""

filename: Optional[str]
glob: Optional[str] # Conflicts with filename. If both are specified, glob wins
parse: Optional[str] # If different from outer scope
serialize: Optional[List[str]] # If different from outer scope
search: Optional[str] # If different from outer scope
replace: Optional[str] # If different from outer scope
no_regex: Optional[bool] # If different from outer scope
ignore_missing_version: Optional[bool]
filename: Optional[str] = None
glob: Optional[str] = None # Conflicts with filename. If both are specified, glob wins
parse: Optional[str] = None # If different from outer scope
serialize: Optional[List[str]] = None # If different from outer scope
search: Optional[str] = None # If different from outer scope
replace: Optional[str] = None # If different from outer scope
no_regex: Optional[bool] = None # If different from outer scope
ignore_missing_version: Optional[bool] = None


class Config(BaseSettings):
"""Bump Version configuration."""

current_version: Optional[str]
parse: str
serialize: List[str] = Field(min_items=1)
serialize: List[str] = Field(min_length=1)
search: str
replace: str
no_regex: bool
Expand All @@ -67,11 +68,9 @@ class Config(BaseSettings):
scm_info: Optional["SCMInfo"]
parts: Dict[str, VersionPartConfig]
files: List[FileConfig]
included_paths: List[str] = []
excluded_paths: List[str] = []

class Config:
env_prefix = "bumpversion_"
included_paths: List[str] = Field(default_factory=list)
excluded_paths: List[str] = Field(default_factory=list)
model_config = SettingsConfigDict(env_prefix="bumpversion_")

def add_files(self, filename: Union[str, List[str]]) -> None:
"""Add a filename to the list of files."""
Expand Down Expand Up @@ -177,7 +176,7 @@ def get_configuration(config_file: Union[str, Path, None] = None, **overrides) -
Returns:
The configuration
"""
from bumpversion.scm import SCMInfo, get_scm_info
from bumpversion.scm import SCMInfo, SourceCodeManager, get_scm_info # noqa: F401

config_dict = DEFAULTS.copy()
parsed_config = read_config_file(config_file) if config_file else {}
Expand All @@ -195,7 +194,7 @@ def get_configuration(config_file: Union[str, Path, None] = None, **overrides) -
config_dict["files"] = get_all_file_configs(config_dict)

# Resolve the SCMInfo class for Pydantic's BaseSettings
Config.update_forward_refs(SCMInfo=SCMInfo)
Config.model_rebuild()
config = Config(**config_dict) # type: ignore[arg-type]

# Get the information about the SCM
Expand Down
4 changes: 2 additions & 2 deletions bumpversion/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ class NumericFunction(PartFunction):

FIRST_NUMERIC = re.compile(r"(\D*)(\d+)(.*)")

def __init__(self, optional_value: Optional[str] = None, first_value: Optional[str] = None):
def __init__(self, optional_value: Union[str, int, None] = None, first_value: Union[str, int, None] = None):
if first_value is not None and not self.FIRST_NUMERIC.search(str(first_value)):
raise ValueError(f"The given first value {first_value} does not contain any digit")

self.first_value = str(first_value or 0)
self.optional_value = optional_value or self.first_value
self.optional_value = str(optional_value or self.first_value)

def bump(self, value: Union[str, int]) -> str:
"""Increase the first numerical value by one."""
Expand Down
11 changes: 7 additions & 4 deletions bumpversion/version_part.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import re
import string
from copy import copy
from typing import Any, Dict, List, MutableMapping, Optional
from typing import Any, Dict, List, MutableMapping, Optional, Union

from click import UsageError

Expand All @@ -23,12 +23,15 @@ class VersionPart:
based on the configuration given.
"""

def __init__(self, config: VersionPartConfig, value: Optional[str] = None):
self._value = value
def __init__(self, config: VersionPartConfig, value: Union[str, int, None] = None):
self._value = str(value) if value is not None else None
self.config = config
self.func: Optional[PartFunction] = None
if config.values:
self.func = ValuesFunction(config.values, config.optional_value, config.first_value)
str_values = [str(v) for v in config.values]
str_optional_value = str(config.optional_value) if config.optional_value is not None else None
str_first_value = str(config.first_value) if config.first_value is not None else None
self.func = ValuesFunction(str_values, str_optional_value, str_first_value)
else:
self.func = NumericFunction(config.optional_value, config.first_value or "0")

Expand Down
12 changes: 10 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ keywords = ["bumpversion", "version", "release"]
dynamic = ["version"]
dependencies = [
"click",
"pydantic<2.0.0",
"pydantic",
"pydantic-settings",
"rich-click",
"rich",
"tomlkit",
]

[project.scripts]
bump-my-version = "bumpversion.cli:cli"
bumpversion = "bumpversion.cli:cli"


[project.urls]
Expand Down Expand Up @@ -237,3 +237,11 @@ search = "Unreleased"
filename = "CHANGELOG.md"
search = "{current_version}...HEAD"
replace = "{current_version}...{new_version}"

[tool.pydoclint]
style = "google"
exclude = '\.git|tests'
require-return-section-when-returning-nothing = false
arg-type-hints-in-docstring = false
check-return-types = false
quiet = true
13 changes: 12 additions & 1 deletion requirements/dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#
alabaster==0.7.13
# via sphinx
annotated-types==0.5.0
# via pydantic
argopt==0.8.2
# via git-fame
astroid==2.15.6
Expand Down Expand Up @@ -103,7 +105,13 @@ pluggy==1.0.0
# via pytest
pre-commit==3.3.3
# via bump-my-version (pyproject.toml)
pydantic==1.10.9
pydantic==2.4.0
# via
# bump-my-version (pyproject.toml)
# pydantic-settings
pydantic-core==2.10.0
# via pydantic
pydantic-settings==2.0.3
# via bump-my-version (pyproject.toml)
pygments==2.15.1
# via
Expand All @@ -123,6 +131,8 @@ pytest-mock==3.11.1
# via bump-my-version (pyproject.toml)
python-dateutil==2.8.2
# via ghp-import
python-dotenv==1.0.0
# via pydantic-settings
pyyaml==6.0
# via
# myst-parser
Expand Down Expand Up @@ -197,6 +207,7 @@ typing-extensions==4.6.3
# via
# astroid
# pydantic
# pydantic-core
# sphinx-autodoc2
# typer
uc-micro-py==1.0.2
Expand Down
13 changes: 12 additions & 1 deletion requirements/docs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#
alabaster==0.7.13
# via sphinx
annotated-types==0.5.0
# via pydantic
astroid==2.15.6
# via sphinx-autodoc2
babel==2.12.1
Expand Down Expand Up @@ -57,7 +59,13 @@ myst-parser==2.0.0
# via bump-my-version (pyproject.toml)
packaging==23.1
# via sphinx
pydantic==1.10.9
pydantic==2.4.0
# via
# bump-my-version (pyproject.toml)
# pydantic-settings
pydantic-core==2.10.0
# via pydantic
pydantic-settings==2.0.3
# via bump-my-version (pyproject.toml)
pygments==2.15.1
# via
Expand All @@ -66,6 +74,8 @@ pygments==2.15.1
# sphinx
python-dateutil==2.8.2
# via ghp-import
python-dotenv==1.0.0
# via pydantic-settings
pyyaml==6.0
# via myst-parser
requests==2.31.0
Expand Down Expand Up @@ -121,6 +131,7 @@ typing-extensions==4.6.3
# via
# astroid
# pydantic
# pydantic-core
# sphinx-autodoc2
uc-micro-py==1.0.2
# via linkify-it-py
Expand Down
16 changes: 14 additions & 2 deletions requirements/prod.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#
# pip-compile --output-file=requirements/prod.txt pyproject.toml
#
annotated-types==0.5.0
# via pydantic
click==8.1.3
# via
# bump-my-version (pyproject.toml)
Expand All @@ -12,10 +14,18 @@ markdown-it-py==3.0.0
# via rich
mdurl==0.1.2
# via markdown-it-py
pydantic==1.10.9
pydantic==2.4.0
# via
# bump-my-version (pyproject.toml)
# pydantic-settings
pydantic-core==2.10.0
# via pydantic
pydantic-settings==2.0.3
# via bump-my-version (pyproject.toml)
pygments==2.15.1
# via rich
python-dotenv==1.0.0
# via pydantic-settings
rich==13.4.2
# via
# bump-my-version (pyproject.toml)
Expand All @@ -25,4 +35,6 @@ rich-click==1.6.1
tomlkit==0.11.8
# via bump-my-version (pyproject.toml)
typing-extensions==4.6.3
# via pydantic
# via
# pydantic
# pydantic-core
16 changes: 14 additions & 2 deletions requirements/test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#
# pip-compile --extra=test --output-file=requirements/test.txt pyproject.toml
#
annotated-types==0.5.0
# via pydantic
cfgv==3.3.1
# via pre-commit
click==8.1.3
Expand Down Expand Up @@ -38,7 +40,13 @@ pluggy==1.0.0
# via pytest
pre-commit==3.3.3
# via bump-my-version (pyproject.toml)
pydantic==1.10.9
pydantic==2.4.0
# via
# bump-my-version (pyproject.toml)
# pydantic-settings
pydantic-core==2.10.0
# via pydantic
pydantic-settings==2.0.3
# via bump-my-version (pyproject.toml)
pygments==2.15.1
# via rich
Expand All @@ -51,6 +59,8 @@ pytest-cov==4.1.0
# via bump-my-version (pyproject.toml)
pytest-mock==3.11.1
# via bump-my-version (pyproject.toml)
python-dotenv==1.0.0
# via pydantic-settings
pyyaml==6.0
# via pre-commit
rich==13.4.2
Expand All @@ -66,7 +76,9 @@ tomli==2.0.1
tomlkit==0.11.8
# via bump-my-version (pyproject.toml)
typing-extensions==4.6.3
# via pydantic
# via
# pydantic
# pydantic-core
virtualenv==20.23.1
# via pre-commit

Expand Down
2 changes: 0 additions & 2 deletions setup.cfg

This file was deleted.

4 changes: 0 additions & 4 deletions setup.py

This file was deleted.

8 changes: 7 additions & 1 deletion tests/fixtures/basic_cfg_expected.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,13 @@
'optional_value': 'gamma',
'values': ['dev', 'gamma']}},
'replace': '{new_version}',
'scm_info': SCMInfo(tool=No SCM tool, commit_sha=None, distance_to_latest_tag=None, current_version=None, dirty=None),
'scm_info': {'branch_name': None,
'commit_sha': None,
'current_version': None,
'dirty': None,
'distance_to_latest_tag': None,
'short_branch_name': None,
'tool': None},
'search': '{current_version}',
'serialize': ['{major}.{minor}.{patch}-{release}', '{major}.{minor}.{patch}'],
'sign_tags': False,
Expand Down
Loading

0 comments on commit 946e49c

Please sign in to comment.