Skip to content

feat: expose classmethod to build tortoise config#2162

Merged
waketzheng merged 13 commits intotortoise:developfrom
waketzheng:feat-typed-config
May 1, 2026
Merged

feat: expose classmethod to build tortoise config#2162
waketzheng merged 13 commits intotortoise:developfrom
waketzheng:feat-typed-config

Conversation

@waketzheng
Copy link
Copy Markdown
Contributor

@waketzheng waketzheng commented Mar 31, 2026

Description

  1. The init function of tortoise.context.Context is too long
  2. Add some new classmethods to TortoiseConfig which is used for sharing config between contexts

Motivation and Context

This function has too many lines; shorten it.

How Has This Been Tested?

make ci

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added the changelog accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Mar 31, 2026

Merging this PR will not alter performance

✅ 24 untouched benchmarks


Comparing waketzheng:feat-typed-config (9ea070b) with develop (f89618d)

Open in CodSpeed

@seladb
Copy link
Copy Markdown
Contributor

seladb commented Apr 5, 2026

@waketzheng this PR has many changes that are not related to the PR title and description. Maybe you should break them into multiple PRs?

abondar
abondar previously approved these changes Apr 5, 2026
@seladb
Copy link
Copy Markdown
Contributor

seladb commented Apr 5, 2026

@waketzheng this PR still seems to have unrelated changes. We have this PR to add pre-commit, and this PR for Starlette v1 support. These changes can probably be removed from this PR

@waketzheng
Copy link
Copy Markdown
Contributor Author

@waketzheng this PR still seems to have unrelated changes. We have this PR to add pre-commit, and this PR for Starlette v1 support. These changes can probably be removed from this PR

@seladb No need to do that. I expected this PR to be reviewed after the previous one merged, so I explicitly mentioned that it’s based on #2159. Actually, I refactored the code once when fixing mypy complaints after Starlette upgraded, and then separated it into three PRs.

@seladb
Copy link
Copy Markdown
Contributor

seladb commented Apr 6, 2026

@waketzheng this PR still seems to have unrelated changes. We have this PR to add pre-commit, and this PR for Starlette v1 support. These changes can probably be removed from this PR

@seladb No need to do that. I expected this PR to be reviewed after the previous one merged, so I explicitly mentioned that it’s based on #2159. Actually, I refactored the code once when fixing mypy complaints after Starlette upgraded, and then separated it into three PRs.

@waketzheng is there a reason to open these changes as stacked PRs? It looks like these are separate changes that can each have their own PR based on develop 🤔

@waketzheng
Copy link
Copy Markdown
Contributor Author

  1. Using stacked PRs avoids merge conflicts
  2. I can prek run --all-files to check this PR (prek is a reimagined version of pre-commit, built in Rust.)
  3. You can select the latest commit (instead of all commits) to compare the code between this PR and the previous one.
image image

@seladb
Copy link
Copy Markdown
Contributor

seladb commented Apr 6, 2026

  1. Using stacked PRs avoids merge conflicts

Yes, if the changes in the stacked PRs depend on each other, otherwise there shouldn't be merge conflicts. Here it looks like the actual commit related to this PR doesn't depend on the changes in the other PR. What am I missing here? 🤔

@waketzheng
Copy link
Copy Markdown
Contributor Author

Yes, if the changes in the stacked PRs depend on each other, otherwise there shouldn't be merge conflicts. Here it looks like the actual commit related to this PR doesn't depend on the changes in the other PR. What am I missing here? 🤔

You forgot that I need the pre-commit config to enforce code style.

@seladb
Copy link
Copy Markdown
Contributor

seladb commented Apr 7, 2026

You forgot that I need the pre-commit config to enforce code style.

I'm not sure why pre-commit is needed for this PR, but anyway - the pre-commit PR was merged so this PR is simpler now 🙂

Comment thread tortoise/config.py Outdated
Comment thread tortoise/config.py Outdated
@waketzheng waketzheng requested a review from seladb April 9, 2026 17:09
Copy link
Copy Markdown
Contributor

@seladb seladb left a comment

Choose a reason for hiding this comment

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

Please see one comment, otherwise LGTM

Comment thread tortoise/config.py Outdated
@waketzheng waketzheng requested a review from seladb April 14, 2026 17:18
Comment thread tortoise/config.py Outdated
Comment thread tortoise/config.py Outdated
)

@classmethod
def merge_args(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm not sure the method name is clear, merge_args seems to create a merged configuration from config, config_file, db_url and modules, but this is actually not the case.

Maybe generate_config could be a better name.

Moreover, maybe we need to normalize the classmethods in this class:

@classmethod
def generate_config(
    cls,
    config: dict[str, Any] | Self | None = None,
    config_file: str | None = None,
    db_url: str | None = None,
    modules: dict[str, Iterable[str | ModuleType]] | None = None,
) -> Self:
    ...

@classmethod
def generate_config_from_db_url_and_modules(cls, db_url: str, modules: dict[str, Iterable[str | ModuleType]]) -> Self:
    ...

@classmethod
def generate_config_from_config_file(cls, config_file: str) -> Self:
    ...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the suggestions. I’ve updated the naming:

  1. To keep consistent with from_dict, I’ll use from_db_url_and_modules / from_config_file.

  2. Since there’s already a parameter called config, I used resolve_args instead of generate_config to avoid confusion.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I like the from_* convention and it's consistent with the existing code. Maybe we can rename resolve_args to from_args? 🤔

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Maybe we can rename resolve_args to from_args?

That is one solution. However, I strongly recommend using resolve_* instead of from_* for this function, because:

  1. resolve_* checks whether arguments conflict, whereas from_* does not.
  2. resolve indicates that this function will parse the arguments, not just load configuration from them.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think from_* is clearer, but we can let @abondar decide...

Comment thread tortoise/config.py Outdated
Comment thread tortoise/cli/cli.py Outdated
@waketzheng waketzheng requested a review from seladb April 17, 2026 16:10
Copy link
Copy Markdown
Contributor

@seladb seladb left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Copy Markdown
Member

@abondar abondar left a comment

Choose a reason for hiding this comment

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

There is duplication of args in docstring
I think resolve_args is okay

We need to add tests regarding these new methods, even if simple ones

Otherwise - looks good to me, @waketzheng you can fix these issues and merge without seeking my explicit reapproval

Comment thread tortoise/config.py
Comment on lines +289 to +298
Args:
config (dict[str, Any] | TortoiseConfig | None):
config_file (str | None): Path to a config YAML or JSON file.
db_url (str | None): Database URL for config generation.
modules (dict[str, Iterable[str | ModuleType]] | None): App modules for config generation.
Args:
config: A configuration dict or TortoiseConfig instance.
config_file: Path to config file.
db_url: Database URL for config generation.
modules: App modules for config generation.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

args are duplicated

@waketzheng waketzheng merged commit 9d0e53e into tortoise:develop May 1, 2026
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants