Skip to content

Commit fa4a4f0

Browse files
author
AGAEV Denis E
committed
Fix errors
1 parent 9775736 commit fa4a4f0

File tree

3 files changed

+75
-61
lines changed

3 files changed

+75
-61
lines changed

python/psqlpy/_internal/__init__.pyi

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class QueryResult:
1616
custom_decoders: dict[str, Callable[[bytes], Any]] | None = None,
1717
) -> list[dict[Any, Any]]:
1818
"""Return result from database as a list of dicts."""
19+
1920
def as_class(
2021
self: Self,
2122
as_class: Callable[..., _CustomClass],
@@ -57,6 +58,7 @@ class SingleQueryResult:
5758

5859
def result(self: Self) -> dict[Any, Any]:
5960
"""Return result from database as a dict."""
61+
6062
def as_class(
6163
self: Self,
6264
as_class: Callable[..., _CustomClass],
@@ -191,11 +193,13 @@ class Cursor:
191193
192194
Execute DECLARE command for the cursor.
193195
"""
196+
194197
async def close(self: Self) -> None:
195198
"""Close the cursor.
196199
197200
Execute CLOSE command for the cursor.
198201
"""
202+
199203
async def fetch(
200204
self: Self,
201205
fetch_number: int | None = None,
@@ -210,6 +214,7 @@ class Cursor:
210214
### Returns:
211215
result as `QueryResult`.
212216
"""
217+
213218
async def fetch_next(
214219
self: Self,
215220
) -> QueryResult:
@@ -220,6 +225,7 @@ class Cursor:
220225
### Returns:
221226
result as `QueryResult`.
222227
"""
228+
223229
async def fetch_prior(
224230
self: Self,
225231
) -> QueryResult:
@@ -230,6 +236,7 @@ class Cursor:
230236
### Returns:
231237
result as `QueryResult`.
232238
"""
239+
233240
async def fetch_first(
234241
self: Self,
235242
) -> QueryResult:
@@ -240,6 +247,7 @@ class Cursor:
240247
### Returns:
241248
result as `QueryResult`.
242249
"""
250+
243251
async def fetch_last(
244252
self: Self,
245253
) -> QueryResult:
@@ -250,6 +258,7 @@ class Cursor:
250258
### Returns:
251259
result as `QueryResult`.
252260
"""
261+
253262
async def fetch_absolute(
254263
self: Self,
255264
absolute_number: int,
@@ -261,6 +270,7 @@ class Cursor:
261270
### Returns:
262271
result as `QueryResult`.
263272
"""
273+
264274
async def fetch_relative(
265275
self: Self,
266276
relative_number: int,
@@ -272,6 +282,7 @@ class Cursor:
272282
### Returns:
273283
result as `QueryResult`.
274284
"""
285+
275286
async def fetch_forward_all(
276287
self: Self,
277288
) -> QueryResult:
@@ -282,6 +293,7 @@ class Cursor:
282293
### Returns:
283294
result as `QueryResult`.
284295
"""
296+
285297
async def fetch_backward(
286298
self: Self,
287299
backward_count: int,
@@ -293,6 +305,7 @@ class Cursor:
293305
### Returns:
294306
result as `QueryResult`.
295307
"""
308+
296309
async def fetch_backward_all(
297310
self: Self,
298311
) -> QueryResult:
@@ -327,13 +340,15 @@ class Transaction:
327340
328341
`begin()` can be called only once per transaction.
329342
"""
343+
330344
async def commit(self: Self) -> None:
331345
"""Commit the transaction.
332346
333347
Execute `COMMIT`.
334348
335349
`commit()` can be called only once per transaction.
336350
"""
351+
337352
async def execute(
338353
self: Self,
339354
querystring: str,
@@ -372,6 +387,7 @@ class Transaction:
372387
await transaction.commit()
373388
```
374389
"""
390+
375391
async def execute_many(
376392
self: Self,
377393
querystring: str,
@@ -430,6 +446,7 @@ class Transaction:
430446
- `prepared`: should the querystring be prepared before the request.
431447
By default any querystring will be prepared.
432448
"""
449+
433450
async def fetch_row(
434451
self: Self,
435452
querystring: str,
@@ -470,6 +487,7 @@ class Transaction:
470487
await transaction.commit()
471488
```
472489
"""
490+
473491
async def fetch_val(
474492
self: Self,
475493
querystring: str,
@@ -511,6 +529,7 @@ class Transaction:
511529
)
512530
```
513531
"""
532+
514533
async def pipeline(
515534
self,
516535
queries: list[tuple[str, list[Any] | None]],
@@ -577,6 +596,7 @@ class Transaction:
577596
578597
```
579598
"""
599+
580600
async def create_savepoint(self: Self, savepoint_name: str) -> None:
581601
"""Create new savepoint.
582602
@@ -606,6 +626,7 @@ class Transaction:
606626
await transaction.rollback_savepoint("my_savepoint")
607627
```
608628
"""
629+
609630
async def rollback(self: Self) -> None:
610631
"""Rollback all queries in the transaction.
611632
@@ -627,6 +648,7 @@ class Transaction:
627648
await transaction.rollback()
628649
```
629650
"""
651+
630652
async def rollback_savepoint(self: Self, savepoint_name: str) -> None:
631653
"""ROLLBACK to the specified `savepoint_name`.
632654
@@ -653,6 +675,7 @@ class Transaction:
653675
await transaction.rollback_savepoint("my_savepoint")
654676
```
655677
"""
678+
656679
async def release_savepoint(self: Self, savepoint_name: str) -> None:
657680
"""Execute ROLLBACK TO SAVEPOINT.
658681
@@ -678,6 +701,7 @@ class Transaction:
678701
await transaction.release_savepoint
679702
```
680703
"""
704+
681705
def cursor(
682706
self: Self,
683707
querystring: str,
@@ -771,6 +795,7 @@ class Connection:
771795
dict_result: List[Dict[Any, Any]] = query_result.result()
772796
```
773797
"""
798+
774799
async def execute_many(
775800
self: Self,
776801
querystring: str,
@@ -824,6 +849,7 @@ class Connection:
824849
- `prepared`: should the querystring be prepared before the request.
825850
By default any querystring will be prepared.
826851
"""
852+
827853
async def fetch_row(
828854
self: Self,
829855
querystring: str,
@@ -861,6 +887,7 @@ class Connection:
861887
dict_result: Dict[Any, Any] = query_result.result()
862888
```
863889
"""
890+
864891
async def fetch_val(
865892
self: Self,
866893
querystring: str,
@@ -901,6 +928,7 @@ class Connection:
901928
)
902929
```
903930
"""
931+
904932
def transaction(
905933
self,
906934
isolation_level: IsolationLevel | None = None,
@@ -1016,6 +1044,7 @@ class ConnectionPool:
10161044
- `max_db_pool_size`: maximum size of the connection pool.
10171045
- `conn_recycling_method`: how a connection is recycled.
10181046
"""
1047+
10191048
async def execute(
10201049
self: Self,
10211050
querystring: str,
@@ -1051,6 +1080,7 @@ class ConnectionPool:
10511080
# it will be dropped on Rust side.
10521081
```
10531082
"""
1083+
10541084
async def fetch(
10551085
self: Self,
10561086
querystring: str,
@@ -1071,11 +1101,13 @@ class ConnectionPool:
10711101
- `prepared`: should the querystring be prepared before the request.
10721102
By default any querystring will be prepared.
10731103
"""
1104+
10741105
async def connection(self: Self) -> Connection:
10751106
"""Create new connection.
10761107
10771108
It acquires new connection from the database pool.
10781109
"""
1110+
10791111
def close(self: Self) -> None:
10801112
"""Close the connection pool."""
10811113

src/additional_types.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ impl<'a> FromSql<'a> for RustLine {
8989
let second_coord = coord!(x: x2, y: y2);
9090

9191
let new_line = Line::new(first_coord, second_coord);
92-
return Ok(RustLine::new(new_line));
92+
Ok(RustLine::new(new_line))
9393
}
9494

9595
fn accepts(_ty: &Type) -> bool {
@@ -120,7 +120,7 @@ impl<'a> FromSql<'a> for RustPolygon {
120120

121121
let polygon_exterior = LineString::new(vec_coord);
122122
let new_polygon = Polygon::new(polygon_exterior, vec![]);
123-
return Ok(RustPolygon::new(new_polygon));
123+
Ok(RustPolygon::new(new_polygon))
124124
}
125125

126126
fn accepts(_ty: &Type) -> bool {
@@ -188,15 +188,6 @@ impl<T: CoordFloat> Circle<T> {
188188
}
189189
}
190190

191-
impl<T: CoordNum> Default for Circle<T> {
192-
fn default() -> Self {
193-
Self {
194-
center: coord! {x: T::zero(), y: T::zero()},
195-
radius: T::zero(),
196-
}
197-
}
198-
}
199-
200191
impl<'a> FromSql<'a> for Circle {
201192
fn from_sql(
202193
_ty: &Type,
@@ -215,7 +206,7 @@ impl<'a> FromSql<'a> for Circle {
215206
let r = buf.read_f64::<BigEndian>()?;
216207

217208
let new_circle = Circle::new(x, y, r);
218-
return Ok(new_circle);
209+
Ok(new_circle)
219210
}
220211

221212
fn accepts(_ty: &Type) -> bool {

src/value_converter.rs

Lines changed: 40 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -850,63 +850,54 @@ pub fn build_python_from_serde_value(
850850
pub fn build_point(value: Py<PyAny>) -> RustPSQLDriverPyResult<Point> {
851851
Python::with_gil(|gil| {
852852
let bind_value = value.bind(gil);
853-
if bind_value.is_instance_of::<PyList>()
854-
| bind_value.is_instance_of::<PyTuple>()
855-
| bind_value.is_instance_of::<PySet>()
853+
if !bind_value.is_instance_of::<PyList>()
854+
& !bind_value.is_instance_of::<PyTuple>()
855+
& !bind_value.is_instance_of::<PySet>()
856856
{
857-
let mut result_vec: Vec<f64> = vec![];
858-
let params = bind_value.extract::<Vec<Py<PyAny>>>()?;
857+
return Err(RustPSQLDriverError::PyToRustValueConversionError(
858+
"PyPoint must have pair int/float values combination passed in tuple, list or set."
859+
.to_string(),
860+
));
861+
}
862+
863+
let mut result_vec: Vec<f64> = vec![];
864+
let params = bind_value.extract::<Vec<Py<PyAny>>>()?;
865+
866+
if params.len() != 2 {
867+
return Err(RustPSQLDriverError::PyToRustValueConversionError(
868+
"PyPoint supports only pair of int/float combination.".to_string(),
869+
));
870+
}
859871

860-
if params.len() != 2 {
872+
for inner in params {
873+
let inner_bind = inner.bind(gil);
874+
875+
if !inner_bind.is_instance_of::<PyFloat>() & !inner_bind.is_instance_of::<PyInt>() {
861876
return Err(RustPSQLDriverError::PyToRustValueConversionError(
862-
"PyPoint supports only pair of int/float combination.".to_string(),
877+
"PyPoint supports only list/tuple/set pair of int/float combination."
878+
.to_string(),
863879
));
864880
}
865881

866-
for inner in params {
867-
let inner_bind = inner.bind(gil);
868-
869-
if inner_bind.is_instance_of::<PyFloat>() | inner_bind.is_instance_of::<PyInt>() {
870-
let python_dto = py_to_rust(inner_bind)?;
871-
match python_dto {
872-
PythonDTO::PyIntI16(pyint) => result_vec.push(pyint.try_into().unwrap()),
873-
PythonDTO::PyIntI32(pyint) => result_vec.push(pyint.try_into().unwrap()),
874-
PythonDTO::PyIntI64(_) => {
875-
return Err(RustPSQLDriverError::PyToRustValueConversionError(
876-
"Not implemented this type yet".into(),
877-
))
878-
}
879-
PythonDTO::PyIntU32(pyint) => result_vec.push(pyint.try_into().unwrap()),
880-
PythonDTO::PyIntU64(_) => {
881-
return Err(RustPSQLDriverError::PyToRustValueConversionError(
882-
"Not implemented this type yet".into(),
883-
))
884-
}
885-
PythonDTO::PyFloat32(pyfloat) => {
886-
result_vec.push(pyfloat.try_into().unwrap())
887-
}
888-
PythonDTO::PyFloat64(pyfloat) => result_vec.push(pyfloat),
889-
_ => {
890-
return Err(RustPSQLDriverError::PyToRustValueConversionError(
891-
"Incorrect types of point coordinates. It must be int or float"
892-
.into(),
893-
))
894-
}
895-
};
896-
} else {
882+
let python_dto = py_to_rust(inner_bind)?;
883+
match python_dto {
884+
PythonDTO::PyIntI16(pyint) => result_vec.push(pyint.into()),
885+
PythonDTO::PyIntI32(pyint) => result_vec.push(pyint.into()),
886+
PythonDTO::PyIntU32(pyint) => result_vec.push(pyint.into()),
887+
PythonDTO::PyFloat32(pyfloat) => result_vec.push(pyfloat.into()),
888+
PythonDTO::PyFloat64(pyfloat) => result_vec.push(pyfloat),
889+
PythonDTO::PyIntI64(_) | PythonDTO::PyIntU64(_) => {
897890
return Err(RustPSQLDriverError::PyToRustValueConversionError(
898-
"PyPoint supports only list/tuple/set pair of int/float combination."
899-
.to_string(),
900-
));
891+
"Not implemented this type yet".into(),
892+
))
901893
}
902-
}
903-
904-
return Ok(point!(x: result_vec[0], y: result_vec[1]));
894+
_ => {
895+
return Err(RustPSQLDriverError::PyToRustValueConversionError(
896+
"Incorrect types of point coordinates. It must be int or float".into(),
897+
))
898+
}
899+
};
905900
}
906-
907-
return Err(RustPSQLDriverError::PyToRustValueConversionError(
908-
"PyPoint must have pair int/float values combination passed in tuple, list or set."
909-
.to_string(),
910-
));
901+
Ok(point!(x: result_vec[0], y: result_vec[1]))
911902
})
912903
}

0 commit comments

Comments
 (0)