Skip to content

Commit f12734d

Browse files
committed
Switch ordering of classes to make types Python 3.8 compatible
1 parent 90be040 commit f12734d

3 files changed

Lines changed: 81 additions & 79 deletions

File tree

flask_phpbb3/backends/base.py

Lines changed: 72 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,12 @@
11
import abc
2-
from typing import Any, Dict, List, Optional
2+
from typing import Any, Dict, List, Optional, Union
33

44
import cachelib
55

66

77
ACL_OPTIONS_CACHE_TTL: int = 3600 * 1
88

99

10-
class BaseBackend:
11-
KNOWN_OPERATIONS = (
12-
'fetch',
13-
'get',
14-
'has',
15-
'set',
16-
)
17-
KNOWN_DRIVERS = (
18-
'psycopg2',
19-
)
20-
21-
def __init__(
22-
self,
23-
cache: cachelib.BaseCache,
24-
config: Dict[str, Any]
25-
):
26-
self._functions: Dict[str, str] = {}
27-
self._connection = None
28-
self._cache = cache
29-
self._config = config
30-
31-
self._prepare_statements()
32-
custom_statements = self._config.get('CUSTOM_STATEMENTS', {})
33-
if not isinstance(custom_statements, dict):
34-
custom_statements = {}
35-
self._functions.update(custom_statements)
36-
37-
@abc.abstractmethod
38-
def _setup_connection(self) -> None:
39-
raise NotImplementedError
40-
41-
@abc.abstractmethod
42-
def _prepare_statements(self) -> None:
43-
raise NotImplementedError
44-
45-
@property
46-
@abc.abstractmethod
47-
def _db(self) -> Any:
48-
raise NotImplementedError
49-
50-
@abc.abstractmethod
51-
def execute(
52-
self,
53-
command: str,
54-
cache: bool = False,
55-
cache_ttl: Optional[int] = None,
56-
skip: int = 0,
57-
limit: Optional[int] = 10,
58-
**kwargs: int | str
59-
) -> Any:
60-
raise NotImplementedError
61-
62-
@abc.abstractmethod
63-
def close(self) -> None:
64-
raise NotImplementedError
65-
66-
@property
67-
@abc.abstractmethod
68-
def is_closed(self) -> bool:
69-
raise NotImplementedError
70-
71-
def get_user_acl(self, raw_user_permissions: str) -> UserAcl:
72-
raw_acl_options = self.execute(
73-
'fetch_acl_options',
74-
cache=True,
75-
cache_ttl=ACL_OPTIONS_CACHE_TTL,
76-
limit=None,
77-
)
78-
return UserAcl(raw_acl_options, raw_user_permissions)
79-
80-
8110
class UserAcl:
8211
def __init__(self, raw_acl_options: List[Dict], raw_user_permissions: str):
8312
self._acl_options = self._parse_acl_options(raw_acl_options)
@@ -188,3 +117,74 @@ def has_privileges(self, *privileges: str, **kwargs: int) -> bool:
188117
for option in privileges:
189118
output |= self.has_privilege(option, forum_id=forum_id)
190119
return output
120+
121+
122+
class BaseBackend:
123+
KNOWN_OPERATIONS = (
124+
'fetch',
125+
'get',
126+
'has',
127+
'set',
128+
)
129+
KNOWN_DRIVERS = (
130+
'psycopg2',
131+
)
132+
133+
def __init__(
134+
self,
135+
cache: cachelib.BaseCache,
136+
config: Dict[str, Any]
137+
):
138+
self._functions: Dict[str, str] = {}
139+
self._connection = None
140+
self._cache = cache
141+
self._config = config
142+
143+
self._prepare_statements()
144+
custom_statements = self._config.get('CUSTOM_STATEMENTS', {})
145+
if not isinstance(custom_statements, dict):
146+
custom_statements = {}
147+
self._functions.update(custom_statements)
148+
149+
@abc.abstractmethod
150+
def _setup_connection(self) -> None:
151+
raise NotImplementedError
152+
153+
@abc.abstractmethod
154+
def _prepare_statements(self) -> None:
155+
raise NotImplementedError
156+
157+
@property
158+
@abc.abstractmethod
159+
def _db(self) -> Any:
160+
raise NotImplementedError
161+
162+
@abc.abstractmethod
163+
def execute(
164+
self,
165+
command: str,
166+
cache: bool = False,
167+
cache_ttl: Optional[int] = None,
168+
skip: int = 0,
169+
limit: Optional[int] = 10,
170+
**kwargs: Union[int, str]
171+
) -> Any:
172+
raise NotImplementedError
173+
174+
@abc.abstractmethod
175+
def close(self) -> None:
176+
raise NotImplementedError
177+
178+
@property
179+
@abc.abstractmethod
180+
def is_closed(self) -> bool:
181+
raise NotImplementedError
182+
183+
def get_user_acl(self, raw_user_permissions: str) -> UserAcl:
184+
raw_acl_options = self.execute(
185+
'fetch_acl_options',
186+
cache=True,
187+
cache_ttl=ACL_OPTIONS_CACHE_TTL,
188+
limit=None,
189+
)
190+
return UserAcl(raw_acl_options, raw_user_permissions)

flask_phpbb3/backends/psycopg2.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import json
2-
from typing import Any, Dict, List, Optional
2+
from typing import Any, Dict, List, Optional, Union
33

44
try:
55
import psycopg2
@@ -128,7 +128,7 @@ def _sql_query(
128128
cache_ttl: Optional[int] = None,
129129
skip: int = 0,
130130
limit: Optional[int] = 10,
131-
**kwargs: int | str
131+
**kwargs: Union[int, str]
132132
) -> Any:
133133
"""Executes a query with values in kwargs."""
134134
if operation not in self.KNOWN_OPERATIONS:
@@ -180,7 +180,7 @@ def _execute_operation(
180180
self,
181181
operation: str,
182182
query: str,
183-
params: Dict[str, str | int]
183+
params: Dict[str, Union[str, int]]
184184
) -> Any:
185185
cursor = self._db.cursor()
186186

@@ -214,7 +214,7 @@ def execute(
214214
cache_ttl: Optional[int] = None,
215215
skip: int = 0,
216216
limit: Optional[int] = 10,
217-
**kwargs: int | str
217+
**kwargs: Union[int, str]
218218
) -> Any:
219219
cache_key_prefix: Optional[str] = None
220220
if cache:

flask_phpbb3/sessions.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
from __future__ import annotations
2+
13
import json
2-
from typing import Any, Dict, Optional, Set
4+
from typing import Any, Dict, Optional, Set, Union
35

46
import cachelib
57

@@ -30,7 +32,7 @@ def __init__(self) -> None:
3032
# request should not be executed multiple times
3133
self._request_cache: Dict[str, Any] = {}
3234

33-
def __setitem__(self, key: str, value: str | int) -> None:
35+
def __setitem__(self, key: str, value: Union[str, int]) -> None:
3436
modified: bool = self.get(key) != value
3537
super(PhpBB3Session, self).__setitem__(key, value)
3638
if key not in self._read_only_properties:
@@ -60,7 +62,7 @@ def is_authenticated(self) -> bool:
6062
user_id = int(self.get('user_id', 1))
6163
return user_id > 1
6264

63-
def is_member(self, group: int | str) -> bool:
65+
def is_member(self, group: Union[int, str]) -> bool:
6466
"""Tests if user is a member of specified group."""
6567
if isinstance(group, int):
6668
# Try with default group

0 commit comments

Comments
 (0)