Coverage for tests / test_type_conversion.py: 100%

320 statements  

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

1import os 1abcdefghij

2import sys 1abcdefghij

3from datetime import datetime 1abcdefghij

4from enum import Enum 1abcdefghij

5from pathlib import Path 1abcdefghij

6from types import SimpleNamespace 1abcdefghij

7from typing import Annotated, Any, Literal, get_args, get_origin 1abcdefghij

8 

9import pytest 1abcdefghij

10import typer 1abcdefghij

11from typer import _click, _param_types 1abcdefghij

12from typer.core import _TyperParameter 1abcdefghij

13from typer.testing import CliRunner 1abcdefghij

14 

15from tests.utils import needs_linux, needs_windows 1abcdefghij

16 

17runner = CliRunner() 1abcdefghij

18 

19 

20def test_optional(): 1abcdefghij

21 app = typer.Typer() 1abcdefghij

22 

23 @app.command() 1abcdefghij

24 def opt(user: str | None = None): 1abcdefghij

25 if user: 1abcdefghij

26 print(f"User: {user}") 1abcdefghij

27 else: 

28 print("No user") 1abcdefghij

29 

30 result = runner.invoke(app) 1abcdefghij

31 assert result.exit_code == 0 1abcdefghij

32 assert "No user" in result.output 1abcdefghij

33 

34 result = runner.invoke(app, ["--user", "Camila"]) 1abcdefghij

35 assert result.exit_code == 0 1abcdefghij

36 assert "User: Camila" in result.output 1abcdefghij

37 

38 

39def test_union_type_optional(): 1abcdefghij

40 app = typer.Typer() 1abcdefghij

41 

42 @app.command() 1abcdefghij

43 def opt(user: str | None = None): 1abcdefghij

44 if user: 1abcdefghij

45 print(f"User: {user}") 1abcdefghij

46 else: 

47 print("No user") 1abcdefghij

48 

49 result = runner.invoke(app) 1abcdefghij

50 assert result.exit_code == 0 1abcdefghij

51 assert "No user" in result.output 1abcdefghij

52 

53 result = runner.invoke(app, ["--user", "Camila"]) 1abcdefghij

54 assert result.exit_code == 0 1abcdefghij

55 assert "User: Camila" in result.output 1abcdefghij

56 

57 

58def test_optional_tuple(): 1abcdefghij

59 app = typer.Typer() 1abcdefghij

60 

61 @app.command() 1abcdefghij

62 def opt(number: tuple[int, int] | None = None): 1abcdefghij

63 if number: 1abcdefghij

64 print(f"Number: {number}") 1abcdefghij

65 else: 

66 print("No number") 1abcdefghij

67 

68 result = runner.invoke(app) 1abcdefghij

69 assert result.exit_code == 0 1abcdefghij

70 assert "No number" in result.output 1abcdefghij

71 

72 result = runner.invoke(app, ["--number", "4", "2"]) 1abcdefghij

73 assert result.exit_code == 0 1abcdefghij

74 assert "Number: (4, 2)" in result.output 1abcdefghij

75 

76 

77def test_no_type(): 1abcdefghij

78 app = typer.Typer() 1abcdefghij

79 

80 @app.command() 1abcdefghij

81 def no_type(user): 1abcdefghij

82 print(f"User: {user}") 1abcdefghij

83 

84 result = runner.invoke(app, ["Camila"]) 1abcdefghij

85 assert result.exit_code == 0 1abcdefghij

86 assert "User: Camila" in result.output 1abcdefghij

87 

88 

89class SomeEnum(Enum): 1abcdefghij

90 ONE = "one" 1abcdefghij

91 TWO = "two" 1abcdefghij

92 THREE = "three" 1abcdefghij

93 

94 

95@pytest.mark.parametrize( 1abcdefghij

96 "type_annotation", 

97 [list[Path], list[SomeEnum], list[str]], 

98) 

99def test_list_parameters_convert_to_lists(type_annotation): 1abcdefghij

100 # Lists containing objects that are converted by Click (i.e. not Path or Enum) 

101 # should not be inadvertently converted to tuples 

102 expected_element_type = type_annotation.__args__[0] 1abcdefghij

103 app = typer.Typer() 1abcdefghij

104 

