Coverage for tests / atomic_write_example.py: 100%

29 statements  

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

1import time 1abcdefg

2 

3import typer 1abcdefg

4 

5app = typer.Typer() 1abcdefg

6 

7 

8@app.command() 1abcdefg

9def write_atomic( 1abcdefg

10 config: typer.FileText = typer.Option(..., mode="w", atomic=True), 

11 pause: float = typer.Option(0.3), 

12) -> None: 

13 config.write("atomic-content-1\n") 1abcdefg

14 config.flush() 1abcdefg

15 typer.echo("halfway") 1abcdefg

16 time.sleep(pause) 1abcdefg

17 config.write("atomic-content-2\n") 1abcdefg

18 config.flush() 1abcdefg

19 typer.echo("written atomically") 1abcdefg

20 

21 

22@app.command() 1abcdefg

23def write_atomic_binary( 1abcdefg

24 config: typer.FileBinaryWrite = typer.Option(..., atomic=True, lazy=False), 

25) -> None: 

26 config.write(b"\x00\x01binary-atomic\n") 1abcdefg

27 typer.echo("written binary atomically") 1abcdefg

28 

29 

30@app.command() 1abcdefg

31def api_atomic( 1abcdefg

32 config: typer.FileText = typer.Option(..., mode="w", atomic=True, lazy=False), 

33) -> None: 

34 typer.echo(f"name={config.name}") 1abcdefg

35 typer.echo(f"repr={repr(config)}") 1abcdefg

36 with config as entered: 1abcdefg

37 typer.echo(f"entered={entered is config}") 1abcdefg

38 entered.write("atomic-api-done\n") 1abcdefg

39 

40 

41@app.command() 1abcdefg

42def invalid_atomic_append( 1abcdefg

43 config: typer.FileText = typer.Option(..., mode="a", atomic=True, lazy=False), 

44) -> None: 

45 typer.echo(config.name) # pragma: no cover 

46 

47 

48@app.command() 1abcdefg

49def invalid_atomic_exclusive( 1abcdefg

50 config: typer.FileText = typer.Option(..., mode="x", atomic=True, lazy=False), 

51) -> None: 

52 typer.echo(config.name) # pragma: no cover 

53 

54 

55@app.command() 1abcdefg

56def invalid_atomic_read( 1abcdefg

57 config: typer.FileText = typer.Option(..., mode="r", atomic=True, lazy=False), 

58) -> None: 

59 typer.echo(config.name) # pragma: no cover 

60 

61 

62if __name__ == "__main__": 1abcdefg

63 app() 1abcdefg