Skip to content

Commit 1c610f6

Browse files
OutSquareCapitalevertlammerts
authored andcommitted
bugfix/refactor:
- Fixed StatementType members, they had incorrect values. the "_STATEMENT" part was only on the C++ side, not on the python side - Moved all enums in __init__ file in a new _enums.pyi file, to avoid bloating the init file - Created a new CppEnum Protocol, and used it as a base class for all public enums to reduce duplication. - Created literals type and using them as argument in conjunction of the corresponding enum whenever pertinent
1 parent ba2c469 commit 1c610f6

File tree

3 files changed

+135
-209
lines changed

3 files changed

+135
-209
lines changed

_duckdb-stubs/__init__.pyi

Lines changed: 16 additions & 177 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@ import pathlib
33
import typing
44
from typing_extensions import Self
55
from ._expression import Expression
6+
from ._enums import (
7+
CSVLineTerminator,
8+
StatementType,
9+
ExpectedResultType,
10+
ExplainType,
11+
PythonExceptionHandling,
12+
RenderMode,
13+
token_type,
14+
)
615

716
if typing.TYPE_CHECKING:
817
import fsspec
@@ -30,6 +39,7 @@ if typing.TYPE_CHECKING:
3039
HiveTypes,
3140
ColumnsTypes,
3241
)
42+
from ._enums import ExplainTypeLiteral, CSVLineTerminatorLiteral, RenderModeLiteral
3343
from duckdb import sqltypes, func
3444

3545
__all__: lst[str] = [
@@ -175,28 +185,6 @@ __all__: lst[str] = [
175185
]
176186

177187
class BinderException(ProgrammingError): ...
178-
179-
class CSVLineTerminator:
180-
CARRIAGE_RETURN_LINE_FEED: typing.ClassVar[
181-
CSVLineTerminator
182-
] # value = <CSVLineTerminator.CARRIAGE_RETURN_LINE_FEED: 1>
183-
LINE_FEED: typing.ClassVar[CSVLineTerminator] # value = <CSVLineTerminator.LINE_FEED: 0>
184-
__members__: typing.ClassVar[
185-
dict[str, CSVLineTerminator]
186-
] # value = {'LINE_FEED': <CSVLineTerminator.LINE_FEED: 0>, 'CARRIAGE_RETURN_LINE_FEED': <CSVLineTerminator.CARRIAGE_RETURN_LINE_FEED: 1>} # noqa: E501
187-
def __eq__(self, other: object) -> bool: ...
188-
def __getstate__(self) -> int: ...
189-
def __hash__(self) -> int: ...
190-
def __index__(self) -> int: ...
191-
def __init__(self, value: typing.SupportsInt) -> None: ...
192-
def __int__(self) -> int: ...
193-
def __ne__(self, other: object) -> bool: ...
194-
def __setstate__(self, state: typing.SupportsInt) -> None: ...
195-
@property
196-
def name(self) -> str: ...
197-
@property
198-
def value(self) -> int: ...
199-
200188
class CatalogException(ProgrammingError): ...
201189
class ConnectionException(OperationalError): ...
202190
class ConstraintException(IntegrityError): ...
@@ -283,7 +271,7 @@ class DuckDBPyConnection:
283271
normalize_names: bool | None = None,
284272
null_padding: bool | None = None,
285273
names: lst[str] | None = None,
286-
lineterminator: str | None = None,
274+
lineterminator: CSVLineTerminator | CSVLineTerminatorLiteral | None = None,
287275
columns: ColumnsTypes | None = None,
288276
auto_type_candidates: lst[StrIntoPyType] | None = None,
289277
max_line_size: int | None = None,
@@ -384,7 +372,7 @@ class DuckDBPyConnection:
384372
normalize_names: bool | None = None,
385373
null_padding: bool | None = None,
386374
names: lst[str] | None = None,
387-
lineterminator: str | None = None,
375+
lineterminator: CSVLineTerminator | CSVLineTerminatorLiteral | None = None,
388376
columns: ColumnsTypes | None = None,
389377
auto_type_candidates: lst[StrIntoPyType] | None = None,
390378
max_line_size: int | None = None,
@@ -546,7 +534,7 @@ class DuckDBPyRelation:
546534
def distinct(self) -> DuckDBPyRelation: ...
547535
def except_(self, other_rel: Self) -> DuckDBPyRelation: ...
548536
def execute(self) -> DuckDBPyRelation: ...
549-
def explain(self, type: ExplainType = ExplainType.STANDARD) -> str: ...
537+
def explain(self, type: ExplainType | ExplainTypeLiteral = ExplainType.STANDARD) -> str: ...
550538
def favg(
551539
self, expression: str, groups: str = "", window_spec: str = "", projected_columns: str = ""
552540
) -> DuckDBPyRelation: ...
@@ -689,7 +677,7 @@ class DuckDBPyRelation:
689677
max_rows: typing.SupportsInt | None = None,
690678
max_col_width: typing.SupportsInt | None = None,
691679
null_value: str | None = None,
692-
render_mode: RenderMode | None = None,
680+
render_mode: RenderMode | RenderModeLiteral | None = None,
693681
) -> None: ...
694682
def sort(self, *args: IntoExpr) -> DuckDBPyRelation: ...
695683
def sql_query(self) -> str: ...
@@ -822,46 +810,6 @@ class DuckDBPyRelation:
822810
def types(self) -> lst[sqltypes.DuckDBPyType]: ...
823811

