Coverage for src / lexigram / contracts / auth / user.py: 100%

29 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-08-19 05:41 +0800

1"""User protocols. 

2 

3Protocols defining user identity contracts. 

4""" 

5 

6from __future__ import annotations 

7 

8from typing import Protocol, runtime_checkable 

9 

10 

11@runtime_checkable 

12class UserProtocol(Protocol): 

13 """Protocol for user entity. 

14 

15 Defines the minimal contract for a user in the system. 

16 """ 

17 

18 @property 

19 def user_id(self) -> str: 

20 """Unique user identifier.""" 

21 ... 

22 

23 @property 

24 def email(self) -> str: 

25 """User email address.""" 

26 ... 

27 

28 @property 

29 def is_active(self) -> bool: 

30 """Whether user account is active.""" 

31 ... 

32 

33 

34@runtime_checkable 

35class AuthenticatedUserProtocol(Protocol): 

36 """Protocol for authenticated user attached to requests. 

37 

38 This protocol defines the contract that any authenticated user 

39 implementation must follow for type-safe user handling. 

40 

41 Example: 

42 ```python 

43 def get_user_profile(user: AuthenticatedUserProtocol) -> dict: 

44 return {"user_id": user.user_id, "roles": user.roles} 

45 ``` 

46 """ 

47 

48 @property 

49 def user_id(self) -> str: 

50 """Unique identifier for the user.""" 

51 ... 

52 

53 @property 

54 def name(self) -> str: 

55 """Name of the authenticated user.""" 

56 ... 

57 

58 @property 

59 def email(self) -> str: 

60 """Email address of the user.""" 

61 ... 

62 

63 @property 

64 def is_active(self) -> bool: 

65 """Whether the user account is active.""" 

66 ... 

67 

68 @property 

69 def is_verified(self) -> bool: 

70 """Whether the user's email is verified.""" 

71 ... 

72 

73 @property 

74 def roles(self) -> list[str]: 

75 """List of roles assigned to the user.""" 

76 ... 

77 

78 @property 

79 def permissions(self) -> list[str]: 

80 """List of permissions granted to the user.""" 

81 ... 

82 

83 def has_role(self, role: str) -> bool: 

84 """Check if user has a specific role. 

85 

86 Args: 

87 role: The role to check for. 

88 

89 Returns: 

90 True if the user has the role. 

91 """ 

92 ... 

93 

94 def has_permission(self, permission: str) -> bool: 

95 """Check if user has a specific permission. 

96 

97 Args: 

98 permission: The permission to check for. 

99 

100 Returns: 

101 True if the user has the permission. 

102 """ 

103 ... 

104 

105 

106__all__ = [ 

107 "AuthenticatedUserProtocol", 

108 "UserProtocol", 

109]