Skip to content
Closed
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
511e3ea
Add type annotations for `peewee`
Nov 18, 2020
7bbba59
Merge branch 'master' into peewee
Dec 1, 2020
b7eebcd
More work on annotations
Dec 1, 2020
1f7af9f
Commit final parts of first draft of the PR.
Jan 16, 2021
92dff01
Incremental fixes
Jan 26, 2021
0d047c4
Merge branch 'master' into peewee
dargueta Apr 14, 2021
2fa7292
Add stuff, reformat according to Black
Nov 17, 2021
41eedc7
Merge branch 'master' into peewee
Nov 17, 2021
fa81e48
A few more tweaks to int/float confusion
Nov 17, 2021
04ed2a9
Merge branch 'master' into peewee
Feb 20, 2022
9175536
Hopefully the last of the linter fixes, thanks to @pylipp
Feb 20, 2022
e3acec8
Merge branch 'master' into peewee
AlexWaygood Apr 18, 2022
3153594
Fix location of new stub file
AlexWaygood Apr 18, 2022
30d6289
Fix many lint errors
AlexWaygood Apr 18, 2022
d2a9556
Add to stricter-pyright ignorelist
AlexWaygood Apr 18, 2022
8adc6b0
Fix a few obvious-ish things
Akuli Apr 25, 2022
12f0e6f
use _typeshed.Self when appropriate
Akuli Apr 25, 2022
18795ac
Merge branch 'master' into peewee
AlexWaygood May 18, 2022
e8e4b80
Merge branch 'master' into peewee
AlexWaygood May 19, 2022
9d44ac6
More lint fixes
AlexWaygood May 19, 2022
0504beb
Merge branch 'master' into peewee
AlexWaygood May 19, 2022
9a7c5f9
Swap a few 'incomplete' comments for `_typeshed.Incomplete`
AlexWaygood Jun 15, 2022
5011f56
Fix `TFunc`-related issues
AlexWaygood Jun 15, 2022
719dd62
Nit: Rename several `TypeVar`s to use suffixes instead of prefixes
AlexWaygood Jun 15, 2022
fda102c
Add some missing type parameters, use some more `Incomplete` where I'…
AlexWaygood Jun 15, 2022
45fb0c2
Fix a few mypy complaints, falling back to `Incomplete` when I'm unsure
AlexWaygood Jun 15, 2022
3e31653
Mark more things as incomplete
AlexWaygood Jun 19, 2022
589ad8d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 19, 2022
a63e648
More fixes
AlexWaygood Jun 19, 2022
e86cbd2
Merge
AlexWaygood Jun 19, 2022
0e80802
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 19, 2022
503a71a
Merge branch 'master' into peewee
AlexWaygood Oct 3, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Nit: Rename several TypeVars to use suffixes instead of prefixes
  • Loading branch information
AlexWaygood committed Jun 15, 2022
commit 719dd627dc513e3e5c7d78bd14a6ba459be78efb
151 changes: 76 additions & 75 deletions stubs/peewee/peewee.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ from typing import Any, AnyStr, ClassVar, Generic, NamedTuple, NoReturn, Pattern
from typing_extensions import Concatenate, Literal, ParamSpec, TypeAlias

_T = TypeVar("_T")
_TModel = TypeVar("_TModel", bound=Model)
_TConvFunc = Callable[[Any], Any]
_TFunc = TypeVar("_TFunc", bound=Callable[..., Any])
_TClass = TypeVar("_TClass", bound=type)
_TContextClass = TypeVar("_TContextClass", bound=Context)
_TField = TypeVar("_TField", bound=Field)
_TNode = TypeVar("_TNode", bound=Node)
_ModelT = TypeVar("_ModelT", bound=Model)
_FuncT = TypeVar("_FuncT", bound=Callable[..., Any])
_ClassT = TypeVar("_ClassT", bound=type)
_ContextClassT = TypeVar("_ContextClassT", bound=Context)
_FieldT = TypeVar("_FieldT", bound=Field)
_NodeT = TypeVar("_NodeT", bound=Node)
_P = ParamSpec("_P")

