Commit Graph

23 Commits

Author SHA1 Message Date
Nikita Sobolev
62a6c3d616 Annotate known magic-method return types (#9131) 2022-11-08 18:28:42 +00:00
Jelle Zijlstra
9733901770 psycopg2: __nonzero__ was removed (#8863)
Fixes #8862
2022-10-07 02:23:56 +01:00
Joel
8715951fca Improve return type for psycopg2 connect function (#8567)
When a `connection_factory` argument is provided to psycopg2's `connect` function, the function's return type now matches that of the factory output class. However, if `cursor_factory` is set and has a non-`None` value and/or `connection_factory` is not set or is `None`, the return type is simply `connection`, as before.
2022-08-20 16:34:13 -07:00
Joel
234ef7ed9d Additional return types for psycopg2 connections (#8528)
* Return types for psycopg2 dict connections

DB connection and cursor methods that deal with `dict`-style results now indicate the expected return types as implemented.

* Return types for psycopg2 namedtuple connections

DB connection and cursor methods that deal with `namedtuple` results now indicate the expected return types as implemented.

* Use ABC iterator

As required by flake8, the `Iterator` referenced in psycopg2's `extras` module has been switched to the `collections.abc.Iterator[T]` variant.

* Fix base psycopg2 cursor iter return type

The previous return type of `Self` is wrong; `cursor`'s `__iter__` method returns an `Iterator`.

* Correct return type for cursor iter and next methods

The previous attempt to fix the return type of `cursor`'s (and subclasses) `__iter__` method was misguided; they should indeed return `Self`. It's the `__next__` methods that had to be updated to return the correct record/row instance type.

* Comprehensive overloads for psycopg2 extra connections

Provides full method signatures for the `cursor` methods of the `DictConnection`, `RealDictConnection` and `NamedTupleConnection` types. Notably this includes a default value for `cursor_factory` in each case, while preserving the option to override the parameter manually.

* Have mypy ignore incompatible psycopg2 return types

The return types of the `fetch*` and `__next__` methods of `DictCursor`, `RealDictCursor` and `NamedTupleCursor` are incompatible with the base `cursor` class's corresponding methods' return types. However, this does accurately reflect reality, so ignore the mypy errors in those cases.

* Use ABC callable for psycopg2 extras module

As required by flake8, the `Callable` referenced in psycopg2's `extras` module has been switched to the `Callable` variant.

* Remove superfluous psycopg2 member overrides

Several members in the `DictCursorBase` type definition were entirely unnecessary, so they have been removed. In addition, adds a type to the `size` param of the `cursor`'s `fetchmany` method.
2022-08-16 21:48:05 -07:00
Tareq Al-Ahdal
4f6aa12446 Enhanced type hinting for psycopg2 stubs (#8500) 2022-08-08 23:08:17 +02:00
Alex Waygood
e3d4bdc91a Third-party stubs: enforce CamelCase for type alias names (#8256)
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
2022-07-19 08:23:24 +01:00
Sarah Hoffmann
4e0aaccdab Extend annotations for psycopg2.sql (#8263) 2022-07-11 16:31:39 +02:00
Jelle Zijlstra
cb9023988d psycopg2: stub improvements (#7964)
Fixes an entry from #7928 along with a number of other improvements.

I went off the C code:
https://github.com/psycopg/psycopg2/blob/master/psycopg/connection_type.c
2022-06-02 18:02:07 -07:00
PIG208
5be7976fe3 psycopg2: Fix the return type of Composable.as_string (#7984)
Signed-off-by: Zixuan James Li <359101898@qq.com>
2022-05-29 15:15:56 -07:00
David Robertson
e5594aac60 Annotations for psycopg2.ConnectionInfo (#7834)
* Annotations for psycopg2.ConnectionInfo

These annotations come from the documentation here:

https://www.psycopg.org/docs/extensions.html#psycopg2.extensions.ConnectionInfo
If there was doubt, I referred to the libpq documentation cited by
psycopg2's docs.

I wasn't completely sure about `dsn_parameters`. Psycopg2's docs list it
as an `dict`, and the example suggests it's a `dict[str, str]` at that.
From psycopg2's source I found

    1d3a89a0bb/psycopg/conninfo_type.c (L183-L206)

which is implemented here:

    1d3a89a0bb/psycopg/utils.c (L251-L279)

I'm no expert in CPython's API, but this looks to me like it's building
a `dict[str, str]`.

Additionally, the libpq docs

https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-PQCONNINFO
https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-PQCONNDEFAULTS

show that the underlying data just consists of strings.

Additionally, I'm pretty sure from this chunk of source

    1d3a89a0bb/psycopg/conninfo_type.c (L581-L598)

That `ConnectionInfo.__init__` takes one positional-only argument, which
must be a `psycopg2.connection`. But I don't think users are intended to
be constructing this type, so I've not added that annotation.

* Annotate `connection.info` and related attributes

* Make ConnectionInfo attributes properties

According to 1d3a89a0bb/psycopg/conninfo_type.c (L534-L563)

* Mark connection attributes as readonly

according to 8ef195f2ff/psycopg/connection_type.c (L1244)

* Explain why some properties aren't `T | None`
2022-05-21 07:38:52 -07:00
Sebastian Rittau
2a0fc1b582 Annotate Error and Diagnostics (#7671)
Move cursor class to top of file so it can be used as base class
2022-04-21 21:19:34 -07:00
Alex Waygood
b093c90a94 Use TypeAlias for type aliases where possible, part II (#7667) 2022-04-20 20:02:47 +01:00
Alex Waygood
740193a8fc Use TypeAlias where possible for type aliases (#7630) 2022-04-15 18:01:00 -07:00
Bao
79c654ef43 psycopg2: correct return type (#7607)
Fixes the return type of `psycopg2.cursor.fetchone()` to match the psycopg2 code:

1d3a89a0bb/psycopg/cursor_type.c (L647-L651)
1d3a89a0bb/psycopg/cursor_type.c (L748-L786)

It also matches the [psycopg2 documentation](https://www.psycopg.org/docs/cursor.html?highlight=copy_from#cursor.fetchone) as well as the [DB-API](https://peps.python.org/pep-0249/#fetchone)
2022-04-07 15:36:26 -07:00
Anders Kaseorg
b78f0c21ba psycopg2: Accept Composable in place of query string (#7494)
https://www.psycopg.org/docs/sql.html#psycopg2.sql.Composable
“Composable objects can be passed directly to execute(),
executemany(), copy_expert() in place of the query string.”

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
2022-03-15 20:47:30 -07:00
Sebastian Rittau
889164cb0c psycopg2: Annotate cursor execute* and dunder methods (#7460) 2022-03-08 08:13:19 -08:00
Alex Waygood
64af11c78f Correct several positional-only differences in third-party stubs (#7352) 2022-02-22 06:49:51 -08:00
Alex Waygood
8d5d2520ac Use PEP 585 syntax wherever possible (#6717) 2021-12-28 11:31:43 +01:00
Daniel Foerster
506be4fb0a Fix psycopg2 connection.cursor() stub (#6470)
Adds `scrollable=` argument missing since psycopg2 2.5 and prevents `Any` from being hinted when `cursor_factory=` is passed.
2021-12-02 17:50:25 +01:00
Joachim Jablon
e96dde7618 psycopg2: use Error and Warning from _psycopg.pyi in errors.pyi (#6454) 2021-11-30 15:26:50 +02:00
Joachim Jablon
7e836db2f3 Move abstract methods to AbstractConnectionPool (#6340) 2021-11-23 09:40:45 +01:00
Sebastian Rittau
9f86972350 Add star to all non-0.1 versions (#6146) 2021-10-11 13:41:19 -07:00
Jukka Lehtosalo
a0f199727b Stubs for psycopg2 (#5783) 2021-07-15 17:29:56 +02:00