]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Remove constraints on Row TypeVar
authorDenis Laxalde <denis.laxalde@dalibo.com>
Thu, 25 Feb 2021 07:24:29 +0000 (08:24 +0100)
committerDenis Laxalde <denis.laxalde@dalibo.com>
Fri, 23 Apr 2021 06:50:16 +0000 (08:50 +0200)
It's not clear why there is both Tuple[Any, ...] and Any as possible
types for Row and since Any is one possibility it seems that anything is
possible so those constraints appear to be useless.

We can now remove a few '# type: ignore[no-any-return]' in
*Cursor.fetchone(). On the other hand, mypy wants us to declare the type
of 'records' values coming from transformer's load_rows():

    error: Need type annotation for 'records' (hint: "records: List[<type>] = ...")  [var-annotated]

psycopg3/psycopg3/cursor.py
psycopg3/psycopg3/proto.py
psycopg3/psycopg3/server_cursor.py

index d678338d8b210f42846b89530f721c5454784c99..cb157f9df2e4d76c72b3f65d5959b3db0641d0ff 100644 (file)
@@ -546,7 +546,7 @@ class Cursor(BaseCursor["Connection"]):
         record = self._tx.load_row(self._pos)
         if record is not None:
             self._pos += 1
-        return record  # type: ignore[no-any-return]
+        return record
 
     def fetchmany(self, size: int = 0) -> List[Row]:
         """
@@ -561,7 +561,7 @@ class Cursor(BaseCursor["Connection"]):
 
         if not size:
             size = self.arraysize
-        records = self._tx.load_rows(
+        records: List[Row] = self._tx.load_rows(
             self._pos, min(self._pos + size, self.pgresult.ntuples)
         )
         self._pos += len(records)
@@ -575,7 +575,9 @@ class Cursor(BaseCursor["Connection"]):
         """
         self._check_result()
         assert self.pgresult
-        records = self._tx.load_rows(self._pos, self.pgresult.ntuples)
+        records: List[Row] = self._tx.load_rows(
+            self._pos, self.pgresult.ntuples
+        )
         self._pos = self.pgresult.ntuples
         return records
 
@@ -675,7 +677,7 @@ class AsyncCursor(BaseCursor["AsyncConnection"]):
         rv = self._tx.load_row(self._pos)
         if rv is not None:
             self._pos += 1
-        return rv  # type: ignore[no-any-return]
+        return rv
 
     async def fetchmany(self, size: int = 0) -> List[Row]:
         self._check_result()
@@ -683,7 +685,7 @@ class AsyncCursor(BaseCursor["AsyncConnection"]):
 
         if not size:
             size = self.arraysize
-        records = self._tx.load_rows(
+        records: List[Row] = self._tx.load_rows(
             self._pos, min(self._pos + size, self.pgresult.ntuples)
         )
         self._pos += len(records)
@@ -692,7 +694,9 @@ class AsyncCursor(BaseCursor["AsyncConnection"]):
     async def fetchall(self) -> List[Row]:
         self._check_result()
         assert self.pgresult
-        records = self._tx.load_rows(self._pos, self.pgresult.ntuples)
+        records: List[Row] = self._tx.load_rows(
+            self._pos, self.pgresult.ntuples
+        )
         self._pos = self.pgresult.ntuples
         return records
 
index a63e3efd56d9a015618a009270f9dbf527275eb7..b97858780a96758af4dd7e409f1662388aaed87e 100644 (file)
@@ -47,7 +47,7 @@ Wait states.
 
 # Row factories
 
-Row = TypeVar("Row", Tuple[Any, ...], Any)
+Row = TypeVar("Row")
 
 
 class RowMaker(Protocol):
index 09f4541e16087942dea8c711961a504c9d2f4b70..d15ccfcb3bad0706e9224bfe3d72955d43d73124 100644 (file)
@@ -242,10 +242,10 @@ class ServerCursor(BaseCursor["Connection"]):
 
     def fetchone(self) -> Optional[Row]:
         with self._conn.lock:
-            recs = self._conn.wait(self._helper._fetch_gen(self, 1))
+            recs: List[Row] = self._conn.wait(self._helper._fetch_gen(self, 1))
         if recs:
             self._pos += 1
-            return recs[0]  # type: ignore[no-any-return]
+            return recs[0]
         else:
             return None
 
@@ -253,20 +253,24 @@ class ServerCursor(BaseCursor["Connection"]):
         if not size:
             size = self.arraysize
         with self._conn.lock:
-            recs = self._conn.wait(self._helper._fetch_gen(self, size))
+            recs: List[Row] = self._conn.wait(
+                self._helper._fetch_gen(self, size)
+            )
         self._pos += len(recs)
         return recs
 
     def fetchall(self) -> Sequence[Row]:
         with self._conn.lock:
-            recs = self._conn.wait(self._helper._fetch_gen(self, None))
+            recs: List[Row] = self._conn.wait(
+                self._helper._fetch_gen(self, None)
+            )
         self._pos += len(recs)
         return recs
 
     def __iter__(self) -> Iterator[Row]:
         while True:
             with self._conn.lock:
-                recs = self._conn.wait(
+                recs: List[Row] = self._conn.wait(
                     self._helper._fetch_gen(self, self.itersize)
                 )
             for rec in recs:
@@ -359,10 +363,12 @@ class AsyncServerCursor(BaseCursor["AsyncConnection"]):
 
     async def fetchone(self) -> Optional[Row]:
         async with self._conn.lock:
-            recs = await self._conn.wait(self._helper._fetch_gen(self, 1))
+            recs: List[Row] = await self._conn.wait(
+                self._helper._fetch_gen(self, 1)
+            )
         if recs:
             self._pos += 1
-            return recs[0]  # type: ignore[no-any-return]
+            return recs[0]
         else:
             return None
 
@@ -370,20 +376,24 @@ class AsyncServerCursor(BaseCursor["AsyncConnection"]):
         if not size:
             size = self.arraysize
         async with self._conn.lock:
-            recs = await self._conn.wait(self._helper._fetch_gen(self, size))
+            recs: List[Row] = await self._conn.wait(
+                self._helper._fetch_gen(self, size)
+            )
         self._pos += len(recs)
         return recs
 
     async def fetchall(self) -> Sequence[Row]:
         async with self._conn.lock:
-            recs = await self._conn.wait(self._helper._fetch_gen(self, None))
+            recs: List[Row] = await self._conn.wait(
+                self._helper._fetch_gen(self, None)
+            )
         self._pos += len(recs)
         return recs
 
     async def __aiter__(self) -> AsyncIterator[Row]:
         while True:
             async with self._conn.lock:
-                recs = await self._conn.wait(
+                recs: List[Row] = await self._conn.wait(
                     self._helper._fetch_gen(self, self.itersize)
                 )
             for rec in recs: