Coverage for faststream/_internal/cli/utils/errors.py: 71%

10 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-17 21:20 +0000

1from typing import TYPE_CHECKING 

2 

3from typer.core import TyperOption 

4 

5from faststream.exceptions import StartupValidationError 

6 

7# type checkers see one click, or `TyperOption` stops matching the union of both 

8if TYPE_CHECKING: 

9 from typer._click import exceptions as click_exceptions 

10else: 

11 try: 

12 from typer._click import exceptions as click_exceptions 

13 except ImportError: # pragma: no cover - Typer < 0.26 

14 from click import exceptions as click_exceptions 

15 

16try: 

17 from typer.rich_utils import rich_format_error 

18except ImportError: # typer-slim ships without rich 

19 

20 def rich_format_error(self: click_exceptions.ClickException) -> None: 

21 self.show() 

22 

23 

24def draw_startup_errors(startup_exc: StartupValidationError) -> None: 

25 for field in startup_exc.invalid_fields: 

26 rich_format_error( 

27 click_exceptions.BadParameter( 

28 message=( 

29 "extra option in your application " 

30 "`lifespan/on_startup` hook has a wrong type." 

31 ), 

32 param=TyperOption(param_decls=[f"--{field}"]), 

33 ), 

34 ) 

35 

36 if startup_exc.missed_fields: 36 ↛ exitline 36 didn't return from function 'draw_startup_errors' because the condition on line 36 was always true

37 rich_format_error( 

38 click_exceptions.MissingParameter( 

39 message=( 

40 "You registered extra options in your application " 

41 "`lifespan/on_startup` hook, but does not set in CLI." 

42 ), 

43 param=TyperOption( 

44 param_decls=[f"--{x}" for x in startup_exc.missed_fields], 

45 ), 

46 ), 

47 )