Skip to content

refactor: improve types and avoid nested loop, add types for untyped functions #1466

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 30 commits into
base: refactors
Choose a base branch
from

Conversation

bearomorphism
Copy link
Contributor

@bearomorphism bearomorphism commented May 29, 2025

Description

  • Performance improvement: make possible_tags a set[str] to avoid linear-time search for each candidate tag.
  • Types: make the return type more strict (e.g. from Sequence to list) and make the parameter type as abstract as possible (e.g. from list to Sequence or Iterable)
  • Add types for all untyped functions except smart_open.
  • Added some # type: ignore to bypass mypy check. We can address those potential errors in the future?
  • Added some cast also for bypassing mypy check.
  • Make some methods protected.
  • Make ruff rules more strict.
  • Add types for dictionary arguments in commands.

Note that I didn't run mypy --disallow-untyped-defs tests/ because typing tests/ does not help much for development.

Before (4b6b3fb)

Very long error messages after running mypy
commitizen-py3.13➜  commitizen git:(master) mypy commitizen/ tests/
commitizen/config/base_config.py:10: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
commitizen/config/base_config.py:12: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
commitizen/exceptions.py:45: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
commitizen/commands/commit.py:96: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
commitizen/commands/commit.py:98: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
commitizen/commands/commit.py:100: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
commitizen/commands/commit.py:101: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
commitizen/commands/commit.py:102: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
commitizen/commands/commit.py:104: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
commitizen/commands/commit.py:114: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
commitizen/commands/commit.py:115: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
commitizen/commands/commit.py:116: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
commitizen/commands/commit.py:141: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
commitizen/commands/commit.py:142: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
commitizen/commands/changelog.py:164: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
commitizen/commands/changelog.py:165: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
commitizen/commands/changelog.py:168: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
commitizen/commands/changelog.py:229: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
Success: no issues found in 109 source files
commitizen-py3.13➜  commitizen git:(master) mypy --disallow-untyped-defs commitizen
commitizen/out.py:11: error: Function is missing a type annotation for one or more arguments  [no-untyped-def]
commitizen/out.py:16: error: Function is missing a type annotation for one or more arguments  [no-untyped-def]
commitizen/out.py:36: error: Function is missing a return type annotation  [no-untyped-def]
commitizen/config/base_config.py:9: error: Function is missing a return type annotation  [no-untyped-def]
commitizen/config/base_config.py:9: note: Use "-> None" if function does not return a value
commitizen/config/base_config.py:10: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
commitizen/config/base_config.py:12: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
commitizen/config/base_config.py:22: error: Function is missing a type annotation  [no-untyped-def]
commitizen/providers/base_provider.py:32: error: Function is missing a return type annotation  [no-untyped-def]
commitizen/providers/base_provider.py:61: error: Function is missing a return type annotation  [no-untyped-def]
commitizen/providers/base_provider.py:69: error: Function is missing a return type annotation  [no-untyped-def]
commitizen/providers/base_provider.py:82: error: Function is missing a return type annotation  [no-untyped-def]
commitizen/providers/base_provider.py:90: error: Function is missing a return type annotation  [no-untyped-def]
commitizen/providers/poetry_provider.py:18: error: Function is missing a return type annotation  [no-untyped-def]
commitizen/providers/commitizen_provider.py:14: error: Function is missing a return type annotation  [no-untyped-def]
commitizen/providers/cargo_provider.py:31: error: Function is missing a return type annotation  [no-untyped-def]
commitizen/exceptions.py:43: error: Function is missing a type annotation  [no-untyped-def]
commitizen/exceptions.py:45: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
commitizen/exceptions.py:53: error: Function is missing a type annotation  [no-untyped-def]
commitizen/exceptions.py:60: error: Function is missing a type annotation  [no-untyped-def]
commitizen/cmd.py:31: error: Function is missing a type annotation for one or more arguments  [no-untyped-def]
commitizen/git.py:39: error: Function is missing a type annotation for one or more arguments  [no-untyped-def]
commitizen/git.py:46: error: Function is missing a type annotation for one or more arguments  [no-untyped-def]
commitizen/git.py:63: error: Function is missing a return type annotation  [no-untyped-def]
commitizen/git.py:66: error: Function is missing a type annotation  [no-untyped-def]
commitizen/git.py:71: error: Function is missing a type annotation  [no-untyped-def]
commitizen/git.py:76: error: Function is missing a type annotation  [no-untyped-def]
commitizen/git.py:80: error: Function is missing a return type annotation  [no-untyped-def]
commitizen/git.py:163: error: Function is missing a return type annotation  [no-untyped-def]
commitizen/git.py:291: error: Function is missing a type annotation  [no-untyped-def]
commitizen/cz/base.py:79: error: Function is missing a return type annotation  [no-untyped-def]
commitizen/hooks.py:9: error: Function is missing a type annotation  [no-untyped-def]
commitizen/config/yaml_config.py:20: error: Function is missing a return type annotation  [no-untyped-def]
commitizen/config/yaml_config.py:20: note: Use "-> None" if function does not return a value
commitizen/config/yaml_config.py:44: error: Function is missing a type annotation  [no-untyped-def]
commitizen/config/toml_config.py:20: error: Function is missing a return type annotation  [no-untyped-def]
commitizen/config/toml_config.py:20: note: Use "-> None" if function does not return a value
commitizen/config/toml_config.py:33: error: Function is missing a type annotation  [no-untyped-def]
commitizen/config/json_config.py:19: error: Function is missing a return type annotation  [no-untyped-def]
commitizen/config/json_config.py:19: note: Use "-> None" if function does not return a value
commitizen/config/json_config.py:23: error: Function is missing a type annotation  [no-untyped-def]
commitizen/cz/utils.py:9: error: Function is missing a type annotation  [no-untyped-def]
commitizen/cz/utils.py:15: error: Function is missing a type annotation  [no-untyped-def]
commitizen/changelog.py:66: error: Function is missing a return type annotation  [no-untyped-def]
commitizen/changelog.py:66: note: Use "-> None" if function does not return a value
commitizen/changelog.py:166: error: Function is missing a return type annotation  [no-untyped-def]
commitizen/changelog.py:221: error: Function is missing a type annotation for one or more arguments  [no-untyped-def]
commitizen/providers/scm_provider.py:26: error: Function is missing a return type annotation  [no-untyped-def]
commitizen/cz/conventional_commits/conventional_commits.py:12: error: Function is missing a type annotation  [no-untyped-def]
commitizen/cz/conventional_commits/conventional_commits.py:23: error: Function is missing a type annotation  [no-untyped-def]
commitizen/commands/list_cz.py:9: error: Function is missing a type annotation for one or more arguments  [no-untyped-def]
commitizen/commands/list_cz.py:12: error: Function is missing a return type annotation  [no-untyped-def]
commitizen/commands/list_cz.py:12: note: Use "-> None" if function does not return a value
commitizen/commands/schema.py:8: error: Function is missing a type annotation for one or more arguments  [no-untyped-def]
commitizen/commands/schema.py:12: error: Function is missing a return type annotation  [no-untyped-def]
commitizen/commands/schema.py:12: note: Use "-> None" if function does not return a value
commitizen/commands/init.py:82: error: Function is missing a type annotation for one or more arguments  [no-untyped-def]
commitizen/commands/init.py:88: error: Function is missing a return type annotation  [no-untyped-def]
commitizen/commands/init.py:88: note: Use "-> None" if function does not return a value
commitizen/commands/init.py:210: error: Function is missing a type annotation for one or more arguments  [no-untyped-def]
commitizen/commands/init.py:305: error: Function is missing a return type annotation  [no-untyped-def]
commitizen/commands/init.py:326: error: Function is missing a return type annotation  [no-untyped-def]
commitizen/commands/init.py:372: error: Function is missing a return type annotation  [no-untyped-def]
commitizen/commands/info.py:8: error: Function is missing a type annotation for one or more arguments  [no-untyped-def]
commitizen/commands/info.py:12: error: Function is missing a return type annotation  [no-untyped-def]
commitizen/commands/info.py:12: note: Use "-> None" if function does not return a value
commitizen/commands/example.py:8: error: Function is missing a type annotation for one or more arguments  [no-untyped-def]
commitizen/commands/example.py:12: error: Function is missing a return type annotation  [no-untyped-def]
commitizen/commands/example.py:12: note: Use "-> None" if function does not return a value
commitizen/commands/commit.py:95: error: Function is missing a return type annotation  [no-untyped-def]
commitizen/commands/commit.py:95: note: Use "-> None" if function does not return a value
commitizen/commands/commit.py:96: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
commitizen/commands/commit.py:98: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
commitizen/commands/commit.py:100: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
commitizen/commands/commit.py:101: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
commitizen/commands/commit.py:102: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
commitizen/commands/commit.py:104: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
commitizen/commands/commit.py:114: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
commitizen/commands/commit.py:115: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
commitizen/commands/commit.py:116: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
commitizen/commands/commit.py:141: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
commitizen/commands/commit.py:142: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
commitizen/commands/check.py:20: error: Function is missing a type annotation for one or more arguments  [no-untyped-def]
commitizen/commands/check.py:51: error: Function is missing a return type annotation  [no-untyped-def]
commitizen/commands/check.py:51: note: Use "-> None" if function does not return a value
commitizen/commands/check.py:64: error: Function is missing a return type annotation  [no-untyped-def]
commitizen/commands/check.py:64: note: Use "-> None" if function does not return a value
commitizen/commands/check.py:100: error: Function is missing a return type annotation  [no-untyped-def]
commitizen/commands/version.py:13: error: Function is missing a type annotation for one or more arguments  [no-untyped-def]
commitizen/commands/version.py:19: error: Function is missing a return type annotation  [no-untyped-def]
commitizen/commands/version.py:19: note: Use "-> None" if function does not return a value
commitizen/commands/changelog.py:31: error: Function is missing a type annotation for one or more arguments  [no-untyped-def]
commitizen/commands/changelog.py:135: error: Function is missing a return type annotation  [no-untyped-def]
commitizen/commands/changelog.py:153: error: Function is missing a return type annotation  [no-untyped-def]
commitizen/commands/changelog.py:153: note: Use "-> None" if function does not return a value
commitizen/commands/changelog.py:158: error: Function is missing a return type annotation  [no-untyped-def]
commitizen/commands/changelog.py:164: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
commitizen/commands/changelog.py:165: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
commitizen/commands/changelog.py:168: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
commitizen/commands/changelog.py:229: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
commitizen/cli.py:47: error: Function is missing a return type annotation  [no-untyped-def]
commitizen/cli.py:552: error: Function is missing a return type annotation  [no-untyped-def]
commitizen/cli.py:552: error: Function is missing a type annotation for one or more arguments  [no-untyped-def]
commitizen/cli.py:598: error: Function is missing a return type annotation  [no-untyped-def]
commitizen/cli.py:598: note: Use "-> None" if function does not return a value
Found 71 errors in 28 files (checked 57 source files)

After

commitizen-py3.13➜  commitizen git:(better-type) mypy commitizen/ tests/
Success: no issues found in 110 source files
commitizen-py3.13➜  commitizen git:(better-type) mypy --disallow-untyped-defs commitizen
Success: no issues found in 57 source files

Checklist

Code Changes

  • Add test cases to all the changes you introduce
  • Run poetry all locally to ensure this change passes linter check and tests
  • Manually test the changes:
    • Verify the feature/bug fix works as expected in real-world scenarios
    • Test edge cases and error conditions
    • Ensure backward compatibility is maintained
    • Document any manual testing steps performed
  • Update the documentation for the changes

Copy link

codecov bot commented May 29, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.87%. Comparing base (59fd3f5) to head (667efef).

Additional details and impacted files
@@              Coverage Diff              @@
##           refactors    #1466      +/-   ##
=============================================
+ Coverage      97.82%   97.87%   +0.05%     
=============================================
  Files             57       57              
  Lines           2619     2683      +64     
=============================================
+ Hits            2562     2626      +64     
  Misses            57       57              
Flag Coverage Δ
unittests 97.87% <100.00%> (+0.05%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@bearomorphism bearomorphism changed the title style(tags): improve types perf(tags): improve types and avoid nested loop May 29, 2025
@bearomorphism bearomorphism changed the title perf(tags): improve types and avoid nested loop perf(tags): improve types and avoid nested loop, fix mypy warnings May 29, 2025
Comment on lines 156 to 159
src = Path(tpl.filename)
Path(self.export_template_to).write_text(src.read_text())
src = Path(tpl.filename) # type: ignore
Path(self.export_template_to).write_text(src.read_text()) # type: ignore
Copy link
Contributor Author

@bearomorphism bearomorphism May 29, 2025

Choose a reason for hiding this comment

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

These should be fixed. tpl.filename is str | None.

Copy link
Member

Choose a reason for hiding this comment

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

let's add a TODO comment then

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also created an issue. This can probably be good first?

@bearomorphism bearomorphism marked this pull request as draft May 29, 2025 15:39
@bearomorphism bearomorphism marked this pull request as ready for review May 29, 2025 16:16
@bearomorphism bearomorphism force-pushed the better-type branch 2 times, most recently from 3e41480 to fe0ae1a Compare May 29, 2025 16:19
@bearomorphism bearomorphism changed the title perf(tags): improve types and avoid nested loop, fix mypy warnings refactor(tags): improve types and avoid nested loop, fix mypy warnings May 29, 2025
@bearomorphism bearomorphism changed the title refactor(tags): improve types and avoid nested loop, fix mypy warnings refactor(tags): improve types and avoid nested loop, add types for untyped functions May 29, 2025
@bearomorphism bearomorphism mentioned this pull request May 29, 2025
10 tasks
@bearomorphism bearomorphism changed the title refactor(tags): improve types and avoid nested loop, add types for untyped functions refactor: improve types and avoid nested loop, add types for untyped functions May 29, 2025
@@ -676,7 +679,7 @@ def main():
)
sys.excepthook = no_raise_debug_excepthook

