Coverage for tests/test_param_meta_empty.py: 100%

26 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-11-13 11:07 +0000

1import typer 1abcdefgh

2from typer.testing import CliRunner 1abcdefgh

3 

4runner = CliRunner() 1abcdefgh

5 

6 

7def test_default_with_class_with_custom_eq(): 1abcdefgh

8 app = typer.Typer() 1abcdefgh

9 

10 from typer.models import ParamMeta 1abcdefgh

11 

12 class StupidClass: 1abcdefgh

13 def __init__(self, a): 1abcdefgh

14 self.a = a 1abcdefgh

15 

16 def __eq__(self, other) -> bool: 1abcdefgh

17 if other is ParamMeta.empty: 1abcdefgh

18 return True 1abcdefgh

19 try: 1abcdefgh

20 return self.a == other.a 1abcdefgh

21 except Exception: 1abcdefgh

22 return False 1abcdefgh

23 

24 def __ne__(self, other: object) -> bool: 1abcdefgh

25 return not self.__eq__(other) 1abcdefgh

26 

27 @app.command() 1abcdefgh

28 def cmd(val=StupidClass(42)): 1abcdefgh

29 print(val) 1abcdefgh

30 

31 assert StupidClass(666) == ParamMeta.empty 1abcdefgh

32 assert StupidClass(666) != StupidClass(1) 1abcdefgh

33 

34 result = runner.invoke(app) 1abcdefgh

35 assert result.exit_code == 0, result.output 1abcdefgh

36 assert "StupidClass" in result.output 1abcdefgh