Skip to content

Commit f2bfa4f

Browse files
fix: typing issues
1 parent 62e63ef commit f2bfa4f

File tree

1 file changed

+52
-51
lines changed

1 file changed

+52
-51
lines changed

src/flask_principal.py

Lines changed: 52 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -123,48 +123,6 @@ class PermissionDenied(RuntimeError):
123123
"""Permission denied to the resource"""
124124

125125

126-
class Identity(object):
127-
"""Represent the user's identity.
128-
129-
:param id: The user id
130-
:param auth_type: The authentication type used to confirm the user's
131-
identity.
132-
133-
The identity is used to represent the user's identity in the system. This
134-
object is created on login, or on the start of the request as loaded from
135-
the user's session.
136-
137-
Once loaded it is sent using the `identity-loaded` signal, and should be
138-
populated with additional required information.
139-
140-
Needs that are provided by this identity should be added to the `provides`
141-
set after loading.
142-
"""
143-
def __init__(self, id: Optional[Any], auth_type: Optional[str] = None) -> None:
144-
self.id = id
145-
self.auth_type = auth_type
146-
self.provides: Set[Union[Need, ItemNeed]] = set()
147-
148-
def can(self, permission: BasePermission) -> bool:
149-
"""Whether the identity has access to the permission.
150-
151-
:param permission: The permission to test provision for.
152-
"""
153-
return permission.allows(self)
154-
155-
def __repr__(self) -> str:
156-
return '<{0} id="{1}" auth_type="{2}" provides={3}>'.format(
157-
self.__class__.__name__, self.id, self.auth_type, self.provides
158-
)
159-
160-
161-
class AnonymousIdentity(Identity):
162-
"""An anonymous identity"""
163-
164-
def __init__(self) -> None:
165-
Identity.__init__(self, None)
166-
167-
168126
class IdentityContext(object):
169127
"""The context of an identity for a permission.
170128
@@ -176,14 +134,14 @@ class IdentityContext(object):
176134
flow is continued (context manager) or the function is executed (decorator).
177135
"""
178136

179-
def __init__(self, permission: BasePermission, http_exception: Optional[int] = None) -> None:
137+
def __init__(self, permission: 'BasePermission', http_exception: Optional[int] = None) -> None:
180138
self.permission = permission
181139
self.http_exception = http_exception
182140
"""The permission of this principal
183141
"""
184142

185143
@property
186-
def identity(self) -> Identity:
144+
def identity(self) -> 'Identity':
187145
"""The identity of this principal
188146
"""
189147
return cast(Identity, g.identity)
@@ -230,28 +188,28 @@ def __bool__(self) -> bool:
230188
"""
231189
return self._bool()
232190

233-
def __or__(self, other: Union['Permission', BasePermission]) -> Union['Permission', BasePermission]:
191+
def __or__(self, other: Union['Permission', 'BasePermission']) -> Union['Permission', 'BasePermission']:
234192
"""See ``OrPermission``.
235193
"""
236194
return self.or_(other)
237195

238-
def or_(self, other: Union['Permission', BasePermission]) -> Union['Permission', BasePermission]:
196+
def or_(self, other: Union['Permission', 'BasePermission']) -> Union['Permission', 'BasePermission']:
239197
return OrPermission(self, other)
240198

241-
def __and__(self, other: Union['Permission', BasePermission]) -> Union['Permission', BasePermission]:
199+
def __and__(self, other: Union['Permission', 'BasePermission']) -> Union['Permission', 'BasePermission']:
242200
"""See ``AndPermission``.
243201
"""
244202
return self.and_(other)
245203

246-
def and_(self, other: Union['Permission', BasePermission]) -> Union['Permission', BasePermission]:
204+
def and_(self, other: Union['Permission', 'BasePermission']) -> Union['Permission', 'BasePermission']:
247205
return AndPermission(self, other)
248206

249-
def __invert__(self) -> Union['NotPermission', BasePermission]:
207+
def __invert__(self) -> Union['NotPermission', 'BasePermission']:
250208
"""See ``NotPermission``.
251209
"""
252210
return self.invert()
253211

254-
def invert(self) -> Union['NotPermission', BasePermission]:
212+
def invert(self) -> Union['NotPermission', 'BasePermission']:
255213
return NotPermission(self)
256214

257215
def require(self, http_exception: Optional[int] = None) -> IdentityContext:
@@ -287,7 +245,7 @@ def test(self, http_exception: Optional[int] = None) -> None:
287245
with self.require(http_exception):
288246
pass
289247

290-
def allows(self, identity: Identity) -> bool:
248+
def allows(self, identity: 'Identity') -> bool:
291249
"""Whether the identity can access this permission.
292250
293251
:param identity: The identity
@@ -303,6 +261,49 @@ def can(self) -> bool:
303261
"""
304262
return self.require().can()
305263

264+
class Identity:
265+
"""Represent the user's identity.
266+
267+
:param id: The user id
268+
:param auth_type: The authentication type used to confirm the user's
269+
identity.
270+
271+
The identity is used to represent the user's identity in the system. This
272+
object is created on login, or on the start of the request as loaded from
273+
the user's session.
274+
275+
Once loaded it is sent using the `identity-loaded` signal, and should be
276+
populated with additional required information.
277+
278+
Needs that are provided by this identity should be added to the `provides`
279+
set after loading.
280+
"""
281+
def __init__(self, id: Optional[Any], auth_type: Optional[str] = None) -> None:
282+
self.id = id
283+
self.auth_type = auth_type
284+
self.provides: Set[Union[Need, ItemNeed]] = set()
285+
286+
def can(self, permission: BasePermission) -> bool:
287+
"""Whether the identity has access to the permission.
288+
289+
:param permission: The permission to test provision for.
290+
"""
291+
return permission.allows(self)
292+
293+
def __repr__(self) -> str:
294+
return '<{0} id="{1}" auth_type="{2}" provides={3}>'.format(
295+
self.__class__.__name__, self.id, self.auth_type, self.provides
296+
)
297+
298+
299+
class AnonymousIdentity(Identity):
300+
"""An anonymous identity"""
301+
302+
def __init__(self) -> None:
303+
Identity.__init__(self, None)
304+
305+
306+
306307

307308
class _NaryOperatorPermission(BasePermission):
308309

0 commit comments

Comments
 (0)