From 4d5cb5dd6723cd5de646598c92efa154b67e9e8b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 4 Sep 2023 03:17:06 +0000 Subject: [PATCH] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .pre-commit-config.yaml | 2 +- duckdb_engine/__init__.py | 27 ++++++-------- duckdb_engine/datatypes.py | 37 +++++++++---------- duckdb_engine/tests/conftest.py | 4 +- .../tests/snapshots/snap_test_basic.py | 2 +- duckdb_engine/tests/test_basic.py | 22 +++++------ duckdb_engine/tests/test_datatypes.py | 5 +-- duckdb_engine/tests/test_ibis.py | 6 +-- duckdb_engine/tests/test_integration.py | 2 +- duckdb_engine/tests/test_pandas.py | 13 +++---- 10 files changed, 55 insertions(+), 65 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 2c36c395..8cfb5e4a 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -61,4 +61,4 @@ repos: - id: ruff args: - --fix - - --exit-non-zero-on-fix \ No newline at end of file + - --exit-non-zero-on-fix diff --git a/duckdb_engine/__init__.py b/duckdb_engine/__init__.py index 8cea1a91..0859f0c6 100644 --- a/duckdb_engine/__init__.py +++ b/duckdb_engine/__init__.py @@ -15,9 +15,8 @@ import duckdb import sqlalchemy -from sqlalchemy import pool, text +from sqlalchemy import pool, text, util from sqlalchemy import types as sqltypes -from sqlalchemy import util from sqlalchemy.dialects.postgresql import UUID from sqlalchemy.dialects.postgresql.base import PGDialect, PGInspector, PGTypeCompiler from sqlalchemy.dialects.postgresql.psycopg2 import PGDialect_psycopg2 @@ -55,12 +54,12 @@ def Binary(x: Any) -> Any: class DuckDBInspector(PGInspector): def get_check_constraints( - self, table_name: str, schema: Optional[str] = None, **kw: Any + self, table_name: str, schema: Optional[str] = None, **kw: Any, ) -> List[Dict[str, Any]]: try: return super().get_check_constraints(table_name, schema, **kw) except Exception as e: - raise NotImplementedError() from e + raise NotImplementedError from e class ConnectionWrapper: @@ -71,7 +70,7 @@ class ConnectionWrapper: def __init__(self, c: duckdb.DuckDBPyConnection) -> None: self.__c = c - self.notices = list() + self.notices = [] def cursor(self) -> "Connection": return self @@ -88,7 +87,7 @@ def fetchmany(self, size: Optional[int] = None) -> List: return cast(list, self.__c.fetch_df_chunk().values.tolist()) except RuntimeError as e: if e.args[0].startswith( - "Invalid Input Error: Attempting to fetch from an unsuccessful or closed streaming query result" + "Invalid Input Error: Attempting to fetch from an unsuccessful or closed streaming query result", ): return [] else: @@ -230,7 +229,7 @@ def _get_server_version_info(self, connection: "Connection") -> Tuple[int, int]: return (8, 0) def get_default_isolation_level(self, connection: "Connection") -> None: - raise NotImplementedError() + raise NotImplementedError def do_rollback(self, connection: "Connection") -> None: try: @@ -254,7 +253,7 @@ def get_view_names( ) -> Any: s = "SELECT table_name FROM information_schema.tables WHERE table_type='VIEW' and table_schema=:schema_name" rs = connection.execute( - text(s), {"schema_name": schema if schema is not None else "main"} + text(s), {"schema_name": schema if schema is not None else "main"}, ) return [row[0] for row in rs] @@ -293,10 +292,10 @@ def import_dbapi(cls: Type["Dialect"]) -> Type[DBAPI]: return cls.dbapi() def do_executemany( - self, cursor: Any, statement: Any, parameters: Any, context: Optional[Any] = ... + self, cursor: Any, statement: Any, parameters: Any, context: Optional[Any] = ..., ) -> None: return DefaultDialect.do_executemany( - self, cursor, statement, parameters, context + self, cursor, statement, parameters, context, ) # FIXME: this method is a hack around the fact that we use a single cursor for all queries inside a connection, @@ -310,8 +309,7 @@ def get_multi_columns( kind: Optional[Tuple[str, ...]] = None, **kw: Any, ) -> List: - """ - Copyright 2005-2023 SQLAlchemy authors and contributors . + """Copyright 2005-2023 SQLAlchemy authors and contributors . Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in @@ -331,7 +329,6 @@ def get_multi_columns( OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ - has_filter_names, params = self._prepare_filter_names(filter_names) # type: ignore[attr-defined] query = self._columns_query(schema, has_filter_names, scope, kind) # type: ignore[attr-defined] rows = list(connection.execute(query, params).mappings()) @@ -341,7 +338,7 @@ def get_multi_columns( domains = { ((d["schema"], d["name"]) if not d["visible"] else (d["name"],)): d for d in self._load_domains( # type: ignore[attr-defined] - connection, schema="*", info_cache=kw.get("info_cache") + connection, schema="*", info_cache=kw.get("info_cache"), ) } @@ -352,7 +349,7 @@ def get_multi_columns( if rec["visible"] else ((rec["schema"], rec["name"]), rec) for rec in self._load_enums( # type: ignore[attr-defined] - connection, schema="*", info_cache=kw.get("info_cache") + connection, schema="*", info_cache=kw.get("info_cache"), ) ) diff --git a/duckdb_engine/datatypes.py b/duckdb_engine/datatypes.py index e92627ba..62c9a934 100644 --- a/duckdb_engine/datatypes.py +++ b/duckdb_engine/datatypes.py @@ -1,5 +1,4 @@ -""" -See https://duckdb.org/docs/sql/data_types/numeric for more information +"""See https://duckdb.org/docs/sql/data_types/numeric for more information. Also ```sql @@ -32,7 +31,7 @@ class UInt32(Integer): class UInt16(Integer): - "AKA USMALLINT" + "AKA USMALLINT." class UInt8(Integer): @@ -40,13 +39,15 @@ class UInt8(Integer): class UTinyInteger(Integer): - "AKA UInt1" + "AKA UInt1." + name = "UTinyInt" # UTINYINT - 0 255 class TinyInteger(Integer): - "AKA Int1" + "AKA Int1." + name = "TinyInt" # TINYINT INT1 -128 127 @@ -91,8 +92,7 @@ def compile_uint(element: Integer, compiler: PGTypeCompiler, **kw: Any) -> str: class Struct(TypeEngine): - """ - Represents a STRUCT type in DuckDB + """Represents a STRUCT type in DuckDB. ```python from duckdb_engine.datatypes import Struct @@ -109,13 +109,12 @@ class Struct(TypeEngine): __visit_name__ = "struct" - def __init__(self, fields: Optional[Dict[str, TV]] = None): + def __init__(self, fields: Optional[Dict[str, TV]] = None) -> None: self.fields = fields class Map(TypeEngine): - """ - Represents a MAP type in DuckDB + """Represents a MAP type in DuckDB. ```python from duckdb_engine.datatypes import Map @@ -132,26 +131,25 @@ class Map(TypeEngine): key_type: TV value_type: TV - def __init__(self, key_type: TV, value_type: TV): + def __init__(self, key_type: TV, value_type: TV) -> None: self.key_type = key_type self.value_type = value_type def bind_processor( - self, dialect: Dialect + self, dialect: Dialect, ) -> Optional[Callable[[Optional[dict]], Optional[dict]]]: return lambda value: ( {"key": list(value), "value": list(value.values())} if value else None ) def result_processor( - self, dialect: Dialect, coltype: str + self, dialect: Dialect, coltype: str, ) -> Optional[Callable[[Optional[dict]], Optional[dict]]]: return lambda value: dict(zip(value["key"], value["value"])) if value else {} class Union(TypeEngine): - """ - Represents a UNION type in DuckDB + """Represents a UNION type in DuckDB. ```python from duckdb_engine.datatypes import Union @@ -167,7 +165,7 @@ class Union(TypeEngine): __visit_name__ = "union" fields: Dict[str, TV] - def __init__(self, fields: Dict[str, TV]): + def __init__(self, fields: Dict[str, TV]) -> None: self.fields = fields @@ -217,14 +215,15 @@ def struct_or_union( ) -> str: fields = instance.fields if fields is None: - raise exc.CompileError(f"DuckDB {repr(instance)} type requires fields") + msg = f"DuckDB {repr(instance)} type requires fields" + raise exc.CompileError(msg) return "({})".format( ", ".join( "{} {}".format( - identifier_preparer.quote_identifier(key), process_type(value, compiler) + identifier_preparer.quote_identifier(key), process_type(value, compiler), ) for key, value in fields.items() - ) + ), ) diff --git a/duckdb_engine/tests/conftest.py b/duckdb_engine/tests/conftest.py index b3a2c775..b0c98cf9 100644 --- a/duckdb_engine/tests/conftest.py +++ b/duckdb_engine/tests/conftest.py @@ -19,14 +19,14 @@ FuncT = TypeVar("FuncT", bound=Callable[..., Any]) -@fixture +@fixture() def engine() -> Engine: registry.register("duckdb", "duckdb_engine", "Dialect") return create_engine("duckdb:///:memory:") -@fixture +@fixture() def session(engine: Engine) -> Session: return sessionmaker(bind=engine)() diff --git a/duckdb_engine/tests/snapshots/snap_test_basic.py b/duckdb_engine/tests/snapshots/snap_test_basic.py index 5aca2ae2..1dca255f 100644 --- a/duckdb_engine/tests/snapshots/snap_test_basic.py +++ b/duckdb_engine/tests/snapshots/snap_test_basic.py @@ -12,5 +12,5 @@ "name": "id", "nullable": True, "type": GenericRepr("INTEGER()"), - } + }, ] diff --git a/duckdb_engine/tests/test_basic.py b/duckdb_engine/tests/test_basic.py index 302636f3..1e5dc90d 100644 --- a/duckdb_engine/tests/test_basic.py +++ b/duckdb_engine/tests/test_basic.py @@ -34,7 +34,7 @@ from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import Session, relationship, sessionmaker -from .. import DBAPI, Dialect +from duckdb_engine import DBAPI, Dialect try: # sqlalchemy 2 @@ -48,7 +48,7 @@ class Mapped(Generic[T]): # type: ignore[no-redef] pass -@fixture +@fixture() def engine() -> Engine: registry.register("duckdb", "duckdb_engine", "Dialect") @@ -61,7 +61,7 @@ def engine() -> Engine: class CompressedString(types.TypeDecorator): - """Custom Column Type""" + """Custom Column Type.""" impl = types.BLOB @@ -107,7 +107,7 @@ class IntervalModel(Base): field = Column(Interval) -@fixture +@fixture() def session(engine: Engine) -> Session: return sessionmaker(bind=engine)() @@ -172,7 +172,7 @@ def test_get_views(engine: Engine) -> None: con.execute(text("create view test as select 1")) con.execute( - text("create schema scheme; create view scheme.schema_test as select 1") + text("create schema scheme; create view scheme.schema_test as select 1"), ) views = engine.dialect.get_view_names(con) @@ -197,11 +197,11 @@ def test_preload_extension() -> None: # check that we get an error indicating that the extension was loaded with engine.connect() as conn, raises(Exception, match="HTTP HEAD"): conn.execute( - text("SELECT * FROM read_parquet('https://domain/path/to/file.parquet');") + text("SELECT * FROM read_parquet('https://domain/path/to/file.parquet');"), ) -@fixture +@fixture() def inspector(engine: Engine, session: Session) -> Inspector: session.execute(text("create table test (id int);")) session.commit() @@ -309,7 +309,7 @@ def test_binary(session: Session) -> None: def test_comment_support() -> None: - "comments not yet supported by duckdb" + "Comments not yet supported by duckdb." with raises(DBAPI.ParserException, match="syntax error"): duckdb.default_connection.execute('comment on sqlite_master is "hello world";') @@ -385,8 +385,8 @@ def test_url_config_and_dict_config() -> None: with eng.connect() as conn: res = conn.execute( text( - "select current_setting('worker_threads'), current_setting('memory_limit')" - ) + "select current_setting('worker_threads'), current_setting('memory_limit')", + ), ) row = res.first() assert row is not None @@ -395,7 +395,7 @@ def test_url_config_and_dict_config() -> None: def test_do_ping(tmp_path: Path, caplog: LogCaptureFixture) -> None: engine = create_engine( - "duckdb:///" + str(tmp_path / "db"), pool_pre_ping=True, pool_size=1 + "duckdb:///" + str(tmp_path / "db"), pool_pre_ping=True, pool_size=1, ) logger = cast(logging.Logger, engine.pool.logger) # type: ignore diff --git a/duckdb_engine/tests/test_datatypes.py b/duckdb_engine/tests/test_datatypes.py index 2f9eebc7..1f6f7718 100644 --- a/duckdb_engine/tests/test_datatypes.py +++ b/duckdb_engine/tests/test_datatypes.py @@ -12,12 +12,12 @@ from sqlalchemy.sql import sqltypes from sqlalchemy.types import JSON -from ..datatypes import Map, Struct, types +from duckdb_engine.datatypes import Map, Struct, types @mark.parametrize("coltype", types) def test_unsigned_integer_type( - engine: Engine, session: Session, coltype: Type[Integer] + engine: Engine, session: Session, coltype: Type[Integer], ) -> None: Base = declarative_base() @@ -126,7 +126,6 @@ class Entry(base): id = Column(Integer, primary_key=True, default=0) struct = Column(Struct(fields={"name": String})) map = Column(Map(String, Integer)) - # union = Column(Union(fields={"name": String, "age": Integer})) base.metadata.create_all(bind=engine) diff --git a/duckdb_engine/tests/test_ibis.py b/duckdb_engine/tests/test_ibis.py index d9f9c03b..606d20d7 100644 --- a/duckdb_engine/tests/test_ibis.py +++ b/duckdb_engine/tests/test_ibis.py @@ -1,6 +1,4 @@ -""" -these are largely just smoke tests -""" +"""these are largely just smoke tests.""" from csv import DictWriter from pathlib import Path @@ -15,7 +13,7 @@ from ibis.backends.duckdb import Backend -@fixture +@fixture() def ibis_conn() -> "Backend": import ibis diff --git a/duckdb_engine/tests/test_integration.py b/duckdb_engine/tests/test_integration.py index acf4497a..c624746b 100644 --- a/duckdb_engine/tests/test_integration.py +++ b/duckdb_engine/tests/test_integration.py @@ -22,7 +22,7 @@ def test_integration(engine: Engine) -> None: @mark.remote_data @mark.skipif( - "dev" in duckdb.__version__, reason="md extension not available for dev builds" # type: ignore[attr-defined] + "dev" in duckdb.__version__, reason="md extension not available for dev builds", # type: ignore[attr-defined] ) def test_motherduck() -> None: importorskip("duckdb", "0.7.1") diff --git a/duckdb_engine/tests/test_pandas.py b/duckdb_engine/tests/test_pandas.py index 34b8cfff..f71e03dc 100644 --- a/duckdb_engine/tests/test_pandas.py +++ b/duckdb_engine/tests/test_pandas.py @@ -1,9 +1,6 @@ #! /usr/bin/env python3 -# vim:fenc=utf-8 -""" -Test pandas functionality. -""" +"""Test pandas functionality.""" import pathlib import random @@ -29,7 +26,7 @@ None, "multi", ], # TODO Implement a callable insert method? - } + }, ) args = { @@ -38,7 +35,7 @@ } params = { k: list( - product(*(_v for _k, _v in _possible_args.items() if _k in args[k])) # type: ignore + product(*(_v for _k, _v in _possible_args.items() if _k in args[k])), # type: ignore ) for k in args } @@ -56,7 +53,7 @@ for cs in cast(List[Optional[int]], _possible_args["chunksize"]) if cs is not None ) -for i in range(sample_rowcount): +for _i in range(sample_rowcount): sample_data["datetime"].append(datetime.utcnow()) sample_data["int"].append(random.randint(0, 100)) sample_data["float"].append(round(random.random(), 5)) @@ -130,7 +127,7 @@ def test_read_sql_duckdb_table(tmp_path: pathlib.Path) -> None: db = str(tmp_path / "test_db.duckdb") con = duckdb.connect(database=db, read_only=False) df = pd.DataFrame( - {"a": [1, 2, 3, 4], "b": [0.1, 0.2, 0.3, 0.4], "c": ["a", "b", "c", "d"]} + {"a": [1, 2, 3, 4], "b": [0.1, 0.2, 0.3, 0.4], "c": ["a", "b", "c", "d"]}, ) con.register("df_view", df) con.execute("CREATE TABLE test_data AS SELECT * FROM df_view;")