_ConvFunc: TypeAlias = Callable[[Any], Any]

__version__: str

__all__ = [
Expand Down Expand Up @@ -167,14 +168,14 @@ MODEL_BASE: str

# TODO (dargueta)
class _callable_context_manager:
def __call__(self, fn: _TFunc) -> _TFunc: ...
def __call__(self, fn: _FuncT) -> _FuncT: ...

class Proxy:
obj: Any
def initialize(self, obj: object) -> None: ...
def attach_callback(self, callback: _TConvFunc) -> _TConvFunc: ...
def attach_callback(self, callback: _ConvFunc) -> _ConvFunc: ...
@staticmethod # This is technically inaccurate but that's how it's used
def passthrough(method: _TFunc) -> _TFunc: ...
def passthrough(method: _FuncT) -> _FuncT: ...
def __enter__(self) -> Any: ...
def __exit__(self, exc_type, exc_val, exc_tb) -> Any: ...
def __getattr__(self, attr: str) -> Any: ...
Expand Down Expand Up @@ -239,7 +240,7 @@ class Context:
# TODO (dargueta): Is this right?
def sql(self, obj: object) -> Context: ...
def literal(self: Self, keyword: str) -> Self: ...
def value(self, value: object, converter: _TConvFunc | None = ..., add_param: bool = ...) -> Context: ...
def value(self, value: object, converter: _ConvFunc | None = ..., add_param: bool = ...) -> Context: ...
def __sql__(self, ctx: Context) -> Context: ...
def parse(self, node: Node) -> tuple[str, tuple | None]: ...
def query(self) -> tuple[str, tuple | None]: ...
Expand Down Expand Up @@ -391,8 +392,8 @@ class CTE(_HashableSource, Source):
def __sql__(self, ctx: Context) -> Context: ...

class ColumnBase(Node):
_converter: _TConvFunc | None
def converter(self: Self, converter: _TConvFunc | None = ...) -> Self: ...
_converter: _ConvFunc | None
def converter(self: Self, converter: _ConvFunc | None = ...) -> Self: ...
@overload
def alias(self: Self, alias: None) -> Self: ...
@overload
Expand Down Expand Up @@ -456,13 +457,13 @@ class Column(ColumnBase):
def __hash__(self) -> int: ...
def __sql__(self, ctx: Context) -> Context: ...

class WrappedNode(ColumnBase, Generic[_TNode]):
node: _TNode
class WrappedNode(ColumnBase, Generic[_NodeT]):
node: _NodeT
_coerce: bool
_converter: _TConvFunc | None
def __init__(self, node: _TNode): ...
_converter: _ConvFunc | None
def __init__(self, node: _NodeT): ...
def is_alias(self) -> bool: ...
def unwrap(self) -> _TNode: ...
def unwrap(self) -> _NodeT: ...

class EntityFactory:
node: Node
Expand Down Expand Up @@ -503,9 +504,9 @@ class BitwiseNegated(BitwiseMixin, WrappedNode):

class Value(ColumnBase):
value: object
converter: _TConvFunc | None
converter: _ConvFunc | None
multi: bool
def __init__(self, value: object, converter: _TConvFunc | None = ..., unpack: bool = ...): ...
def __init__(self, value: object, converter: _ConvFunc | None = ..., unpack: bool = ...): ...
def __sql__(self, ctx: Context) -> Context: ...

def AsIs(value: object) -> Value: ...
Expand Down Expand Up @@ -558,12 +559,12 @@ def Check(constraint: str) -> SQL: ...
class Function(ColumnBase):
name: str
arguments: tuple
def __init__(self, name: str, arguments: tuple, coerce: bool = ..., python_value: _TConvFunc | None = ...): ...
def __init__(self, name: str, arguments: tuple, coerce: bool = ..., python_value: _ConvFunc | None = ...): ...
def __getattr__(self, attr: str) -> Callable[..., Function]: ...
# TODO (dargueta): `where` is an educated guess
def filter(self: Self, where: Expression | None = ...) -> Self: ...
def order_by(self: Self, *ordering: Field | Expression) -> Self: ...
def python_value(self: Self, func: _TConvFunc | None = ...) -> Self: ...
def python_value(self: Self, func: _ConvFunc | None = ...) -> Self: ...
def over(
self,
partition_by: Sequence[Field] | Window | None = ...,
Expand Down Expand Up @@ -729,7 +730,7 @@ class BaseQuery(Node):
def dicts(self: Self, as_dict: bool = ...) -> Self: ...
def tuples(self: Self, as_tuple: bool = ...) -> Self: ...
def namedtuples(self: Self, as_namedtuple: bool = ...) -> Self: ...
def objects(self: Self, constructor: _TConvFunc | None = ...) -> Self: ...
def objects(self: Self, constructor: _ConvFunc | None = ...) -> Self: ...
def __sql__(self, ctx: Context) -> Context: ...
def sql(self) -> tuple[str, tuple | None]: ...
def execute(self, database: Database | None = ...) -> CursorWrapper: ...
Expand Down Expand Up @@ -909,7 +910,7 @@ class Index(Node):
class ModelIndex(Index):
def __init__(
self,
model: type[_TModel],
model: type[_ModelT],
fields: Iterable[Field | Node | str],
unique: bool = ...,
safe: bool = ...,
Expand Down Expand Up @@ -983,7 +984,7 @@ class ConnectionContext(_callable_context_manager):
def __exit__(self, exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: object) -> None: ...

class Database(_callable_context_manager):
context_class: ClassVar[type[_TContextClass]]
context_class: ClassVar[type[_ContextClassT]]
field_types: ClassVar[Mapping[str, str]]
operations: ClassVar[Mapping[str, Any]] # TODO (dargueta) Verify k/v types
param: ClassVar[str]
Expand Down Expand Up @@ -1032,7 +1033,7 @@ class Database(_callable_context_manager):
def execute_sql(self, sql: str, params: tuple | None = ..., commit: bool | _TSentinel = ...) -> __ICursor: ...
def execute(self, query: Query, commit: bool | _TSentinel = ..., **context_options: object) -> __ICursor: ...
def get_context_options(self) -> Mapping[str, object]: ...
def get_sql_context(self, **context_options: object) -> _TContextClass: ...
def get_sql_context(self, **context_options: object) -> _ContextClassT: ...
def conflict_statement(self, on_conflict: OnConflict, query: Query) -> SQL | None: ...
def conflict_update(self, oc: OnConflict, query: Query) -> NodeList: ...
def last_insert_id(self, cursor: __ICursor, query_type: int | None = ...) -> int: ...
Expand Down Expand Up @@ -1130,13 +1131,13 @@ class SqliteDatabase(Database):
@wal_autocheckpoint.setter
def wal_autocheckpoint(self, value: object) -> Any: ...
def register_aggregate(self, klass: type[__IAggregate], name: str | None = ..., num_params: int = ...): ...
def aggregate(self, name: str | None = ..., num_params: int = ...) -> Callable[[_TClass], _TClass]: ...
def aggregate(self, name: str | None = ..., num_params: int = ...) -> Callable[[_ClassT], _ClassT]: ...
def register_collation(self, fn: Callable, name: str | None = ...) -> None: ...
def collation(self, name: str | None = ...) -> Callable[[_TFunc], _TFunc]: ...
def collation(self, name: str | None = ...) -> Callable[[_FuncT], _FuncT]: ...
def register_function(self, fn: Callable, name: str | None = ..., num_params: int = ...) -> int: ...
def func(self, name: str | None = ..., num_params: int = ...) -> Callable[[_TFunc], _TFunc]: ...
def func(self, name: str | None = ..., num_params: int = ...) -> Callable[[_FuncT], _FuncT]: ...
def register_window_function(self, klass: type, name: str | None = ..., num_params: int = ...) -> None: ...
def window_function(self, name: str | None = ..., num_params: int = ...) -> Callable[[_TClass], _TClass]: ...
def window_function(self, name: str | None = ..., num_params: int = ...) -> Callable[[_ClassT], _ClassT]: ...
def register_table_function(self, klass: type[__ITableFunction], name: str | None = ...) -> None: ...
def table_function(self, name: str | None = ...) -> Callable[[type[__ITableFunction]], type[__ITableFunction]]: ...
def unregister_aggregate(self, name: str) -> None: ...
Expand Down Expand Up @@ -1298,8 +1299,8 @@ class ForeignKeyAccessor(FieldAccessor):
@overload
def __get__(self, instance: None, instance_type: type) -> Any: ...
@overload
def __get__(self, instance: _TModel, instance_type: type[_TModel]) -> ForeignKeyField: ...
def __set__(self, instance: _TModel, obj: object) -> None: ...
def __get__(self, instance: _ModelT, instance_type: type[_ModelT]) -> ForeignKeyField: ...
def __set__(self, instance: _ModelT, obj: object) -> None: ...

class NoQueryForeignKeyAccessor(ForeignKeyAccessor):
def get_rel_instance(self, instance: Model) -> Any: ...
Expand All @@ -1321,7 +1322,7 @@ class ObjectIdAccessor:
@overload
def __get__(self, instance: None, instance_type: type[Model]) -> ForeignKeyField: ...
@overload
def __get__(self, instance: _TModel, instance_type: type[_TModel] = ...) -> Any: ...
def __get__(self, instance: _ModelT, instance_type: type[_ModelT] = ...) -> Any: ...
def __set__(self, instance: Model, value: object) -> None: ...

class Field(ColumnBase):
Expand Down Expand Up @@ -1466,9 +1467,9 @@ class BigBitFieldData:

class BigBitFieldAccessor(FieldAccessor):
@overload
def __get__(self, instance: None, instance_type: type[_TModel]) -> Field: ...
def __get__(self, instance: None, instance_type: type[_ModelT]) -> Field: ...
@overload
def __get__(self, instance: _TModel, instance_type: type[_TModel]) -> BigBitFieldData: ...
def __get__(self, instance: _ModelT, instance_type: type[_ModelT]) -> BigBitFieldData: ...
def __set__(self, instance: Any, value: memoryview | bytearray | BigBitFieldData | str | bytes) -> None: ...

class BigBitField(BlobField):
Expand Down Expand Up @@ -1499,7 +1500,7 @@ class BinaryUUIDField(BlobField):
@overload
def python_value(self, value: bytearray | bytes | memoryview | uuid.UUID) -> uuid.UUID: ...

def format_date_time(value: str, formats: Iterable[str], post_process: _TConvFunc | None = ...) -> str: ...
def format_date_time(value: str, formats: Iterable[str], post_process: _ConvFunc | None = ...) -> str: ...
@overload
def simple_date_time(value: _T) -> _T: ...

Expand Down Expand Up @@ -1598,8 +1599,8 @@ class BooleanField(Field):

class BareField(Field):
# If `adapt` was omitted from the constructor or None, this attribute won't exist.
adapt: _TConvFunc | None
def __init__(self, adapt: _TConvFunc | None = ..., *args: object, **kwargs: object): ...
adapt: _ConvFunc | None
def __init__(self, adapt: _ConvFunc | None = ..., *args: object, **kwargs: object): ...
def ddl_datatype(self, ctx: Context) -> None: ...

class ForeignKeyField(Field):
Expand Down Expand Up @@ -1699,10 +1700,10 @@ class ManyToManyField(MetaField):
def get_models(self) -> list[type[Model]]: ...
def get_through_model(self) -> type[Model] | DeferredThroughModel: ...

class VirtualField(MetaField, Generic[_TField]):
field_class: type[_TField]
field_instance: _TField | None
def __init__(self, field_class: type[_TField] | None = ..., *args: object, **kwargs: object): ...
class VirtualField(MetaField, Generic[_FieldT]):
field_class: type[_FieldT]
field_instance: _FieldT | None
def __init__(self, field_class: type[_FieldT] | None = ..., *args: object, **kwargs: object): ...
def db_value(self, value: object) -> Any: ...
def python_value(self, value: object) -> Any: ...
def bind(self, model: type[Model], name: str, set_attribute: bool = ...) -> None: ...
Expand Down Expand Up @@ -1938,15 +1939,15 @@ class Model(Node, metaclass=ModelBase):
def add_index(cls, *fields: str | SQL | Index, **kwargs: object) -> None: ...

# "Provide a separate reference to a model in a query."
class ModelAlias(Node, Generic[_TModel]):
model: type[_TModel]
class ModelAlias(Node, Generic[_ModelT]):
model: type[_ModelT]
alias: str | None
def __init__(self, model: type[_TModel], alias: str | None = ...): ...
def __init__(self, model: type[_ModelT], alias: str | None = ...): ...
def __getattr__(self, attr: str) -> Any: ...
def __setattr__(self, attr: str, value: object) -> NoReturn: ...
def get_field_aliases(self) -> list[Field]: ...
def select(self, *selection: Field) -> ModelSelect: ...
def __call__(self, **kwargs) -> _TModel: ...
def __call__(self, **kwargs) -> _ModelT: ...
def __sql__(self, ctx: Context) -> Context: ...

_TModelOrTable: TypeAlias = type[Model] | ModelAlias | Table
Expand Down Expand Up @@ -1977,10 +1978,10 @@ class _ModelQueryHelper:
default_row_type: ClassVar[int]
def objects(self: Self, constructor: Callable[..., Any] | None = ...) -> Self: ...

class ModelRaw(_ModelQueryHelper, RawQuery, Generic[_TModel]):
model: type[_TModel]
def __init__(self, model: type[_TModel], sql: str, params: tuple, **kwargs: object): ...
def get(self) -> _TModel: ...
class ModelRaw(_ModelQueryHelper, RawQuery, Generic[_ModelT]):
model: type[_ModelT]
def __init__(self, model: type[_ModelT], sql: str, params: tuple, **kwargs: object): ...
def get(self) -> _ModelT: ...

class BaseModelSelect(_ModelQueryHelper):
def union_all(self, rhs: object) -> ModelCompoundSelectQuery: ...
Expand All @@ -1996,13 +1997,13 @@ class BaseModelSelect(_ModelQueryHelper):
def get(self, database: Database | None = ...) -> Any: ...
def group_by(self: Self, *columns: type[Model] | Table | Field) -> Self: ...

class ModelCompoundSelectQuery(BaseModelSelect, CompoundSelectQuery, Generic[_TModel]):
model: type[_TModel]
def __init__(self, model: type[_TModel], *args: object, **kwargs: object): ...
class ModelCompoundSelectQuery(BaseModelSelect, CompoundSelectQuery, Generic[_ModelT]):
model: type[_ModelT]
def __init__(self, model: type[_ModelT], *args: object, **kwargs: object): ...

class ModelSelect(BaseModelSelect, Select, Generic[_TModel]):
model: type[_TModel]
def __init__(self, model: type[_TModel], fields_or_models: Iterable[_TFieldOrModel], is_default: bool = ...): ...
class ModelSelect(BaseModelSelect, Select, Generic[_ModelT]):
model: type[_ModelT]
def __init__(self, model: type[_ModelT], fields_or_models: Iterable[_TFieldOrModel], is_default: bool = ...): ...
def clone(self: Self) -> Self: ...
def select(self: Self, *fields_or_models: _TFieldOrModel) -> Self: ... # type: ignore
def switch(self: Self, ctx: type[Model] | None = ...) -> Self: ...
Expand Down Expand Up @@ -2057,57 +2058,57 @@ class ManyToManyQuery(ModelSelect):
def remove(self, value: SelectQuery | type[Model] | Iterable[str]) -> int | None: ...
def clear(self) -> int: ...

class BaseModelCursorWrapper(DictCursorWrapper, Generic[_TModel]):
class BaseModelCursorWrapper(DictCursorWrapper, Generic[_ModelT]):
ncols: int
columns: list[str]
converters: list[_TConvFunc]
converters: list[_ConvFunc]
fields: list[Field]
model: type[_TModel]
model: type[_ModelT]
select: Sequence[str]
def __init__(self, cursor: __ICursor, model: type[_TModel], columns: Sequence[str] | None): ...
def __init__(self, cursor: __ICursor, model: type[_ModelT], columns: Sequence[str] | None): ...
def process_row(self, row: tuple) -> Mapping[str, object]: ... # type: ignore

class ModelDictCursorWrapper(BaseModelCursorWrapper[_TModel]):
class ModelDictCursorWrapper(BaseModelCursorWrapper[_ModelT]):
def process_row(self, row: tuple) -> dict[str, Any]: ...

class ModelTupleCursorWrapper(ModelDictCursorWrapper[_TModel]):
class ModelTupleCursorWrapper(ModelDictCursorWrapper[_ModelT]):
constructor: ClassVar[Callable[[Sequence[Any]], tuple]]
def process_row(self, row: tuple) -> tuple: ... # type: ignore

class ModelNamedTupleCursorWrapper(ModelTupleCursorWrapper[_TModel]): ...
class ModelNamedTupleCursorWrapper(ModelTupleCursorWrapper[_ModelT]): ...

class ModelObjectCursorWrapper(ModelDictCursorWrapper[_TModel]):
constructor: type[_TModel] | Callable[[Any], _TModel]
class ModelObjectCursorWrapper(ModelDictCursorWrapper[_ModelT]):
constructor: type[_ModelT] | Callable[[Any], _ModelT]
is_model: bool
# TODO (dargueta): `select` is some kind of Sequence
def __init__(
self,
cursor: __ICursor,
model: _TModel,
model: _ModelT,
select: Sequence[Incomplete],
constructor: type[_TModel] | Callable[[Any], _TModel],
constructor: type[_ModelT] | Callable[[Any], _ModelT],
): ...
def process_row(self, row: tuple) -> _TModel: ... # type: ignore
def process_row(self, row: tuple) -> _ModelT: ... # type: ignore

class ModelCursorWrapper(BaseModelCursorWrapper[_TModel]):
class ModelCursorWrapper(BaseModelCursorWrapper[_ModelT]):
# TODO (dargueta) -- Iterable[Union[Join, ...]]
from_list: Iterable[Incomplete]
# TODO (dargueta) -- Mapping[<from list type>, tuple[?, ?, Callable[..., _TModel], int?]]
joins: Mapping[Hashable, tuple[object, object, Callable[..., _TModel], int]]
key_to_constructor: dict[type[_TModel], Callable[..., _TModel]]
# TODO (dargueta) -- Mapping[<from list type>, tuple[?, ?, Callable[..., _ModelT], int?]]
joins: Mapping[Hashable, tuple[object, object, Callable[..., _ModelT], int]]
key_to_constructor: dict[type[_ModelT], Callable[..., _ModelT]]
src_is_dest: dict[type[Model], bool]
src_to_dest: list[tuple] # TODO -- tuple[<from list type>, join_type[1], join_type[0], bool, join_type[3]]
column_keys: list[Incomplete]
def __init__(
self,
cursor: __ICursor,
model: type[_TModel],
model: type[_ModelT],
select,
from_list: Iterable[object],
joins: Mapping[Hashable, tuple[object, object, Callable[..., _TModel], int]],
joins: Mapping[Hashable, tuple[object, object, Callable[..., _ModelT], int]],
): ...
def initialize(self) -> None: ...
def process_row(self, row: tuple) -> _TModel: ... # type: ignore
def process_row(self, row: tuple) -> _ModelT: ... # type: ignore

class __PrefetchQuery(NamedTuple):
query: Query # TODO (dargueta): Verify
Expand Down