tortoise / tortoise/tortoise-orm

Tortoise.init_models does not raise ValueError on wrong input

Open
#901 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
5.6k
Forks
516
Avg merge
2d 21h
Merged PRs (30d)
9

Description

Describe the bug

I was using the Tortoise.init_models() module to initialize the orm, but it was not clear to me that the first parameter had to be a list, the doc says: "Models paths to initialise". To me this could be either a list of strings or just one string if only one module is used, which is what I did, I put the string "models" instead of passing the list ["models"] as first argument.

Unfortunately strings are iterable too, and I got stuck with error messages like "module m" not found and the likes, which was not clear to me that it was just the first letter of the sting I was passing as the argument.

From the source code (v 0.17.7) the method Tortoise.init_models is expecting basically a list of strings where each string represents the import statment for each module, but any iterable is acceptable, leading to inconsistent and unexpected result.
`
@classmethod
def init_models(
cls,
models_paths: Iterable[Union[ModuleType, str]],
app_label: str,
_init_relations: bool = True
) -> None:

    Early initialisation of Tortoise ORM Models.

    Initialise the relationships between Models.
    This does not initialise any database connection.
    :param models_paths: Models paths to initialise
    :param app_label: The app label, e.g. 'models'
    :param _init_relations: Whether to init relations or not

    :raises ConfigurationError: If models are invalid.

`

To Reproduce

just try:
Tortoise.init_models("models", app_label="models")
instead of:
Tortoise.init_models(["models"], app_label="models")

Expected behavior
I would expect a ValueError Meassage saying that a string was passed as argument, while a list was expected.
Plus, I think imho that the models_paths should be declared as a List, not as a generic Iterable.

Thank you very much for the effort

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the Tortoise.init_models entry point and reproduce the string and list calls shown in the issue. Verify that a string produces a clear ValueError while the list form continues to initialize models.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
database
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.