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:

close() → None[source]

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

commit() → None[source]

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

cursor() → cardinal_pythonlib.typing_helpers.Pep249DatabaseCursorType[source]

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

messages

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

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:

arraysize

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

Type:See https
callproc(procname: str, *args, **kwargs) → None[source]

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

close() → None[source]

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

connection

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

description

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.

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

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

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

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

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

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

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

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

fetchone() → Optional[Sequence[Any]][source]

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

lastrowid

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

messages

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

next() → Sequence[Any][source]

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

nextset() → Optional[bool][source]

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

rowcount

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

Type:See https
rownumber

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

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

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

setinputsizes(sizes: Sequence[Union[Type[CT_co], int]]) → None[source]

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

setoutputsize(size: int, column: Optional[int]) → 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][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))):
    # ...