824812
class Error(Exception): ...
825-
826-
class ExpectedResultType:
827-
CHANGED_ROWS: typing.ClassVar[ExpectedResultType] # value = <ExpectedResultType.CHANGED_ROWS: 1>
828-
NOTHING: typing.ClassVar[ExpectedResultType] # value = <ExpectedResultType.NOTHING: 2>
829-
QUERY_RESULT: typing.ClassVar[ExpectedResultType] # value = <ExpectedResultType.QUERY_RESULT: 0>
830-
__members__: typing.ClassVar[
831-
dict[str, ExpectedResultType]
832-
] # value = {'QUERY_RESULT': <ExpectedResultType.QUERY_RESULT: 0>, 'CHANGED_ROWS': <ExpectedResultType.CHANGED_ROWS: 1>, 'NOTHING': <ExpectedResultType.NOTHING: 2>} # noqa: E501
833-
def __eq__(self, other: object) -> bool: ...
834-
def __getstate__(self) -> int: ...
835-
def __hash__(self) -> int: ...
836-
def __index__(self) -> int: ...
837-
def __init__(self, value: typing.SupportsInt) -> None: ...
838-
def __int__(self) -> int: ...
839-
def __ne__(self, other: object) -> bool: ...
840-
def __setstate__(self, state: typing.SupportsInt) -> None: ...
841-
@property
842-
def name(self) -> str: ...
843-
@property
844-
def value(self) -> int: ...
845-
846-
class ExplainType:
847-
ANALYZE: typing.ClassVar[ExplainType] # value = <ExplainType.ANALYZE: 1>
848-
STANDARD: typing.ClassVar[ExplainType] # value = <ExplainType.STANDARD: 0>
849-
__members__: typing.ClassVar[
850-
dict[str, ExplainType]
851-
] # value = {'STANDARD': <ExplainType.STANDARD: 0>, 'ANALYZE': <ExplainType.ANALYZE: 1>}
852-
def __eq__(self, other: object) -> bool: ...
853-
def __getstate__(self) -> int: ...
854-
def __hash__(self) -> int: ...
855-
def __index__(self) -> int: ...
856-
def __init__(self, value: typing.SupportsInt) -> None: ...
857-
def __int__(self) -> int: ...
858-
def __ne__(self, other: object) -> bool: ...
859-
def __setstate__(self, state: typing.SupportsInt) -> None: ...
860-
@property
861-
def name(self) -> str: ...
862-
@property
863-
def value(self) -> int: ...
864-
865813
class FatalException(DatabaseError): ...
866814