args.func(conf, arguments)()
args.func(conf, arguments)() # type: ignore
Copy link
Member

Choose a reason for hiding this comment

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

why is it needed? could we try to narrow it a bit e.g., type: ignore[call-args]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Let me check. Maybe we don't need this after #1479

Copy link
Contributor Author

Choose a reason for hiding this comment

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

commitizen/cli.py:682: error: Argument 2 to "Commit" has incompatible type "dict[str, Any]"; expected "CommitArgs"  [arg-type]
commitizen/cli.py:682: error: Argument 2 to "Bump" has incompatible type "dict[str, Any]"; expected "BumpArgs"  [arg-type]
commitizen/cli.py:682: error: Argument 2 to "Changelog" has incompatible type "dict[str, Any]"; expected "ChangelogArgs"  [arg-type]
commitizen/cli.py:682: error: Argument 2 to "Check" has incompatible type "dict[str, Any]"; expected "CheckArgs"  [arg-type]
commitizen/cli.py:682: error: Argument 2 to "Version" has incompatible type "dict[str, Any]"; expected "VersionArgs"  [arg-type]

Copy link
Member

Choose a reason for hiding this comment

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

yep, let's add 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.

This was referenced Jun 5, 2025
Copy link
Member

@Lee-W Lee-W left a comment

Choose a reason for hiding this comment

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

mostly good, 2 nitpicks. will merge it later today

Comment on lines 234 to 236
self.changelog_flag = bool(
self.changelog_flag or self.changelog_to_stdout or self.changelog_config
)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
self.changelog_flag = bool(
self.changelog_flag or self.changelog_to_stdout or self.changelog_config
)
self.changelog_flag = any(
[self.changelog_flag, self.changelog_to_stdout, self.changelog_config]
)

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.

@Lee-W Lee-W added pr-status: ready-to-merge almost ready to merge. just keep it for a few days for others to check and removed pr-status: wait-for-review labels Jun 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr-status: ready-to-merge almost ready to merge. just keep it for a few days for others to check type: bug type: refactor
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants