Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

refactor: do not guess if changelog format is provided, make function private #1471

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 1 commit into
base: refactors
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
15 changes: 10 additions & 5 deletions commitizen/changelog_formats/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,17 @@ def get_changelog_format(
:raises FormatUnknown: if a non-empty name is provided but cannot be found in the known formats
"""
name: str | None = config.settings.get("changelog_format")
format: type[ChangelogFormat] | None = guess_changelog_format(filename)

if name and name in KNOWN_CHANGELOG_FORMATS:
format = KNOWN_CHANGELOG_FORMATS[name]
format = (
name and KNOWN_CHANGELOG_FORMATS.get(name) or _guess_changelog_format(filename)
)

if not format:
raise ChangelogFormatUnknown(f"Unknown changelog format '{name}'")

return format(config)


def guess_changelog_format(filename: str | None) -> type[ChangelogFormat] | None:
def _guess_changelog_format(filename: str | None) -> type[ChangelogFormat] | None:
Copy link
Member

Choose a reason for hiding this comment

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

We probably could add a deprecation warning to guess_changelog_format and remove it in v5

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed, please review

"""
Try guessing the file format from the filename.

Expand All @@ -91,3 +90,9 @@ def guess_changelog_format(filename: str | None) -> type[ChangelogFormat] | None
if filename.endswith(f".{alt_extension}"):
return format
return None


def __getattr__(name: str):
if name == "guess_changelog_format":
return _guess_changelog_format
raise AttributeError(f"module {__name__} has no attribute {name}")
4 changes: 1 addition & 3 deletions commitizen/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@ def committer_factory(config: BaseConfig) -> BaseCommitizen:
"""Return the correct commitizen existing in the registry."""
name: str = config.settings["name"]
Copy link
Member

Choose a reason for hiding this comment

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

probably no harm to keep it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

try:
_cz = registry[name](config)
return registry[name](config)
except KeyError:
msg_error = (
"The committer has not been found in the system.\n\n"
f"Try running 'pip install {name}'\n"
)
raise NoCommitizenFoundException(msg_error)
else:
return _cz
8 changes: 4 additions & 4 deletions tests/test_changelog_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@
from commitizen.changelog_formats import (
KNOWN_CHANGELOG_FORMATS,
ChangelogFormat,
_guess_changelog_format,
get_changelog_format,
guess_changelog_format,
)
from commitizen.config.base_config import BaseConfig
from commitizen.exceptions import ChangelogFormatUnknown


@pytest.mark.parametrize("format", KNOWN_CHANGELOG_FORMATS.values())
def test_guess_format(format: type[ChangelogFormat]):
assert guess_changelog_format(f"CHANGELOG.{format.extension}") is format
assert _guess_changelog_format(f"CHANGELOG.{format.extension}") is format
for ext in format.alternative_extensions:
assert guess_changelog_format(f"CHANGELOG.{ext}") is format
assert _guess_changelog_format(f"CHANGELOG.{ext}") is format


@pytest.mark.parametrize("filename", ("CHANGELOG", "NEWS", "file.unknown", None))
def test_guess_format_unknown(filename: str):
assert guess_changelog_format(filename) is None
assert _guess_changelog_format(filename) is None


@pytest.mark.parametrize(
Expand Down
6 changes: 5 additions & 1 deletion tests/test_defaults.py → tests/test_deprecated.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest

from commitizen import defaults
from commitizen import changelog_formats, defaults


def test_getattr_deprecated_vars():
Expand All @@ -15,6 +15,10 @@ def test_getattr_deprecated_vars():
assert defaults.change_type_order == defaults.CHANGE_TYPE_ORDER
assert defaults.encoding == defaults.ENCODING
assert defaults.name == defaults.DEFAULT_SETTINGS["name"]
assert (
changelog_formats._guess_changelog_format
== changelog_formats.guess_changelog_format
)

# Verify warning messages
assert len(record) == 7
Expand Down