867815
class HTTPException(IOException):
@@ -885,45 +833,6 @@ class OutOfRangeException(DataError): ...
885833
class ParserException(ProgrammingError): ...
886834
class PermissionException(DatabaseError): ...
887835
class ProgrammingError(DatabaseError): ...
888-
889-
class PythonExceptionHandling:
890-
DEFAULT: typing.ClassVar[PythonExceptionHandling] # value = <PythonExceptionHandling.DEFAULT: 0>
891-
RETURN_NULL: typing.ClassVar[PythonExceptionHandling] # value = <PythonExceptionHandling.RETURN_NULL: 1>
892-
__members__: typing.ClassVar[
893-
dict[str, PythonExceptionHandling]
894-
] # value = {'DEFAULT': <PythonExceptionHandling.DEFAULT: 0>, 'RETURN_NULL': <PythonExceptionHandling.RETURN_NULL: 1>} # noqa: E501
895-
def __eq__(self, other: object) -> bool: ...
896-
def __getstate__(self) -> int: ...
897-
def __hash__(self) -> int: ...
898-
def __index__(self) -> int: ...
899-
def __init__(self, value: typing.SupportsInt) -> None: ...
900-
def __int__(self) -> int: ...
901-
def __ne__(self, other: object) -> bool: ...
902-
def __setstate__(self, state: typing.SupportsInt) -> None: ...
903-
@property
904-
def name(self) -> str: ...
905-
@property
906-
def value(self) -> int: ...
907-
908-
class RenderMode:
909-
COLUMNS: typing.ClassVar[RenderMode] # value = <RenderMode.COLUMNS: 1>
910-
ROWS: typing.ClassVar[RenderMode] # value = <RenderMode.ROWS: 0>
911-
__members__: typing.ClassVar[
912-
dict[str, RenderMode]
913-
] # value = {'ROWS': <RenderMode.ROWS: 0>, 'COLUMNS': <RenderMode.COLUMNS: 1>}
914-
def __eq__(self, other: object) -> bool: ...
915-
def __getstate__(self) -> int: ...
916-
def __hash__(self) -> int: ...
917-
def __index__(self) -> int: ...
918-
def __init__(self, value: typing.SupportsInt) -> None: ...
919-
def __int__(self) -> int: ...
920-
def __ne__(self, other: object) -> bool: ...
921-
def __setstate__(self, state: typing.SupportsInt) -> None: ...
922-
@property
923-
def name(self) -> str: ...
924-
@property
925-
def value(self) -> int: ...
926-
927836
class SequenceException(DatabaseError): ...
928837
class SerializationException(OperationalError): ...
929838

@@ -937,81 +846,11 @@ class Statement:
937846
@property
938847
def type(self) -> StatementType: ...
939848