105 @app.command() 1abcdefghij

106 def list_conversion(container: type_annotation): 1abcdefghij

107 assert isinstance(container, list) 1abcdefghij

108 for element in container: 1abcdefghij

109 assert isinstance(element, expected_element_type) 1abcdefghij

110 

111 result = runner.invoke(app, ["one", "two", "three"]) 1abcdefghij

112 assert result.exit_code == 0 1abcdefghij

113 

114 

115def test_list_scalar_default_map() -> None: 1abcdefghij

116 app = typer.Typer(context_settings={"default_map": {"nums": 7}}) 1abcdefghij

117 

118 @app.command() 1abcdefghij

119 def main(nums: list[int] = typer.Option([])): 1abcdefghij

120 print(nums) 1abcdefghij

121 

122 result = runner.invoke(app, []) 1abcdefghij

123 assert result.exit_code == 0, result.output 1abcdefghij

124 assert result.output.strip() == "[7]" 1abcdefghij

125 

126 

127def test_list_requires_single_type() -> None: 1abcdefghij

128 app = typer.Typer() 1abcdefghij

129 

130 @app.command() 1abcdefghij

131 def main(items: list[str, int]): 1abcdefghij

132 print(items) # pragma: no cover 

133 

134 with pytest.raises(ValueError, match="Expected one list item type"): 1abcdefghij

135 runner.invoke(app, ["hello", "342"]) 1abcdefghij

136 

137 

138@pytest.mark.parametrize( 1abcdefghij

139 "type_annotation", 

140 [ 

141 tuple[str, str], 

142 tuple[str, Path], 

143 tuple[Path, Path], 

144 tuple[str, SomeEnum], 

145 tuple[SomeEnum, SomeEnum], 

146 ], 

147) 

148def test_tuple_parameter_elements_are_converted_recursively(type_annotation): 1abcdefghij

149 # Tuple elements that aren't converted by Click (i.e. Path or Enum) 

150 # should be recursively converted by Typer 

151 expected_element_types = type_annotation.__args__ 1abcdefghij

152 app = typer.Typer() 1abcdefghij

153 

154 @app.command() 1abcdefghij

155 def tuple_recursive_conversion(container: type_annotation): 1abcdefghij

156 assert isinstance(container, tuple) 1abcdefghij

157 for element, expected_type in zip( 1abcdefghij

158 container, expected_element_types, strict=True 

159 ): 

160 assert isinstance(element, expected_type) 1abcdefghij

161 

162 result = runner.invoke(app, ["one", "two"]) 1abcdefghij

163 assert result.exit_code == 0 1abcdefghij

164 

165 

166def test_tuple_wrong_arity(): 1abcdefghij

167 app = typer.Typer() 1abcdefghij

168 

169 @app.command() 1abcdefghij

170 def tuple_arity(value: tuple[str, str] = typer.Option(...)): 1abcdefghij

171 print(value) # pragma: no cover 

172 

173 result = runner.invoke(app, [], default_map={"value": ("only-one",)}) 1abcdefghij

174 assert result.exit_code == 2 1abcdefghij

175 assert "2 values are required, but 1 given." in result.output 1abcdefghij

176 

177 

178def test_tuple_rejects_non_sequence(monkeypatch): 1abcdefghij

179 app = typer.Typer(context_settings={"default_map": {"pair": "not_a_tuple"}}) 1abcdefghij

180 

181 @app.command() 1abcdefghij

182 def main(pair: tuple[str, str] = ("a", "b")): 1abcdefghij

183 print(pair) # pragma: no cover 

184 

185 result = runner.invoke(app, []) 1abcdefghij

186 assert result.exit_code == 2 1abcdefghij

187 assert "value is not a valid tuple" in result.output 1abcdefghij

188 

189 

190def test_custom_parse(): 1abcdefghij

191 app = typer.Typer() 1abcdefghij

192 

193 @app.command() 1abcdefghij

194 def custom_parser( 1abcdefghij

195 hex_value: int = typer.Argument(None, parser=lambda x: int(x, 0)), 

196 ): 

197 assert hex_value == 0x56 1abcdefghij

198 

199 result = runner.invoke(app, ["0x56"]) 1abcdefghij

200 assert result.exit_code == 0 1abcdefghij

