Repository navigation
Tests for late registration in named sub-Typer applications #1991
First Check
Example Codeimport typer
from typer.testing import CliRunner
app = typer.Typer()
sub_app = typer.Typer()
events = []
def default_callback():
events.append("default")
def add_typer_callback():
events.append("add_typer")
sub_app = typer.Typer(callback=default_callback)
# Attach the child app first
app.add_typer(
sub_app,
name="child",
callback=add_typer_callback,
)
# Register the callback after attaching the child app
@sub_app.callback()
def child_callback():
events.append("child")
# Register a command after attaching the child app
@sub_app.command()
def hello():
print("Hello!")
runner = CliRunner()
result = runner.invoke(app, ["child", "hello"])
print("Exit code:", result.exit_code)
print("Output:", result.output)
print("Callbacks executed:", events)
assert result.exit_code == 0
assert events == ["add_typer"]
### Expected output
### ===============
# Exit code: 0
# Output: Hello!
# Callbacks executed: ['add_typer']DescriptionWhile exploring how I couldn't find existing tests specifically covering registration after a named child app has already been attached using I have added two tests to cover these scenarios:
Would these be useful additions to the test suite, or is this behavior already covered or intentionally left unspecified? Operating SystemLinux Operating System DetailsNo response Project Version0.27.3 Python Version3.10.2 Additional ContextI tested both scenarios locally. Both new tests pass, and the complete test suite passes with 1,476 passed, 29 skipped, and 2 xfailed. Python version: 3.10.12. No implementation changes have been made. The proposed contribution only adds tests to |
Replies: 1 comment
|
Both scenarios are already covered, through the docs examples rather than
So the precedence Unit-level tests in |
Both scenarios are already covered, through the docs examples rather than
tests/test_core.py. Every file underdocs_src/has a matching test undertests/test_tutorial/, and the Sub-Typer Callback Override tutorial uses exactly this "attach first, register later" order:docs_src/subcommands/callback_override/tutorial003_py310.pycreatesusers_app = typer.Typer(callback=default_callback), callsapp.add_typer(users_app, name="users"), and only then registers@users_app.callback()and@users_app.command() create.tests/test_tutorial/test_subcommands/test_callback_override/test_tutorial003.pychecks that the late command works and that the late@callback()overridesTyper(callback=...).tutor…