940-
class StatementType:
941-
ALTER_STATEMENT: typing.ClassVar[StatementType] # value = <StatementType.ALTER_STATEMENT: 8>
942-
ANALYZE_STATEMENT: typing.ClassVar[StatementType] # value = <StatementType.ANALYZE_STATEMENT: 11>
943-
ATTACH_STATEMENT: typing.ClassVar[StatementType] # value = <StatementType.ATTACH_STATEMENT: 25>
944-
CALL_STATEMENT: typing.ClassVar[StatementType] # value = <StatementType.CALL_STATEMENT: 19>
945-
COPY_DATABASE_STATEMENT: typing.ClassVar[StatementType] # value = <StatementType.COPY_DATABASE_STATEMENT: 28>
946-
COPY_STATEMENT: typing.ClassVar[StatementType] # value = <StatementType.COPY_STATEMENT: 10>
947-
CREATE_FUNC_STATEMENT: typing.ClassVar[StatementType] # value = <StatementType.CREATE_FUNC_STATEMENT: 13>
948-
CREATE_STATEMENT: typing.ClassVar[StatementType] # value = <StatementType.CREATE_STATEMENT: 4>
949-
DELETE_STATEMENT: typing.ClassVar[StatementType] # value = <StatementType.DELETE_STATEMENT: 5>
950-
DETACH_STATEMENT: typing.ClassVar[StatementType] # value = <StatementType.DETACH_STATEMENT: 26>
951-
DROP_STATEMENT: typing.ClassVar[StatementType] # value = <StatementType.DROP_STATEMENT: 15>
952-
EXECUTE_STATEMENT: typing.ClassVar[StatementType] # value = <StatementType.EXECUTE_STATEMENT: 7>
953-
EXPLAIN_STATEMENT: typing.ClassVar[StatementType] # value = <StatementType.EXPLAIN_STATEMENT: 14>
954-
EXPORT_STATEMENT: typing.ClassVar[StatementType] # value = <StatementType.EXPORT_STATEMENT: 16>
955-
EXTENSION_STATEMENT: typing.ClassVar[StatementType] # value = <StatementType.EXTENSION_STATEMENT: 23>
956-
INSERT_STATEMENT: typing.ClassVar[StatementType] # value = <StatementType.INSERT_STATEMENT: 2>
957-
INVALID_STATEMENT: typing.ClassVar[StatementType] # value = <StatementType.INVALID_STATEMENT: 0>
958-
LOAD_STATEMENT: typing.ClassVar[StatementType] # value = <StatementType.LOAD_STATEMENT: 21>
959-
LOGICAL_PLAN_STATEMENT: typing.ClassVar[StatementType] # value = <StatementType.LOGICAL_PLAN_STATEMENT: 24>
960-
MERGE_INTO_STATEMENT: typing.ClassVar[StatementType] # value = <StatementType.MERGE_INTO_STATEMENT: 30>
961-
MULTI_STATEMENT: typing.ClassVar[StatementType] # value = <StatementType.MULTI_STATEMENT: 27>
962-
PRAGMA_STATEMENT: typing.ClassVar[StatementType] # value = <StatementType.PRAGMA_STATEMENT: 17>
963-
PREPARE_STATEMENT: typing.ClassVar[StatementType] # value = <StatementType.PREPARE_STATEMENT: 6>
964-
RELATION_STATEMENT: typing.ClassVar[StatementType] # value = <StatementType.RELATION_STATEMENT: 22>
965-
SELECT_STATEMENT: typing.ClassVar[StatementType] # value = <StatementType.SELECT_STATEMENT: 1>
966-
SET_STATEMENT: typing.ClassVar[StatementType] # value = <StatementType.SET_STATEMENT: 20>
967-
TRANSACTION_STATEMENT: typing.ClassVar[StatementType] # value = <StatementType.TRANSACTION_STATEMENT: 9>
968-
UPDATE_STATEMENT: typing.ClassVar[StatementType] # value = <StatementType.UPDATE_STATEMENT: 3>
969-
VACUUM_STATEMENT: typing.ClassVar[StatementType] # value = <StatementType.VACUUM_STATEMENT: 18>
970-
VARIABLE_SET_STATEMENT: typing.ClassVar[StatementType] # value = <StatementType.VARIABLE_SET_STATEMENT: 12>
971-
__members__: typing.ClassVar[
972-
dict[str, StatementType]
973-
] # value = {'INVALID_STATEMENT': <StatementType.INVALID_STATEMENT: 0>, 'SELECT_STATEMENT': <StatementType.SELECT_STATEMENT: 1>, 'INSERT_STATEMENT': <StatementType.INSERT_STATEMENT: 2>, 'UPDATE_STATEMENT': <StatementType.UPDATE_STATEMENT: 3>, 'CREATE_STATEMENT': <StatementType.CREATE_STATEMENT: 4>, 'DELETE_STATEMENT': <StatementType.DELETE_STATEMENT: 5>, 'PREPARE_STATEMENT': <StatementType.PREPARE_STATEMENT: 6>, 'EXECUTE_STATEMENT': <StatementType.EXECUTE_STATEMENT: 7>, 'ALTER_STATEMENT': <StatementType.ALTER_STATEMENT: 8>, 'TRANSACTION_STATEMENT': <StatementType.TRANSACTION_STATEMENT: 9>, 'COPY_STATEMENT': <StatementType.COPY_STATEMENT: 10>, 'ANALYZE_STATEMENT': <StatementType.ANALYZE_STATEMENT: 11>, 'VARIABLE_SET_STATEMENT': <StatementType.VARIABLE_SET_STATEMENT: 12>, 'CREATE_FUNC_STATEMENT': <StatementType.CREATE_FUNC_STATEMENT: 13>, 'EXPLAIN_STATEMENT': <StatementType.EXPLAIN_STATEMENT: 14>, 'DROP_STATEMENT': <StatementType.DROP_STATEMENT: 15>, 'EXPORT_STATEMENT': <StatementType.EXPORT_STATEMENT: 16>, 'PRAGMA_STATEMENT': <StatementType.PRAGMA_STATEMENT: 17>, 'VACUUM_STATEMENT': <StatementType.VACUUM_STATEMENT: 18>, 'CALL_STATEMENT': <StatementType.CALL_STATEMENT: 19>, 'SET_STATEMENT': <StatementType.SET_STATEMENT: 20>, 'LOAD_STATEMENT': <StatementType.LOAD_STATEMENT: 21>, 'RELATION_STATEMENT': <StatementType.RELATION_STATEMENT: 22>, 'EXTENSION_STATEMENT': <StatementType.EXTENSION_STATEMENT: 23>, 'LOGICAL_PLAN_STATEMENT': <StatementType.LOGICAL_PLAN_STATEMENT: 24>, 'ATTACH_STATEMENT': <StatementType.ATTACH_STATEMENT: 25>, 'DETACH_STATEMENT': <StatementType.DETACH_STATEMENT: 26>, 'MULTI_STATEMENT': <StatementType.MULTI_STATEMENT: 27>, 'COPY_DATABASE_STATEMENT': <StatementType.COPY_DATABASE_STATEMENT: 28>, 'MERGE_INTO_STATEMENT': <StatementType.MERGE_INTO_STATEMENT: 30>} # noqa: E501
974-
def __eq__(self, other: object) -> bool: ...
975-
def __getstate__(self) -> int: ...
976-
def __hash__(self) -> int: ...
977-
def __index__(self) -> int: ...
978-
def __init__(self, value: typing.SupportsInt) -> None: ...
979-
def __int__(self) -> int: ...
980-
def __ne__(self, other: object) -> bool: ...
981-
def __setstate__(self, state: typing.SupportsInt) -> None: ...
982-
@property
983-
def name(self) -> str: ...
984-
@property
985-
def value(self) -> int: ...
986-
987849
class SyntaxException(ProgrammingError): ...
988850
class TransactionException(OperationalError): ...
989851
class TypeMismatchException(DataError): ...
990852
class Warning(Exception): ...
991853

