Skip to content

Commit

Permalink
Merge pull request #314 from callowayproject/313-python-38-typeerrorfix
Browse files Browse the repository at this point in the history
Fix some bugs
  • Loading branch information
coordt authored Mar 8, 2025
2 parents 29e2251 + 3578c87 commit c748828
Show file tree
Hide file tree
Showing 8 changed files with 255 additions and 208 deletions.
4 changes: 3 additions & 1 deletion bumpversion/config/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class Config(BaseSettings):
pep621_info: Optional[PEP621Info]
scm_info: Optional[SCMInfo]
parts: Dict[str, VersionComponentSpec]
moveable_tags: list[str] = Field(default_factory=list)
moveable_tags: List[str] = Field(default_factory=list)
files: List[FileChange] = Field(default_factory=list)
setup_hooks: List[str] = Field(default_factory=list)
pre_commit_hooks: List[str] = Field(default_factory=list)
Expand All @@ -113,6 +113,8 @@ class Config(BaseSettings):

def add_files(self, filename: Union[str, List[str]]) -> None:
"""Add a filename to the list of files."""
if not filename:
return
filenames = [filename] if isinstance(filename, str) else filename
files = set(self.files)
for name in filenames:
Expand Down
2 changes: 1 addition & 1 deletion bumpversion/config/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def get_all_file_configs(config_dict: dict) -> List[FileChange]:
"regex": config_dict["regex"],
"include_bumps": tuple(config_dict["parts"]),
}
files = [{k: v for k, v in filecfg.items() if v is not None} for filecfg in config_dict["files"]]
files = [{k: v for k, v in filecfg.items() if v is not None} for filecfg in filter(None, config_dict["files"])]
for f in files:
f.update({k: v for k, v in defaults.items() if k not in f})
return [FileChange(**f) for f in files]
Expand Down
12 changes: 6 additions & 6 deletions bumpversion/scm/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import subprocess
from pathlib import Path
from tempfile import NamedTemporaryFile
from typing import Any, ClassVar, MutableMapping, Optional
from typing import Any, ClassVar, List, MutableMapping, Optional

from bumpversion.exceptions import DirtyWorkingDirectoryError
from bumpversion.scm.models import LatestTagInfo, SCMConfig
Expand All @@ -19,9 +19,9 @@
class Git:
"""Git implementation."""

_TEST_AVAILABLE_COMMAND: ClassVar[list[str]] = ["git", "rev-parse", "--git-dir"]
_COMMIT_COMMAND: ClassVar[list[str]] = ["git", "commit", "-F"]
_ALL_TAGS_COMMAND: ClassVar[list[str]] = ["git", "tag", "--list"]
_TEST_AVAILABLE_COMMAND: ClassVar[List[str]] = ["git", "rev-parse", "--git-dir"]
_COMMIT_COMMAND: ClassVar[List[str]] = ["git", "commit", "-F"]
_ALL_TAGS_COMMAND: ClassVar[List[str]] = ["git", "tag", "--list"]

def __init__(self, config: SCMConfig):
self.config = config
Expand Down Expand Up @@ -73,7 +73,7 @@ def add_path(self, path: Pathlike) -> None:
except subprocess.CalledProcessError as e:
format_and_raise_error(e)

def get_all_tags(self) -> list[str]:
def get_all_tags(self) -> List[str]:
"""Return all tags in git."""
try:
result = run_command(self._ALL_TAGS_COMMAND)
Expand All @@ -86,7 +86,7 @@ def get_all_tags(self) -> list[str]:
):
return []