201 

202 

203def test_custom_parse_value_error(): 1abcdefghij

204 app = typer.Typer() 1abcdefghij

205 

206 @app.command() 1abcdefghij

207 def custom_parser( 1abcdefghij

208 hex_value: int = typer.Argument(None, parser=lambda x: int(x, 0)), 

209 ): 

210 print(hex_value) # pragma: no cover 

211 

212 result = runner.invoke(app, ["not-a-hex"]) 1abcdefghij

213 assert result.exit_code == 2 1abcdefghij

214 assert "Invalid value" in result.output 1abcdefghij

215 assert "invalid literal for int()" in result.output 1abcdefghij

216 

217 

218def test_custom_parser_hex(): 1abcdefghij

219 app = typer.Typer() 1abcdefghij

220 

221 @app.command() 1abcdefghij

222 def custom_parser_hex( 1abcdefghij

223 hex_value: int = typer.Argument(None, parser=lambda x: int(x, 0)), 

224 ): 

225 assert hex_value == 0x56 1abcdefghij

226 

227 result = runner.invoke(app, ["0x56"]) 1abcdefghij

228 assert result.exit_code == 0 1abcdefghij

229 

230 

231@pytest.mark.parametrize( 1abcdefghij

232 ("cli_value", "expected"), 

233 [ 

234 ("true", True), 

235 ("false", False), 

236 ("yes", True), 

237 ("no", False), 

238 ("1", True), 

239 ("0", False), 

240 ("on", True), 

241 ("off", False), 

242 ("t", True), 

243 ("f", False), 

244 ("y", True), 

245 ("n", False), 

246 ("", False), 

247 (" true ", True), 

248 (" FALSE ", False), 

249 ("TRUE", True), 

250 ("No", False), 

251 ], 

252) 

253def test_bool_convert_valid(cli_value: str, expected: bool) -> None: 1abcdefghij

254 app = typer.Typer() 1abcdefghij

255 

256 @app.command() 1abcdefghij

257 def main( 1abcdefghij

258 value_1: bool, 

259 value_2: bool = typer.Option(not expected, envvar="MY_TYPER_BOOL"), 

260 ): 

261 print(f"value 1 {value_1}") 1abcdefghij

262 print(f"value 2 {value_2}") 1abcdefghij

263 

264 expected_2 = expected 1abcdefghij

265 # An empty env var counts as unset, so value_2 falls back to its default (True) 

266 if cli_value == "": 1abcdefghij

267 expected_2 = True 1abcdefghij

268 

269 result = runner.invoke(app, [cli_value], env={"MY_TYPER_BOOL": cli_value}) 1abcdefghij

270 assert result.exit_code == 0, result.output 1abcdefghij

271 assert f"value 1 {expected}" in result.output 1abcdefghij

272 assert f"value 2 {expected_2}" in result.output 1abcdefghij

273 

274 

275def test_bool_convert_invalid(): 1abcdefghij

276 app = typer.Typer() 1abcdefghij

277 

278 @app.command() 1abcdefghij

279 def main(value: bool): 1abcdefghij

280 print(value) # pragma: no cover 

281 

282 result = runner.invoke(app, ["maybe"]) 1abcdefghij

283 assert result.exit_code == 2 1abcdefghij

284 assert "Input should be a valid boolean" in result.output 1abcdefghij

285 

286 

287def test_bool_convert_invalid_envvar(): 1abcdefghij

288 app = typer.Typer() 1abcdefghij

289 

290 @app.command() 1abcdefghij

291 def main(value: bool = typer.Option(False, envvar="MY_TYPER_BOOL")): 1abcdefghij

292 print(value) # pragma: no cover 

293 

294 result = runner.invoke(app, [], env={"MY_TYPER_BOOL": "maybe"}) 1abcdefghij

295 assert result.exit_code == 2 1abcdefghij

296 assert "valid boolean, unable to interpret input" in result.output 1abcdefghij

297 

298 

299@pytest.mark.parametrize( 1abcdefghij

300 ("default_value", "expected"), 

301 [("yes", True), ("off", False)], 

302) 

303def test_bool_option_default_map_string(default_value: str, expected: bool) -> None: 1abcdefghij

304 app = typer.Typer() 1abcdefghij

305 

306 @app.command() 1abcdefghij

307 def main(value: bool = not expected): 1abcdefghij

308 print(f"value {value}") 1abcdefghij

309 

310 result = runner.invoke(app, [], default_map={"value": default_value}) 1abcdefghij

311 assert result.exit_code == 0, result.output 1abcdefghij

312 assert f"value {expected}" in result.output 1abcdefghij

313 

314 

315@pytest.mark.parametrize( 1abcdefghij

316 ("arg_enc", "system_enc", "raw_value", "expected_output"), 

317 [ 

318 pytest.param("latin-1", "utf-8", b"\xff", "ÿ"), 

319 pytest.param("ascii", "latin-1", b"\xff", "ÿ"), 

320 pytest.param("ascii", "utf-16", b"\xff", "�"), 

321 pytest.param("ascii", "ascii", b"\xff", "�"), 

322 ], 

323) 

324def test_string_param_type_converts_bytes( 1abcdefghij

325 monkeypatch: pytest.MonkeyPatch, 

326 arg_enc: str, 

327 system_enc: str, 

328 raw_value: bytes, 

329 expected_output: str, 

330): 

331 app = typer.Typer() 1abcdefghij

332 

333 @app.command() 1abcdefghij

334 def show(name: str = typer.Option(...)): 1abcdefghij

335 print(name) 1abcdefghij

336 

337 monkeypatch.setattr(_click._compat, "_get_argv_encoding", lambda: arg_enc) 1abcdefghij

338 monkeypatch.setattr(sys, "getfilesystemencoding", lambda: system_enc) 1abcdefghij

339 

340 result = runner.invoke(app, [], default_map={"name": raw_value}) 1abcdefghij

341 assert result.exit_code == 0 1abcdefghij

342 assert expected_output in result.output 1abcdefghij

343 

344 

345@pytest.mark.parametrize("path_type", [str, bytes, Path]) 1abcdefghij

346def test_path_coerced(path_type) -> None: 1abcdefghij

347 # Ensure coerce_path_result works correctly 

348 app = typer.Typer() 1abcdefghij

349 

350 @app.command() 1abcdefghij

351 def show(path: Any = typer.Option(..., path_type=path_type)): 1abcdefghij

352 print(path) 1abcdefghij

353 

354 result = runner.invoke(app, ["--path", "dir/my_awesome_file.txt"]) 1abcdefghij

355 assert result.exit_code == 0 1abcdefghij

356 assert "my_awesome_file" in result.output 1abcdefghij

357 

358 

359@pytest.mark.parametrize( 1abcdefghij

360 "annotation", 

361 [ 

362 Path, 

363 Any, 

364 pytest.param(str, marks=pytest.mark.xfail(strict=True)), 

365 ], 

366) 

367def test_path_resolves(tmp_path: Path, annotation: Any) -> None: 1abcdefghij

368 app = typer.Typer() 1abcdefghij

369 values: list[Any] = [] 1abcdefghij

370 

371 @app.command() 1abcdefghij

372 def main(loc: annotation = typer.Option(..., resolve_path=True)): 1abcdefghij

373 values.append(loc) 1abcdefghij

374 

375 file = tmp_path / "file.txt" 1abcdefghij

376 file.write_text("x", encoding="utf-8") 1abcdefghij

377 non_canonical = str(tmp_path / "first_dir" / "second_dir" / ".." / "file.txt") 1abcdefghij

378 

379 result = runner.invoke(app, ["--loc", non_canonical]) 1abcdefghij

380 

381 assert result.exit_code == 0 1abcdefghij

382 value = str(values[0]) 1abcdefghij

383 assert ".." not in value 1abcdefghij

384 assert "second_dir" not in value 1abcdefghij

385 assert str(tmp_path / "first_dir" / "file.txt") in value 1abcdefghij

386 

387 

388def test_path_coerced_from_default_map_path_object() -> None: 1abcdefghij

389 app = typer.Typer() 1abcdefghij

390 

391 @app.command() 1abcdefghij

392 def show(path: Path = typer.Option(..., path_type=Path)): 1abcdefghij

393 print(path) 1abcdefghij

394 

395 path_obj = Path("dir/my_awesome_file.txt") 1abcdefghij

396 result = runner.invoke(app, [], default_map={"path": path_obj}) 1abcdefghij

397 assert result.exit_code == 0, result.output 1abcdefghij

398 assert "my_awesome_file" in result.output 1abcdefghij

399 

400 

401def test_path_coerce_invalid_pathlike_default_map() -> None: 1abcdefghij

402 class BadPathLike: 1abcdefghij

403 def __fspath__(self) -> object: 1abcdefghij

404 return 123 # pragma: no cover 

405 

406 app = typer.Typer() 1abcdefghij

407 

408 @app.command() 1abcdefghij

409 def show(path: Path = typer.Option(..., path_type=Path)): 1abcdefghij

410 print(path) # pragma: no cover 

411 

412 result = runner.invoke(app, [], default_map={"path": BadPathLike()}) 1abcdefghij

413 assert result.exit_code == 2 1abcdefghij

414 assert "Invalid value for '--path'" in result.output 1abcdefghij

415 

416 

417@pytest.mark.parametrize( 1abcdefghij

418 ("create_file", "option_kwargs", "deny_mode", "expected_error"), 

419 [ 

420 (True, {"file_okay": False, "dir_okay": True}, None, "is a file"), 

421 (False, {"file_okay": True, "dir_okay": False}, None, "is a directory"), 

422 (True, {"readable": True}, os.R_OK, "is not readable"), 

423 (True, {"readable": False, "writable": True}, os.W_OK, "is not writable"), 

424 ], 

425) 

426def test_path_convert_failures( 1abcdefghij

427 monkeypatch: pytest.MonkeyPatch, 

428 tmp_path: Path, 

429 create_file: bool, 

430 option_kwargs: dict[str, bool], 

431 deny_mode: int | None, 

432 expected_error: str, 

433) -> None: 

434 app = typer.Typer() 1abcdefghij

435 

436 @app.command() 1abcdefghij

437 def show(path: Path = typer.Option(..., **option_kwargs)): 1abcdefghij

438 print(path) # pragma: no cover 

439 

440 if deny_mode is not None: 1abcdefghij

441 original_access = os.access 1abcdefghij

442 

443 def fake_access(path: str, mode: int) -> bool: 1abcdefghij

444 if mode == deny_mode: 1abcdefghij

445 return False 1abcdefghij

446 return original_access(path, mode) # pragma: no cover 

447 

448 monkeypatch.setattr(_param_types.os, "access", fake_access) 1abcdefghij

449 

450 path = tmp_path / "some_path" 1abcdefghij

451 if create_file: 1abcdefghij

452 path.write_text("hello") 1abcdefghij

453 else: 

454 path.mkdir() 1abcdefghij

455 result = runner.invoke(app, ["--path", str(path)]) 1abcdefghij

456 

457 assert result.exit_code != 0 1abcdefghij

458 assert expected_error in result.output 1abcdefghij

459 

460 

461@pytest.mark.parametrize( 1abcdefghij

462 ("default", "expected_annotation"), 

463 [ 

464 (42, int), 

465 (0.5, float), 

466 ("morty", str), 

467 (False, bool), 

468 ("False", str), 

469 ((1, "x"), tuple[int, str]), 

470 ([], str), 

471 ([1], int), 

472 ([[1, "x"]], tuple[int, str]), 

473 ], 

474) 

475def test_default_infers_param_type( 1abcdefghij

476 default: Any, 

477 expected_annotation: Any, 

478) -> None: 

479 app = typer.Typer() 1abcdefghij

480 

481 @app.command() 1abcdefghij

482 def cmd(val=default): 1abcdefghij

483 pass # pragma: no cover 

484 

485 param = next(p for p in typer.main.get_command(app).params if p.name == "val") 1abcdefghij

486 assert isinstance(param, _TyperParameter) 1abcdefghij

487 assert param.get_runtime_param() is not None 1abcdefghij

488 if get_origin(expected_annotation) is tuple: 1abcdefghij

489 assert get_origin(param.get_runtime_param().annotation) is tuple 1abcdefghij

490 assert get_args(param.get_runtime_param().annotation) == get_args( 1abcdefghij

491 expected_annotation 

492 ) 

493 else: 

494 assert param.get_runtime_param().annotation is expected_annotation 1abcdefghij

495 

496 

497@pytest.mark.parametrize( 1abcdefghij

498 ("default", "expected_annotation"), 

499 [ 

500 (42, int), 

501 (0.5, float), 

502 ("morty", str), 

503 (False, bool), 

504 ("False", str), 

505 ], 

506) 

507def test_default_infers_param_type_invoke( 1abcdefghij

508 default: Any, 

509 expected_annotation: Any, 

510) -> None: 

511 app = typer.Typer() 1abcdefghij

512 seen: dict[str, Any] = {} 1abcdefghij

513 

514 @app.command() 1abcdefghij

515 def cmd(val=default): 1abcdefghij

516 seen["val"] = val 1abcdefghij

517 

518 result = runner.invoke(app) 1abcdefghij

519 assert result.exit_code == 0, result.output 1abcdefghij

520 assert seen["val"] == default 1abcdefghij

521 assert type(seen["val"]) is expected_annotation 1abcdefghij

522 

523 

524def test_int_rejects_float_default() -> None: 1abcdefghij

525 app = typer.Typer() 1abcdefghij

526 

527 @app.command() 1abcdefghij

528 def main(age: int = typer.Option(15.3)): 1abcdefghij

529 typer.echo(age) 1abcdefghij

530 

531 result = runner.invoke(app, ["--age", "42"]) 1abcdefghij

532 assert "42" in result.stdout 1abcdefghij

533 

534 # Pydantic validation rejects floats as int instead of converting int(15.3) to 15 

535 result = runner.invoke(app) 1abcdefghij

536 assert result.exit_code != 0 1abcdefghij

537 assert "Input should be a valid integer" in result.stderr 1abcdefghij

538 

539 

540@pytest.mark.parametrize( 1abcdefghij

541 ("platform_case", "stdin_encoding", "filesystem_encoding"), 

542 [ 

543 pytest.param("windows", None, "utf-8", marks=needs_windows), 

544 pytest.param("linux", "latin-1", "utf-8", marks=needs_linux), 

545 pytest.param("linux", None, "latin-1", marks=needs_linux), 

546 ], 

547) 

548def test_argv_encoding( 1abcdefghij

549 monkeypatch: pytest.MonkeyPatch, 

550 platform_case: str, 

551 stdin_encoding: str | None, 

552 filesystem_encoding: str, 

553) -> None: 

554 app = typer.Typer() 1abcdefg

555 

556 @app.command() 1abcdefg

557 def show(name: str = typer.Option(...)): 1abcdefg

558 print(name) 1abcdefg

559 

560 if platform_case == "windows": 1abcdefg

561 import locale 1efg

562 

563 monkeypatch.setattr(locale, "getpreferredencoding", lambda: "latin-1") 1efg

564 else: 

565 argv_encoding = stdin_encoding or filesystem_encoding 1abcd

566 monkeypatch.setattr(_click._compat, "_get_argv_encoding", lambda: argv_encoding) 1abcd

567 monkeypatch.setattr(sys, "getfilesystemencoding", lambda: filesystem_encoding) 1abcd

568 

569 result = runner.invoke(app, [], default_map={"name": b"\xff"}) 1abcdefg

570 assert result.exit_code == 0 1abcdefg

571 assert "ÿ" in result.output 1abcdefg

572 

573 

574@pytest.mark.parametrize( 1abcdefghij

575 ("stdin_encoding", "filesystem_encoding", "expected_encoding"), 

576 [ 

577 pytest.param("latin-1", "utf-8", "latin-1", marks=needs_linux), 

578 pytest.param(None, "latin-1", "latin-1", marks=needs_linux), 

579 ], 

580) 

581def test_argv_encoding_fallback( 1abcdefghij

582 monkeypatch: pytest.MonkeyPatch, 

583 stdin_encoding: str | None, 

584 filesystem_encoding: str, 

585 expected_encoding: str, 

586) -> None: 

587 monkeypatch.setattr( 1abcd

588 _click._compat.sys, 

589 "stdin", 

590 SimpleNamespace(encoding=stdin_encoding), 

591 ) 

592 monkeypatch.setattr( 1abcd

593 _click._compat.sys, 

594 "getfilesystemencoding", 

595 lambda: filesystem_encoding, 

596 ) 