992-
class token_type:
993-
__members__: typing.ClassVar[
994-
dict[str, token_type]
995-
] # value = {'identifier': <token_type.identifier: 0>, 'numeric_const': <token_type.numeric_const: 1>, 'string_const': <token_type.string_const: 2>, 'operator': <token_type.operator: 3>, 'keyword': <token_type.keyword: 4>, 'comment': <token_type.comment: 5>} # noqa: E501
996-
comment: typing.ClassVar[token_type] # value = <token_type.comment: 5>
997-
identifier: typing.ClassVar[token_type] # value = <token_type.identifier: 0>
998-
keyword: typing.ClassVar[token_type] # value = <token_type.keyword: 4>
999-
numeric_const: typing.ClassVar[token_type] # value = <token_type.numeric_const: 1>
1000-
operator: typing.ClassVar[token_type] # value = <token_type.operator: 3>
1001-
string_const: typing.ClassVar[token_type] # value = <token_type.string_const: 2>
1002-
def __eq__(self, other: object) -> bool: ...
1003-
def __getstate__(self) -> int: ...
1004-
def __hash__(self) -> int: ...
1005-
def __index__(self) -> int: ...
1006-
def __init__(self, value: typing.SupportsInt) -> None: ...
1007-
def __int__(self) -> int: ...
1008-
def __ne__(self, other: object) -> bool: ...
1009-
def __setstate__(self, state: typing.SupportsInt) -> None: ...
1010-
@property
1011-
def name(self) -> str: ...
1012-
@property
1013-
def value(self) -> int: ...
1014-
1015854
def CaseExpression(condition: IntoExpr, value: IntoExpr) -> Expression: ...
1016855
def CoalesceOperator(*args: IntoExpr) -> Expression: ...
1017856
def ColumnExpression(*args: str) -> Expression: ...
@@ -1170,7 +1009,7 @@ def from_csv_auto(
11701009
normalize_names: bool | None = None,
11711010
null_padding: bool | None = None,
11721011
names: lst[str] | None = None,
1173-
lineterminator: str | None = None,
1012+
lineterminator: CSVLineTerminator | CSVLineTerminatorLiteral | None = None,
11741013
columns: ColumnsTypes | None = None,
11751014
auto_type_candidates: lst[StrIntoPyType] | None = None,
11761015
max_line_size: int | None = None,
@@ -1317,7 +1156,7 @@ def read_csv(
13171156
normalize_names: bool | None = None,
13181157
null_padding: bool | None = None,
13191158
names: lst[str] | None = None,
1320-
lineterminator: str | None = None,
1159+
lineterminator: CSVLineTerminator | CSVLineTerminatorLiteral | None = None,
13211160
columns: ColumnsTypes | None = None,
13221161
auto_type_candidates: lst[StrIntoPyType] | None = None,
13231162
max_line_size: int | None = None,

0 commit comments

Comments
 (0)