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
« prev ^ index » next coverage.py v7.13.5, created at 2026-08-19 05:41 +0800
1"""User protocols.
3Protocols defining user identity contracts.
4"""
6from __future__ import annotations
8from typing import Protocol, runtime_checkable
11@runtime_checkable
12class UserProtocol(Protocol):
13 """Protocol for user entity.
15 Defines the minimal contract for a user in the system.
16 """
18 @property
19 def user_id(self) -> str:
20 """Unique user identifier."""
21 ...
23 @property
24 def email(self) -> str:
25 """User email address."""
26 ...
28 @property
29 def is_active(self) -> bool:
30 """Whether user account is active."""
31 ...
34@runtime_checkable
35class AuthenticatedUserProtocol(Protocol):
36 """Protocol for authenticated user attached to requests.
38 This protocol defines the contract that any authenticated user
39 implementation must follow for type-safe user handling.
41 Example:
42 ```python
43 def get_user_profile(user: AuthenticatedUserProtocol) -> dict:
44 return {"user_id": user.user_id, "roles": user.roles}
45 ```
46 """
48 @property
49 def user_id(self) -> str:
50 """Unique identifier for the user."""
51 ...
53 @property
54 def name(self) -> str:
55 """Name of the authenticated user."""
56 ...
58 @property
59 def email(self) -> str:
60 """Email address of the user."""
61 ...
63 @property
64 def is_active(self) -> bool:
65 """Whether the user account is active."""
66 ...
68 @property
69 def is_verified(self) -> bool:
70 """Whether the user's email is verified."""
71 ...
73 @property
74 def roles(self) -> list[str]:
75 """List of roles assigned to the user."""
76 ...
78 @property
79 def permissions(self) -> list[str]:
80 """List of permissions granted to the user."""
81 ...
83 def has_role(self, role: str) -> bool:
84 """Check if user has a specific role.
86 Args:
87 role: The role to check for.
89 Returns:
90 True if the user has the role.
91 """
92 ...
94 def has_permission(self, permission: str) -> bool:
95 """Check if user has a specific permission.
97 Args:
98 permission: The permission to check for.
100 Returns:
101 True if the user has the permission.
102 """
103 ...
106__all__ = [
107 "AuthenticatedUserProtocol",
108 "UserProtocol",
109]