Skip to content

refactor(BaseConfig): update function name, upgrade mypy version #1444

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions commitizen/config/base_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ def settings(self) -> Settings:
def path(self) -> Path | None:
return self._path

"""
mypy does not like the following until 1.16
See https://github.com/python/mypy/pull/18510
TODO: update this when 1.16 is available

@path.setter
def path(self, path: str | Path) -> None:
self._path = Path(path)
"""

def update_path(self, path: str | Path) -> None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or maybe let's use setter here with type ignore and update it after 1.16. WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good

self._path = Path(path)

def set_key(self, key, value):
"""Set or update a key in the conf.

Expand All @@ -30,8 +43,5 @@ def set_key(self, key, value):
def update(self, data: Settings) -> None:
self._settings.update(data)

def add_path(self, path: str | Path) -> None:
self._path = Path(path)

Comment on lines -33 to -35
Copy link
Contributor Author

@bearomorphism bearomorphism May 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just found this method name confusing. It's actually updating the path, right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the main reason we make it this way is that we want to handle both str and Path implicitly. We probably could use a setter if that's better

Copy link
Contributor Author

@bearomorphism bearomorphism May 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK I was trying the following

    @property
    def path(self) -> Path | None:
        return self._path

    @path.setter
    def path(self, path: str | Path) -> None:
        self._path = Path(path)

but mypy isn't happy

commitizen/config/yaml_config.py:17: error: Incompatible types in assignment (expression has type "Path | str", variable has type "Path | None")  [assignment]
commitizen/config/toml_config.py:17: error: Incompatible types in assignment (expression has type "Path | str", variable has type "Path | None")  [assignment]
commitizen/config/json_config.py:16: error: Incompatible types in assignment (expression has type "Path | str", variable has type "Path | None")  [assignment]

I'm taking a look at related issues and PRs. Updating mypy to 1.15.0 does not work for me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like python/mypy#18510 will be part of 1.16.

def _parse_setting(self, data: bytes | str) -> None:
raise NotImplementedError()
2 changes: 1 addition & 1 deletion commitizen/config/json_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class JsonConfig(BaseConfig):
def __init__(self, *, data: bytes | str, path: Path | str):
super().__init__()
self.is_empty_config = False
self.add_path(path)
self.update_path(path)
self._parse_setting(data)

def init_empty_config_content(self):
Expand Down
2 changes: 1 addition & 1 deletion commitizen/config/toml_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class TomlConfig(BaseConfig):
def __init__(self, *, data: bytes | str, path: Path | str):
super().__init__()
self.is_empty_config = False
self.add_path(path)
self.update_path(path)
self._parse_setting(data)

def init_empty_config_content(self):
Expand Down
2 changes: 1 addition & 1 deletion commitizen/config/yaml_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class YAMLConfig(BaseConfig):
def __init__(self, *, data: bytes | str, path: Path | str):
super().__init__()
self.is_empty_config = False
self.add_path(path)
self.update_path(path)
self._parse_setting(data)

def init_empty_config_content(self):
Expand Down
108 changes: 51 additions & 57 deletions poetry.lock

Large diffs are not rendered by default.

14 changes: 3 additions & 11 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ deprecated = "^1.2.13"
[tool.poetry.group.linters.dependencies]
ruff = ">=0.5.0,<0.10.0"
pre-commit = ">=2.18,<5.0"
mypy = "^1.4"
mypy = "^1.15.0"
types-deprecated = "^1.2.9.2"
types-python-dateutil = "^2.8.19.13"
types-PyYAML = ">=5.4.3,<7.0.0"
Expand Down Expand Up @@ -250,12 +250,7 @@ cover.help = "Run the test suite with coverage"
cover.ref = "test --cov-report term-missing --cov-report=xml:coverage.xml --cov=commitizen"

all.help = "Run all tasks"
all.sequence = [
"format",
"lint",
"cover",
"check-commit",
]
all.sequence = ["format", "lint", "cover", "check-commit"]

"doc:screenshots".help = "Render documentation screeenshots"
"doc:screenshots".script = "scripts.gen_cli_help_screenshots:gen_cli_help_screenshots"
Expand All @@ -267,10 +262,7 @@ doc.help = "Live documentation server"
doc.cmd = "mkdocs serve"

ci.help = "Run all tasks in CI"
ci.sequence = [
{ cmd = "pre-commit run --all-files" },
"cover",
]
ci.sequence = [{ cmd = "pre-commit run --all-files" }, "cover"]
ci.env = { SKIP = "no-commit-to-branch" }

setup-pre-commit.help = "Install pre-commit hooks"
Expand Down
2 changes: 1 addition & 1 deletion tests/commands/test_init_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def test_init_without_setup_pre_commit_hook(tmpdir, mocker: MockFixture, config)
def test_init_when_config_already_exists(config, capsys):
# Set config path
path = os.sep.join(["tests", "pyproject.toml"])
config.add_path(path)
config.update_path(path)

commands.Init(config)()
captured = capsys.readouterr()
Expand Down
4 changes: 3 additions & 1 deletion tests/test_changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -1641,7 +1641,9 @@ def test_tags_rules_get_version_tags(capsys: pytest.CaptureFixture):

def test_changelog_file_name_from_args_and_config():
mock_config = Mock(spec=BaseConfig)
mock_config.path.parent = "/my/project"
mock_path = Mock(spec=Path)
mock_path.parent = Path("/my/project")
mock_config.path = mock_path
mock_config.settings = {
"name": "cz_conventional_commits",
"changelog_file": "CHANGELOG.md",
Expand Down