Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1from functools import partial 

2from typing import List 

3from warnings import warn 

4 

5 

6class PhytestWarning(Warning): 

7 pass 

8 

9 

10class PhytestAssertion(AssertionError): 

11 pass 

12 

13 

14def assert_or_warn(statement, warning, *messages): 

15 if statement: 

16 return 

17 

18 message = "\n".join(messages) 

19 if warning: 

20 warn(message, PhytestWarning) 

21 else: 

22 raise PhytestAssertion(message) 

23 

24 

25def default_date_patterns(): 

26 return [ 

27 r"\d{4}\.?\d*$", 

28 r"\d{4}-\d{2}-\d{2}", 

29 ] 

30 

31 

32class PhytestObject: 

33 def __init__(self, *args, **kwargs): 

34 super().__init__(*args, **kwargs) 

35 

36 # Add partial methods with the warning flag set to True 

37 for method_name in self.assertion_method_names(): 

38 method = getattr(self, method_name) 

39 truncated_name = method_name[len("assert") :] 

40 warning_name = f"warn{truncated_name}" 

41 setattr(self, warning_name, partial(method, warning=True)) 

42 

43 def assertion_method_names(self) -> List[str]: 

44 """ 

45 Returns a list with the names of the methods used to make assertion statements. 

46 """ 

47 return [ 

48 attribute 

49 for attribute in dir(self) 

50 if attribute.startswith("assert_") and callable(getattr(self, attribute)) 

51 ]