Coverage for tests / test_types.py: 100%

99 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-11-18 11:12 +0000

1from enum import Enum 1abcdefghij

2 

3import typer 1abcdefghij

4from typer.testing import CliRunner 1abcdefghij

5 

6app = typer.Typer(context_settings={"token_normalize_func": str.lower}) 1abcdefghij

7 

8 

9# Exercise the string enum style used by existing Typer apps. 

10class User(str, Enum): # noqa: UP042 1abcdefghij

11 rick = "Rick" 1abcdefghij

12 morty = "Morty" 1abcdefghij

13 

14 

15@app.command() 1abcdefghij

16def hello_option(name: User = User.rick) -> None: 1abcdefghij

17 print(f"Hello {name.value}!") 1abcdefghij

18 

19 

20@app.command() 1abcdefghij

21def hello_argument(name: User) -> None: 1abcdefghij

22 print(f"Hello {name.value}!") 1abcdefghij

23 

24 

25@app.command() 1abcdefghij

26def hello_no_choices( 1abcdefghij

27 name: User = typer.Option(..., "--name", show_choices=False), 

28): 

29 print(f"Hello {name.value}!") 1abcdefghij

30 

31 

32@app.command() 1abcdefghij

33def hello_all_options(name: list[str] = typer.Option(["World"])) -> None: 1abcdefghij

34 for n in name: 1abcdefghij

35 print(f"Hello {n}!") 1abcdefghij

36 

37 

38@app.command() 1abcdefghij

39def hello_all_args(names: list[str] = typer.Argument(["World"])) -> None: 1abcdefghij

40 for name in names: 1abcdefghij

41 print(f"Hello {name}!") 1abcdefghij

42 

43 

44@app.command() 1abcdefghij

45def hello_all_envvar( 1abcdefghij

46 names: list[str] = typer.Argument(["World"], envvar="NAMES"), 

47) -> None: 

48 for name in names: 1abcdefghij

49 print(f"Hello {name}!") 1abcdefghij

50 

51 

52@app.command() 1abcdefghij

53def split_variadic_and_pair(items: list[str], pair: tuple[str, str]) -> None: 1abcdefghij

54 print(f"items={items}") 1abcdefghij

55 print(f"pair={pair}") 1abcdefghij

56 

57 

58runner = CliRunner() 1abcdefghij

59 

60 

61def test_enum_choice() -> None: 1abcdefghij

62 result = runner.invoke( 1abcdefghij

63 app, ["hello-option", "--name", "morty"], catch_exceptions=False 

64 ) 

65 assert result.exit_code == 0 1abcdefghij

66 assert "Hello Morty!" in result.output 1abcdefghij

67 

68 result = runner.invoke(app, ["hello-option", "--name", "Rick"]) 1abcdefghij

69 assert result.exit_code == 0 1abcdefghij

70 assert "Hello Rick!" in result.output 1abcdefghij

71 

72 result = runner.invoke(app, ["hello-option", "--name", "RICK"]) 1abcdefghij

73 assert result.exit_code == 0 1abcdefghij

74 assert "Hello Rick!" in result.output 1abcdefghij

75 

76 result = runner.invoke(app, ["hello-no-choices", "--name", "RICK"]) 1abcdefghij

77 assert result.exit_code == 0 1abcdefghij

78 assert "Hello Rick!" in result.output 1abcdefghij

79 

80 result = runner.invoke(app, ["hello-argument", "RICK"]) 1abcdefghij

81 assert result.exit_code == 0 1abcdefghij

82 assert "Hello Rick!" in result.output 1abcdefghij

83 

84 

85def test_enum_choice_help() -> None: 1abcdefghij

86 result = runner.invoke(app, ["hello-argument", "--help"]) 1abcdefghij

87 assert result.exit_code == 0 1abcdefghij

88 assert "<rick|morty>" in result.output 1abcdefghij

89 

90 result = runner.invoke(app, ["hello-option", "--help"]) 1abcdefghij

91 assert result.exit_code == 0 1abcdefghij

92 assert "<rick|morty>" in result.output 1abcdefghij

93 

94 result = runner.invoke(app, ["hello-no-choices", "--help"]) 1abcdefghij

95 assert result.exit_code == 0 1abcdefghij

96 assert "--name" in result.output 1abcdefghij

97 assert "rick|morty" not in result.output 1abcdefghij

98 

99 

100def test_enum_choice_missing_message() -> None: 1abcdefghij

101 result = runner.invoke(app, ["hello-argument"]) 1abcdefghij

102 assert result.exit_code != 0 1abcdefghij

103 assert "Missing argument" in result.output 1abcdefghij

104 assert "Choose from:" in result.output 1abcdefghij

105 assert "rick" in result.output 1abcdefghij

106 assert "morty" in result.output 1abcdefghij

107 

108 

109def test_list() -> None: 1abcdefghij

110 result = runner.invoke( 1abcdefghij

111 app, ["hello-all-options", "--name", "Rick", "--name", "Morty"] 

112 ) 

113 assert result.exit_code == 0 1abcdefghij

114 assert "Hello World!" not in result.output 1abcdefghij

115 assert "Hello Rick!" in result.output 1abcdefghij

116 assert "Hello Morty!" in result.output 1abcdefghij

117 

118 result = runner.invoke(app, ["hello-all-args", "Rick", "Morty"]) 1abcdefghij

119 assert result.exit_code == 0 1abcdefghij

120 assert "Hello World!" not in result.output 1abcdefghij

121 assert "Hello Rick!" in result.output 1abcdefghij

122 assert "Hello Morty!" in result.output 1abcdefghij

123 

124 

125def test_list_empty() -> None: 1abcdefghij

126 result = runner.invoke(app, ["hello-all-args"]) 1abcdefghij

127 assert result.exit_code == 0 1abcdefghij

128 assert "Hello World!" in result.output 1abcdefghij

129 

130 result = runner.invoke(app, ["hello-all-options"]) 1abcdefghij

131 assert result.exit_code == 0 1abcdefghij

132 assert "Hello World!" in result.output 1abcdefghij

133 

134 result = runner.invoke(app, ["hello-all-envvar"]) 1abcdefghij

135 assert result.exit_code == 0 1abcdefghij

136 assert "Hello World!" in result.output 1abcdefghij

137 

138 

139def test_split_envvar_value(monkeypatch) -> None: 1abcdefghij

140 # This will use split_envvar_value to produce two strings from the envvar 

141 monkeypatch.setenv("NAMES", "Rick Morty") 1abcdefghij

142 result = runner.invoke(app, ["hello-all-envvar"]) 1abcdefghij

143 assert result.exit_code == 0 1abcdefghij

144 assert "Hello Rick!" in result.output 1abcdefghij

145 assert "Hello Morty!" in result.output 1abcdefghij

146 

147 

148def test_list_pair() -> None: 1abcdefghij

149 result = runner.invoke(app, ["split-variadic-and-pair", "a", "b", "c", "x", "y"]) 1abcdefghij

150 assert result.exit_code == 0 1abcdefghij

151 assert "items=['a', 'b', 'c']" in result.output 1abcdefghij

152 assert "pair=('x', 'y')" in result.output 1abcdefghij