feat: expose classmethod to build tortoise config#2162
feat: expose classmethod to build tortoise config#2162waketzheng wants to merge 9 commits intotortoise:developfrom
Conversation
|
@waketzheng this PR has many changes that are not related to the PR title and description. Maybe you should break them into multiple PRs? |
|
@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 |
@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 |
7e95e57 to
b1e1093
Compare
|
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. |
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 🙂 |
seladb
left a comment
There was a problem hiding this comment.
Please see one comment, otherwise LGTM
| return cls.from_dict(config_dict) | ||
|
|
||
| @classmethod | ||
| def _get_config_from_config_file(cls, config_file: str) -> TortoiseConfig: |
There was a problem hiding this comment.
Maybe we can make it a public method instead of private/protected?
There was a problem hiding this comment.
Maybe we can make it a public method instead of private/protected?
If it to be a public method, should generating config from db_url/modules also be public? For example:
230 else:
231 return cls.generate_config(db_url, modules)
232
233 @classmethod
234 def generate_config(
235 cls, db_url: str, modules: dict[str, Iterable[str | ModuleType]]
236 ) -> TortoiseConfig:
237 from tortoise.backends.base.config_generator import generate_config
238
239 config_dict = generate_config(db_url, app_modules=modules)
240 return cls.from_dict(config_dict)
241
242 @classmethod
243 def get_config_from_config_file(cls, config_file: str) -> TortoiseConfig:
|
|
||
| @classmethod | ||
| def generate_config(cls, db_url: str, modules: dict[str, Iterable[str | ModuleType]]) -> Self: | ||
| from tortoise.backends.base.config_generator import generate_config |
There was a problem hiding this comment.
Is there a reason not to move this import to the top of the file? 🤔
| ) | ||
|
|
||
| @classmethod | ||
| def merge_args( |
There was a problem hiding this comment.
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:
...| ) | ||
|
|
||
| @classmethod | ||
| def merge_args( |
There was a problem hiding this comment.
Also - since there are public methods, can we add docstrings to each one?
| if not config_value: | ||
| raise utils.CLIUsageError( | ||
| "You must specify TORTOISE_ORM in option or env, or pyproject.toml [tool.tortoise]", | ||
| ) |
There was a problem hiding this comment.
We can do something like this:
config_value = config_value or utils.tortoise_orm_config()
if not config_value:
raise utils.CLIUsageError(
"You must specify TORTOISE_ORM in option or env, or pyproject.toml [tool.tortoise]",
)
return utils.get_tortoise_config(config_value)

Description
initfunction of tortoise.context.Context is too longMotivation and Context
Base on #2159
This function has too many lines; shorten it.
How Has This Been Tested?
Checklist: