Coverage for tests / atomic_write_example.py: 100%

29 statements  

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

1import time 1abcdefghij

2 

3import typer 1abcdefghij

4 

5app = typer.Typer() 1abcdefghij

6 

7 

8@app.command() 1abcdefghij

9def write_atomic( 1abcdefghij

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") 1abcdefghij

14 config.flush() 1abcdefghij

15 typer.echo("halfway") 1abcdefghij

16 time.sleep(pause) 1abcdefghij

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

18 config.flush() 1abcdefghij

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

20 

21 

22@app.command() 1abcdefghij

23def write_atomic_binary( 1abcdefghij

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

25) -> None: 

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

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

28 

29 

30@app.command() 1abcdefghij

31def api_atomic( 1abcdefghij

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

33) -> None: 

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

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

36 with config as entered: 1abcdefghij

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

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

39 

40 

41@app.command() 1abcdefghij

42def invalid_atomic_append( 1abcdefghij

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() 1abcdefghij

49def invalid_atomic_exclusive( 1abcdefghij

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() 1abcdefghij

56def invalid_atomic_read( 1abcdefghij

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__": 1abcdefghij

63 app() 1abcdefghij