mirror of
https://github.com/davidhalter/typeshed.git
synced 2025-12-30 16:14:24 +08:00
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.
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
from typing import Any
|
||||
from collections.abc import Callable
|
||||
from typing import Any, TypeVar, overload
|
||||
|
||||
# connection and cursor not available at runtime
|
||||
from psycopg2._psycopg import (
|
||||
@@ -32,6 +33,18 @@ from psycopg2._psycopg import (
|
||||
threadsafety as threadsafety,
|
||||
)
|
||||
|
||||
_T_conn = TypeVar("_T_conn", bound=connection)
|
||||
|
||||
@overload
|
||||
def connect(dsn: str, connection_factory: Callable[..., _T_conn], cursor_factory: None = ..., **kwargs: Any) -> _T_conn: ...
|
||||
@overload
|
||||
def connect(
|
||||
dsn: Any | None = ..., connection_factory: Any | None = ..., cursor_factory: Any | None = ..., **kwargs
|
||||
dsn: str | None = ..., *, connection_factory: Callable[..., _T_conn], cursor_factory: None = ..., **kwargs: Any
|
||||
) -> _T_conn: ...
|
||||
@overload
|
||||
def connect(
|
||||
dsn: str | None = ...,
|
||||
connection_factory: Callable[..., connection] | None = ...,
|
||||
cursor_factory: Callable[..., cursor] | None = ...,
|
||||
**kwargs: Any,
|
||||
) -> connection: ...
|
||||
|
||||
Reference in New Issue
Block a user