Coverage for C: \ Users \ peaco \ OneDrive \ Documents \ GitHub \ mt_metadata \ mt_metadata \ common \ rating.py: 95%

22 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-01-10 00:11 -0800

1# ===================================================== 

2# Imports 

3# ===================================================== 

4from typing import Annotated 

5 

6from pydantic import Field, field_validator, ValidationInfo 

7 

8from mt_metadata import NULL_VALUES 

9from mt_metadata.base import MetadataBase 

10 

11 

12# ===================================================== 

13class Rating(MetadataBase): 

14 author: Annotated[ 

15 str | None, 

16 Field( 

17 default=None, 

18 description="Author of who rated the data.", 

19 alias=None, 

20 json_schema_extra={ 

21 "units": None, 

22 "required": False, 

23 "examples": "gradstudent ace", 

24 }, 

25 ), 

26 ] 

27 

28 method: Annotated[ 

29 str | None, 

30 Field( 

31 default=None, 

32 description="The method used to rate the data.", 

33 alias=None, 

34 json_schema_extra={ 

35 "units": None, 

36 "required": False, 

37 "examples": "standard deviation", 

38 }, 

39 ), 

40 ] 

41 

42 value: Annotated[ 

43 int | None | str, 

44 Field( 

45 default=None, 

46 description="A rating from 1-5 where 1 is bad and 5 is good and 0 if unrated.", 

47 ge=0, 

48 le=5, 

49 alias=None, 

50 json_schema_extra={ 

51 "units": None, 

52 "required": True, 

53 "examples": "4", 

54 }, 

55 ), 

56 ] 

57 

58 @field_validator("value", mode="before") 

59 @classmethod 

60 def validate_value( 

61 cls, value: int | None | str | float, info: ValidationInfo 

62 ) -> int | None: 

63 if value in NULL_VALUES: 

64 value = None 

65 else: 

66 try: 

67 value = int(value) 

68 if value < 0 or value > 5: 

69 raise ValueError("Invalid rating value must be between 0 and 5.") 

70 

71 except ValueError: 

72 if value in NULL_VALUES: 

73 value = None 

74 else: 

75 raise ValueError(f"Invalid rating value: {value}") 

76 return value