def commit_and_tag(self, files: list[Pathlike], context: MutableMapping, dry_run: bool = False) -> None:
def commit_and_tag(self, files: List[Pathlike], context: MutableMapping, dry_run: bool = False) -> None:
"""Commit and tag files to the repository using the configuration."""
if dry_run:
return
Expand Down
10 changes: 5 additions & 5 deletions bumpversion/scm/hg.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Mercurial source control management."""

import subprocess
from typing import ClassVar, MutableMapping, Optional
from typing import ClassVar, List, MutableMapping, Optional

from bumpversion.exceptions import DirtyWorkingDirectoryError, SignedTagsError
from bumpversion.scm.models import LatestTagInfo, SCMConfig
Expand All @@ -14,9 +14,9 @@
class Mercurial:
"""Mercurial source control management."""

_TEST_AVAILABLE_COMMAND: ClassVar[list[str]] = ["hg", "root"]
_COMMIT_COMMAND: ClassVar[list[str]] = ["hg", "commit", "--logfile"]
_ALL_TAGS_COMMAND: ClassVar[list[str]] = ["hg", "log", '--rev="tag()"', '--template="{tags}\n"']
_TEST_AVAILABLE_COMMAND: ClassVar[List[str]] = ["hg", "root"]
_COMMIT_COMMAND: ClassVar[List[str]] = ["hg", "commit", "--logfile"]
_ALL_TAGS_COMMAND: ClassVar[List[str]] = ["hg", "log", '--rev="tag()"', '--template="{tags}\n"']

def __init__(self, config: SCMConfig):
self.config = config
Expand Down Expand Up @@ -54,7 +54,7 @@ def add_path(self, path: Pathlike) -> None:
"""Add a path to the Source Control Management repository."""
pass

def commit_and_tag(self, files: list[Pathlike], context: MutableMapping, dry_run: bool = False) -> None:
def commit_and_tag(self, files: List[Pathlike], context: MutableMapping, dry_run: bool = False) -> None:
"""Commit and tag files to the repository using the configuration."""

def tag(self, name: str, sign: bool = False, message: Optional[str] = None) -> None:
Expand Down
12 changes: 6 additions & 6 deletions bumpversion/scm/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ def add_path(self, path: Pathlike) -> None:
"""Add a path to the pending commit."""
...

def get_all_tags(self) -> list[str]:
def get_all_tags(self) -> List[str]:
"""Return all tags in the SCM."""
...

def commit_and_tag(self, files: list[Pathlike], context: MutableMapping, dry_run: bool = False) -> None:
def commit_and_tag(self, files: List[Pathlike], context: MutableMapping, dry_run: bool = False) -> None:
"""Commit and tag files to the repository using the configuration."""
...

Expand Down Expand Up @@ -132,11 +132,11 @@ def add_path(self, path: Pathlike) -> None:
"""Add a path to the pending commit."""
logger.debug("No source code management system configured. Skipping adding path '%s'.", path)

def get_all_tags(self) -> list[str]:
def get_all_tags(self) -> List[str]:
"""Return all tags in the SCM."""
return []

def commit_and_tag(self, files: list[Pathlike], context: MutableMapping, dry_run: bool = False) -> None:
def commit_and_tag(self, files: List[Pathlike], context: MutableMapping, dry_run: bool = False) -> None:
"""Pretend to commit and tag files to the repository using the configuration."""
logger.debug("No source code management system configured. Skipping committing and tagging.")

Expand Down Expand Up @@ -210,7 +210,7 @@ def path_in_repo(self, path: Pathlike) -> bool:
return True
return str(path).startswith(str(self.repository_root))

def commit_and_tag(self, files: list[Pathlike], context: MutableMapping, dry_run: bool = False) -> None:
def commit_and_tag(self, files: List[Pathlike], context: MutableMapping, dry_run: bool = False) -> None:
"""Commit the files to the source code management system."""
logger.indent()

Expand All @@ -227,7 +227,7 @@ def commit_and_tag(self, files: list[Pathlike], context: MutableMapping, dry_run
self.tool.commit_and_tag(files=files, context=context, dry_run=dry_run)
logger.dedent()

def _commit(self, files: list[Pathlike], context: MutableMapping, dry_run: bool = False) -> None:
def _commit(self, files: List[Pathlike], context: MutableMapping, dry_run: bool = False) -> None:
"""Commit the files to the source code management system."""
do_commit = not dry_run
logger.info("%s the commit", "Preparing" if do_commit else "Would prepare")
Expand Down
43 changes: 43 additions & 0 deletions tests/test_bump.py
Original file line number Diff line number Diff line change
Expand Up @@ -692,3 +692,46 @@ def test_changes_to_files_are_committed(git_repo: Path, caplog):
with inside_dir(git_repo):
status = run_command(["git", "status", "--porcelain"])
assert status.stdout == ""


def test_empty_files_config_is_ignored(git_repo: Path, caplog):
"""An empty files config should be ignored."""
# Arrange
config_path = git_repo / ".bumpversion.toml"
config_path.write_text(
dedent(
"""
[tool.bumpversion]
current_version = "0.1.26"
tag_name = "{new_version}"
commit = true
[[tool.bumpversion.files]]
"""
),
encoding="utf-8",
)
with inside_dir(git_repo):
run_command(["git", "add", str(config_path)])
run_command(["git", "commit", "-m", "Initial commit"])
run_command(["git", "tag", "0.1.26"])

# Act
runner: CliRunner = CliRunner()
with inside_dir(git_repo):
result: Result = runner.invoke(
cli.cli,
["bump", "-vv", "minor"],
)

if result.exit_code != 0:
print(caplog.text)
print("Here is the output:")
print(result.output)
import traceback

print(traceback.print_exception(*result.exc_info))

# Assert
assert result.exit_code == 0
Loading

0 comments on commit c748828

Please sign in to comment.