11from __future__ import absolute_import
22
33import json
4- import typing
4+ from typing import Any , Dict , List , Optional
55
66try :
77 import psycopg2
1515
1616
1717class Psycopg2Backend (base .BaseBackend ):
18- def _setup_connection (self ):
19- # type: () -> None
18+ def _setup_connection (self ) -> None :
2019 self ._connection = psycopg2 .connect (
2120 'dbname={DATABASE}'
2221 ' host={HOST}'
@@ -26,15 +25,13 @@ def _setup_connection(self):
2625 )
2726
2827 @property
29- def _db (self ):
30- # type: () -> psycopg2.extensions.connection
28+ def _db (self ) -> psycopg2 .extensions .connection :
3129 if not self ._connection :
3230 self ._setup_connection ()
3331
3432 return self ._connection
3533
36- def _prepare_statements (self ):
37- # type: () -> None
34+ def _prepare_statements (self ) -> None :
3835 """
3936 Initializes prepared SQL statements, depending on version of PHPBB3
4037 """
@@ -106,13 +103,12 @@ def _prepare_statements(self):
106103
107104 # TODO Add/Move to version specific queries
108105
109- def _prepare_custom_fields_statements (self ):
110- # type: () -> None
106+ def _prepare_custom_fields_statements (self ) -> None :
111107 """
112108 Prepares statements for custom fields
113109 """
114110 # Setters for custom fields
115- custom_fields = self ._config .get ('CUSTOM_USER_FIELDS' , [])
111+ custom_fields : List [ Any ] = self ._config .get ('CUSTOM_USER_FIELDS' , [])
116112 for custom_field in custom_fields :
117113 self ._functions ["set_{0}" .format (custom_field )] = (
118114 "UPDATE"
@@ -128,15 +124,14 @@ def _prepare_custom_fields_statements(self):
128124
129125 def _sql_query (
130126 self ,
131- operation , # type: str
132- query , # type: str
133- cache_key_prefix = None , # type: typing.Optional[str]
134- cache_ttl = None , # type: typing.Optional[int]
135- skip = 0 , # type: int
136- limit = 10 , # type: typing.Optional[int]
137- ** kwargs # type: typing.Union[int, str]
138- ):
139- # type: (...) -> typing.Any
127+ operation : str ,
128+ query : str ,
129+ cache_key_prefix : Optional [str ] = None ,
130+ cache_ttl : Optional [int ] = None ,
131+ skip : int = 0 ,
132+ limit : Optional [int ] = 10 ,
133+ ** kwargs : int | str
134+ ) -> Any :
140135 """Executes a query with values in kwargs."""
141136 if operation not in self .KNOWN_OPERATIONS :
142137 raise ValueError ("Unknown operation" )
@@ -149,7 +144,7 @@ def _sql_query(
149144 for key , value in kwargs .items ())
150145 )
151146 raw_data = self ._cache .get (cache_key )
152- if raw_data and isinstance (raw_data , (str , unicode )):
147+ if raw_data and isinstance (raw_data , (str , bytes )):
153148 try :
154149 return json .loads (raw_data )
155150 except ValueError :
@@ -172,20 +167,23 @@ def _sql_query(
172167
173168 return output
174169
175- def _paginate_query (self , query , skip , limit ):
176- # type: (str, int, typing.Optional[int]) -> str
170+ def _paginate_query (
171+ self ,
172+ query : str ,
173+ skip : int ,
174+ limit : Optional [int ]
175+ ) -> str :
177176 output = query + ' OFFSET {:d}' .format (skip )
178177 if limit :
179178 output += ' LIMIT {:d}' .format (limit )
180179 return output
181180
182181 def _execute_operation (
183182 self ,
184- operation , # type: str
185- query , # type: str
186- params # type: typing.Dict[str, typing.Union[str, int]]
187- ):
188- # type: (...) -> typing.Any
183+ operation : str ,
184+ query : str ,
185+ params : Dict [str , str | int ]
186+ ) -> Any :
189187 cursor = self ._db .cursor ()
190188
191189 cursor .execute (
@@ -213,15 +211,14 @@ def _execute_operation(
213211
214212 def execute (
215213 self ,
216- command , # type: str
217- cache = False , # type: bool
218- cache_ttl = None , # type: typing.Optional[int]
219- skip = 0 , # type: int
220- limit = 10 , # type: typing.Optional[int]
221- ** kwargs # type: typing.Union[int, str]
222- ):
223- # type: (...) -> typing.Any
224- cache_key_prefix = None
214+ command : str ,
215+ cache : bool = False ,
216+ cache_ttl : Optional [int ] = None ,
217+ skip : int = 0 ,
218+ limit : Optional [int ] = 10 ,
219+ ** kwargs : int | str
220+ ) -> Any :
221+ cache_key_prefix : Optional [str ] = None
225222 if cache :
226223 cache_key_prefix = command
227224
@@ -246,11 +243,9 @@ def execute(
246243 ** kwargs
247244 )
248245
249- def close (self ):
250- # type: () -> None
246+ def close (self ) -> None :
251247 self ._db .close ()
252248
253249 @property
254- def is_closed (self ):
255- # type: () -> bool
250+ def is_closed (self ) -> bool :
256251 return bool (self ._db .closed )
0 commit comments