597 assert _click._compat._get_argv_encoding() == expected_encoding 1abcd

598 

599 

600@pytest.mark.parametrize( 1abcdefghij

601 ("parameter", "expected_metavar"), 

602 [ 

603 pytest.param(Annotated[str, typer.Option(...)], "<str>"), 

604 pytest.param(Annotated[str, typer.Argument(...)], "<str>"), 

605 pytest.param(Annotated[int, typer.Option(...)], "<int>"), 

606 pytest.param(Annotated[int, typer.Argument(...)], "<int>"), 

607 pytest.param(Annotated[float, typer.Option(...)], "<float>"), 

608 pytest.param( 

609 Annotated[float, typer.Option(..., min=0.666, max=3.42)], "<float range>" 

610 ), 

611 pytest.param(Annotated[bytes, typer.Option(...)], "<bytes>"), 

612 pytest.param(Annotated[list[str], typer.Option(...)], "<list[str]>"), 

613 pytest.param(Annotated[tuple[str, int], typer.Option(...)], "<str,int>..."), 

614 pytest.param(Annotated[tuple[Path, str], typer.Option(...)], "<Path,str>..."), 

615 pytest.param(Annotated[str, typer.Option(..., resolve_path=True)], "<str>"), 

616 pytest.param(Annotated[Any, typer.Option(..., resolve_path=True)], "<path>"), 

617 pytest.param(Annotated[Path, typer.Option(...)], "<path>"), 

618 pytest.param(Annotated[Path, typer.Option(..., dir_okay=False)], "<file>"), 

619 pytest.param(Annotated[Path, typer.Option(..., file_okay=False)], "<dir>"), 

620 pytest.param(Annotated[SomeEnum, typer.Option(...)], "<one|two|three>"), 

621 pytest.param(Annotated[SomeEnum, typer.Argument()], "<one|two|three>"), 

622 pytest.param( 

623 Annotated[SomeEnum, typer.Option(..., show_choices=False)], "<SomeEnum>" 

624 ), 

625 pytest.param( 

626 Annotated[list[SomeEnum], typer.Option(...)], "<list[one|two|three]>" 

627 ), 

628 pytest.param( 

629 Annotated[list[SomeEnum], typer.Option(..., show_choices=False)], 

630 "<list[SomeEnum]>", 

631 ), 

632 pytest.param(Annotated[Literal["x", "y"], typer.Option(...)], "<x|y>"), 

633 pytest.param(Annotated[typer.FileText, typer.Option(...)], "<FileText>"), 

634 pytest.param(Annotated[datetime, typer.Option(...)], "<datetime>"), 

635 pytest.param( 

636 Annotated[datetime, typer.Option(..., formats=["%Y-%m-%d", "%d/%m/%Y"])], 

637 "<%Y-%m-%d|%d/%m/%Y>", 

638 ), 

639 ], 

640) 

641def test_param_type_help_metavar(parameter: Any, expected_metavar: str) -> None: 1abcdefghij

642 app = typer.Typer() 1abcdefghij

643 

644 @app.command() 1abcdefghij

645 def with_default(value: parameter = "my_default"): 1abcdefghij

646 pass # pragma: no cover 

647 

648 @app.command() 1abcdefghij

649 def without_default(value: parameter): 1abcdefghij

650 pass # pragma: no cover 

651 

652 result = runner.invoke(app, ["with-default", "--help"]) 1abcdefghij

653 assert result.exit_code == 0 1abcdefghij

654 assert expected_metavar in result.output 1abcdefghij

655 

656 result = runner.invoke(app, ["without-default", "--help"]) 1abcdefghij

657 assert result.exit_code == 0 1abcdefghij

658 assert expected_metavar in result.output 1abcdefghij

659 

660 

661def test_metavar_preformatted_square_brackets() -> None: 1abcdefghij

662 app = typer.Typer() 1abcdefghij

663 

664 @app.command() 1abcdefghij

665 def main(value: Annotated[str, typer.Argument(metavar="[VALUE]")]): 1abcdefghij

666 pass # pragma: no cover 

667 

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

669 assert result.exit_code == 0 1abcdefghij

670 assert "[VALUE]" in result.output 1abcdefghij

671 assert "[[VALUE]]" not in result.output 1abcdefghij