cardinal_pythonlib.typing_helpers


Original code copyright (C) 2009-2022 Rudolf Cardinal (rudolf@pobox.com).

This file is part of cardinal_pythonlib.

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.


Methods and unusual types for type hints.

class cardinal_pythonlib.typing_helpers.CSVWriterType[source]

Type hint for the result of csv.writer()

See https://stackoverflow.com/questions/51264355/how-to-type-annotate-object-returned-by-csv-writer

class cardinal_pythonlib.typing_helpers.Pep249DatabaseConnectionType[source]

Type hint for a database connection compliant with PEP 249. See https://www.python.org/dev/peps/pep-0249/.

Not supported:

abstract close() None[source]

See https://www.python.org/dev/peps/pep-0249/#connection-objects

abstract commit() None[source]

See https://www.python.org/dev/peps/pep-0249/#connection-objects

abstract cursor() Pep249DatabaseCursorType[source]

See https://www.python.org/dev/peps/pep-0249/#connection-objects

abstract property messages: List[Tuple[Type, Any]]

See https://www.python.org/dev/peps/pep-0249/#optional-db-api-extensions

abstract rollback() None[source]

See https://www.python.org/dev/peps/pep-0249/#connection-objects

class cardinal_pythonlib.typing_helpers.Pep249DatabaseCursorType(*args, **kwargs)[source]

Type hint for a database cursor compliant with PEP 249. See https://www.python.org/dev/peps/pep-0249/#cursor-objects

Example, as per https://docs.python.org/3.6/library/sqlite3.html:

import sqlite3
conn = sqlite3.connect(':memory:')
c = conn.cursor()
c.execute('''
    CREATE TABLE stocks
    (date text, trans text, symbol text, qty real, price real)
''')
c.execute('''
    INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14)
''')
conn.commit()

c.execute("SELECT * FROM stocks")
print(repr(c.description))

help(c)

See also:

abstract property arraysize: int

//www.python.org/dev/peps/pep-0249/#cursor-objects

Type:

See https

abstract callproc(procname: str, *args, **kwargs) None[source]

See https://www.python.org/dev/peps/pep-0249/#cursor-objects

abstract close() None[source]

See https://www.python.org/dev/peps/pep-0249/#cursor-objects

abstract property connection: Pep249DatabaseConnectionType

See https://www.python.org/dev/peps/pep-0249/#optional-db-api-extensions

abstract property description: Sequence[Sequence[Any]] | None

A sequence of column_description objects, where each column_description describes one result column and has the following items:

  • name: str

  • type_code: Optional[Type]? Not sure.

  • display_size: Optional[int]

  • internal_size: Optional[int]

  • precision: Optional[int]

  • scale: Optional[int]

  • null_ok: Optional[bool]

The attribute is None for operations that don’t return rows, and for un-executed cursors.

abstract execute(operation: str, *args, **kwargs) None[source]

See https://www.python.org/dev/peps/pep-0249/#cursor-objects

abstract executemany(operation: str, *args, **kwargs) None[source]

See https://www.python.org/dev/peps/pep-0249/#cursor-objects

abstract fetchall() Sequence[Sequence[Any]][source]

See https://www.python.org/dev/peps/pep-0249/#cursor-objects

abstract fetchmany(size: int | None = None) Sequence[Sequence[Any]][source]

See https://www.python.org/dev/peps/pep-0249/#cursor-objects

abstract fetchone() Sequence[Any] | None[source]

See https://www.python.org/dev/peps/pep-0249/#cursor-objects

abstract property lastrowid: int | None

See https://www.python.org/dev/peps/pep-0249/#optional-db-api-extensions

abstract property messages: List[Tuple[Type, Any]]

See https://www.python.org/dev/peps/pep-0249/#optional-db-api-extensions

abstract next() Sequence[Any][source]

See https://www.python.org/dev/peps/pep-0249/#optional-db-api-extensions

abstract nextset() bool | None[source]

See https://www.python.org/dev/peps/pep-0249/#cursor-objects

abstract property rowcount: int

//www.python.org/dev/peps/pep-0249/#cursor-objects

Type:

See https

abstract property rownumber: int | None

See https://www.python.org/dev/peps/pep-0249/#optional-db-api-extensions

abstract scroll(value: int, mode: str = 'relative') None[source]

See https://www.python.org/dev/peps/pep-0249/#optional-db-api-extensions

abstract setinputsizes(sizes: Sequence[Type | int]) None[source]

See https://www.python.org/dev/peps/pep-0249/#cursor-objects

abstract setoutputsize(size: int, column: int | None) None[source]

See https://www.python.org/dev/peps/pep-0249/#cursor-objects

cardinal_pythonlib.typing_helpers.with_typehint(baseclass: Type[T]) Type[T][source]

Useful function to make mixins with type hints from a base class.

From https://stackoverflow.com/questions/51930339/how-do-i-correctly-add-type-hints-to-mixin-classes.

Specimen usage:

class MyMixin1(with_typehint(SomeBaseClass))):
    # ...
cardinal_pythonlib.typing_helpers.with_typehints(*baseclasses: Type[T]) Type[T][source]

Useful function to make mixins with type hints from multiple base classes.

From https://stackoverflow.com/questions/51930339/how-do-i-correctly-add-type-hints-to-mixin-classes.

Specimen usage:

class MyMixin2(*with_typehints(SomeBaseClass, AnotherBaseClass))):
    # ...