feat: expose classmethod to build tortoise config#2162
feat: expose classmethod to build tortoise config#2162waketzheng merged 13 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
| ) | ||
|
|
||
| @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:
...There was a problem hiding this comment.
Thanks for the suggestions. I’ve updated the naming:
-
To keep consistent with
from_dict, I’ll usefrom_db_url_and_modules/from_config_file. -
Since there’s already a parameter called
config, I usedresolve_argsinstead ofgenerate_configto avoid confusion.
There was a problem hiding this comment.
I like the from_* convention and it's consistent with the existing code. Maybe we can rename resolve_args to from_args? 🤔
There was a problem hiding this comment.
Maybe we can rename
resolve_argstofrom_args?
That is one solution. However, I strongly recommend using resolve_* instead of from_* for this function, because:
resolve_*checks whether arguments conflict, whereasfrom_*does not.resolveindicates that this function will parse the arguments, not just load configuration from them.
There was a problem hiding this comment.
I think from_* is clearer, but we can let @abondar decide...
abondar
left a comment
There was a problem hiding this comment.
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
| 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. |


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