Skip to content

feat: support pathlib.Path as argument of TortoiseConfig.from_config_file#2184

Open
waketzheng wants to merge 6 commits into
tortoise:developfrom
waketzheng:feat/support-Path-as-config-file
Open

feat: support pathlib.Path as argument of TortoiseConfig.from_config_file#2184
waketzheng wants to merge 6 commits into
tortoise:developfrom
waketzheng:feat/support-Path-as-config-file

Conversation

@waketzheng
Copy link
Copy Markdown
Contributor

Description

To support Path as argument of TortoiseConfig.from_config_file/resolve_args, for example:

from pathlib import Path

filepath = Path('/path/to/config.json')
config = TortoiseConfig.from_config_file(filepath)
config2 = TortoiseConfig.resolve_args(config_file=filepath)

assert config == config2 == TortoiseConfig.from_config_file(str(filepath))

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented May 1, 2026

Merging this PR will not alter performance

✅ 24 untouched benchmarks


Comparing waketzheng:feat/support-Path-as-config-file (a6316e1) with develop (6dabe86)

Open in CodSpeed

@waketzheng waketzheng requested a review from abondar May 1, 2026 16:57
Comment thread tests/test_config.py Outdated
Comment thread tests/test_config.py Outdated
Comment thread tests/test_config.py
Comment on lines +102 to +114
yaml_file = file.with_suffix(".yml")
with yaml_file.open("w") as f:
yaml.safe_dump(dict(simple), f, default_flow_style=False)
yaml_file_2 = file.with_suffix(".yaml")
with yaml_file_2.open("w") as f2:
yaml.safe_dump(simple, f2, default_flow_style=False)
assert (
TortoiseConfig.from_config_file(yaml_file)
== TortoiseConfig.from_config_file(str(yaml_file))
== TortoiseConfig.from_config_file(yaml_file_2)
== TortoiseConfig.from_config_file(file)
== TortoiseConfig.resolve_args(config_file=yaml_file)
)
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 we can move yaml to a separate test

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.

We need to verify that the JSON file and YAML file produce the same result when dumped from the same dictionary, so put them in a single function.

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.

Oh ok, I guess it's ok to leave it as is 👍

Comment thread tests/test_config.py Outdated
Comment thread tests/test_config.py Outdated
Comment on lines +117 to +130
simple = {
"connections": {"default": "sqlite://db.sqlite3"},
"apps": {
"app": {
"models": ["app.models"],
"default_connection": "default",
}
},
}
db_url = simple["connections"]["default"]
modules = {"app": simple["apps"]["app"]["models"]}
typed_config = TortoiseConfig.from_db_url_and_modules(db_url, modules)
assert typed_config == TortoiseConfig.resolve_args(db_url=db_url, modules=modules)
assert typed_config.apps == TortoiseConfig.from_dict(simple).apps
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.

nit: a nicer way to write this test:

def test_from_db_url_and_modules(self):
    db_url = "sqlite://db.sqlite3"
    models = ["app.models"]
    simple = {
        "connections": {"default": db_url},
        "apps": {
            "app": {
                "models": models,
                "default_connection": "default",
            }
        },
    }
    modules = {"app": models}

    typed_config = TortoiseConfig.from_db_url_and_modules(db_url, modules)
    assert typed_config == TortoiseConfig.resolve_args(db_url=db_url, modules=modules)
    assert typed_config.apps == TortoiseConfig.from_dict(simple).apps

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.

Updated to use fixture instead.

Comment thread tests/test_config.py Outdated
@waketzheng waketzheng requested a review from seladb May 15, 2026 00:37
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

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.

2 participants