%global _empty_manifest_terminate_build 0 Name: python-mongo-types Version: 0.15.1 Release: 1 Summary: Type stubs for mongoengine w/ basic support for bson and pymongo License: Apache-2.0 URL: https://github.com/sbdchd/mongo-types Source0: https://mirrors.nju.edu.cn/pypi/web/packages/91/40/e79a6caceb8140326963b4ea8829dd8df0434dc815312430b4518bf80c26/mongo-types-0.15.1.tar.gz BuildArch: noarch %description # mongo-types [![PyPI](https://img.shields.io/pypi/v/mongo-types.svg)](https://pypi.org/project/mongo-types/) Type stubs for [`mongoengine`][0] with some basic type stubs for [`pymongo`][1] and [`bson`][2]. Allows for autocomplete and static typing. ## install ```shell pip install mongo-types ``` Monkey patch mongoengine's `QuerySet` so we can type it with a generic argument at runtime: ```python import types from mongoengine.queryset.queryset import QuerySet def no_op(self, x): return self QuerySet.__class_getitem__ = types.MethodType(no_op, QuerySet) ``` ## usage After installing and monkey patching, the types should work for the most part, but you'll probably need to change how you write some things. ### getting `objects` to work By default, the base document is typed to not have an `objects` property so that each document can type it properly. Here's a helper class that's useful for simple cases which don't modify the `QuerySet`. ```python from typing import Generic, Type, TypeVar from mongoengine import QuerySet, Document U = TypeVar("U", bound=Document) class QuerySetManager(Generic[U]): def __get__(self, instance: object, cls: Type[U]) -> QuerySet[U]: return QuerySet(cls, cls._get_collection()) class Page(Document): meta = { "collection": "pages", } objects = QuerySetManager["Page"]() organization = fields.StringField() ``` ### replacing usages of `queryset_class` before: ```python from typing import Type from mongoengine import QuerySet, Document class PostQuerySet(QuerySet): def for_org(self, *, org: str) -> QuerySet: return self.filter(organization=org) def exists(self) -> bool: return self.count() > 0 class Post(Document): meta = { "collection": "posts", "queryset_class": SMSLogQuerySet, } organization = fields.StringField() # --snip-- ``` after: ```python from typing import Type from mongoengine import QuerySet, Document class PostQuerySet(QuerySet["Post"]): def for_org(self, *, org: str) -> QuerySet["Post"]: return self.filter(organization=org) def exists(self) -> bool: return self.count() > 0 class QuerySetManager: def __get__(self, instance: object, cls: Type[Post]) -> PostQuerySet: return PostQuerySet(cls, cls._get_collection()) class Post(Document): meta = { "collection": "posts", } objects = QuerySetManager() organization = fields.StringField() # --snip-- ``` ### replicating `@queryset_manager` behavior before: ```python from mongoengine import Document, QuerySet, queryset_manager, fields class UserQuerySet(QuerySet): def for_org(self, *, org: str) -> QuerySet: return self.filter(organization=org) class User(Document): meta = { "collection": "users", "queryset_class": UserQuerySet, } is_active = fields.BooleanField() # --snip-- @queryset_manager def objects(self, queryset: QuerySet) -> QuerySet: return queryset.filter(is_active=True) @queryset_manager def all_objects(self, queryset: QuerySet) -> QuerySet: return queryset maybe_user = User.all_objects.first() ``` after: ```python from __future__ import annotations from typing import Type from mongoengine import QuerySet, Document class UserQuerySet(QuerySet["User"]): def for_org(self, *, org: str) -> UserQuerySet: return self.filter(organization=org) class QuerySetManager: def __get__(self, instance: object, cls: Type[User]) -> UserQuerySet: return UserQuerySet(cls, cls._get_collection()).filter(is_active=True) class User(Document): meta = { "collection": "users", } is_active = fields.BooleanField() # --snip-- objects = QuerySetManager() @classmethod def all_objects(cls) -> UserQuerySet: return UserQuerySet(cls, cls._get_collection()) maybe_user = User.all_objects().first() ``` ### fixing "Model" has no attribute "id" Mongoengine will define an `id` field for you automatically. Mongo-types require you specify your `id` explicitly so that the types can be more strict. ```python class User(Document): meta = { "collection": "users", } # becomes class User(Document): meta = { "collection": "users", } id = fields.StringField(db_field="_id", primary_key=True, default=default_id) # or if you prefer ObjectIds class User(Document): meta = { "collection": "users", } id = fields.ObjectIdField(db_field="_id", primary_key=True, default=ObjectId) ``` ## dev ```shell poetry install # run formatting, linting, and typechecking s/lint # build poetry build -f wheel # build and publish poetry publish --build ``` [0]: https://github.com/MongoEngine/mongoengine [1]: https://github.com/mongodb/mongo-python-driver/tree/master/pymongo [2]: https://github.com/mongodb/mongo-python-driver/tree/master/bson ## related - - - - %package -n python3-mongo-types Summary: Type stubs for mongoengine w/ basic support for bson and pymongo Provides: python-mongo-types BuildRequires: python3-devel BuildRequires: python3-setuptools BuildRequires: python3-pip %description -n python3-mongo-types # mongo-types [![PyPI](https://img.shields.io/pypi/v/mongo-types.svg)](https://pypi.org/project/mongo-types/) Type stubs for [`mongoengine`][0] with some basic type stubs for [`pymongo`][1] and [`bson`][2]. Allows for autocomplete and static typing. ## install ```shell pip install mongo-types ``` Monkey patch mongoengine's `QuerySet` so we can type it with a generic argument at runtime: ```python import types from mongoengine.queryset.queryset import QuerySet def no_op(self, x): return self QuerySet.__class_getitem__ = types.MethodType(no_op, QuerySet) ``` ## usage After installing and monkey patching, the types should work for the most part, but you'll probably need to change how you write some things. ### getting `objects` to work By default, the base document is typed to not have an `objects` property so that each document can type it properly. Here's a helper class that's useful for simple cases which don't modify the `QuerySet`. ```python from typing import Generic, Type, TypeVar from mongoengine import QuerySet, Document U = TypeVar("U", bound=Document) class QuerySetManager(Generic[U]): def __get__(self, instance: object, cls: Type[U]) -> QuerySet[U]: return QuerySet(cls, cls._get_collection()) class Page(Document): meta = { "collection": "pages", } objects = QuerySetManager["Page"]() organization = fields.StringField() ``` ### replacing usages of `queryset_class` before: ```python from typing import Type from mongoengine import QuerySet, Document class PostQuerySet(QuerySet): def for_org(self, *, org: str) -> QuerySet: return self.filter(organization=org) def exists(self) -> bool: return self.count() > 0 class Post(Document): meta = { "collection": "posts", "queryset_class": SMSLogQuerySet, } organization = fields.StringField() # --snip-- ``` after: ```python from typing import Type from mongoengine import QuerySet, Document class PostQuerySet(QuerySet["Post"]): def for_org(self, *, org: str) -> QuerySet["Post"]: return self.filter(organization=org) def exists(self) -> bool: return self.count() > 0 class QuerySetManager: def __get__(self, instance: object, cls: Type[Post]) -> PostQuerySet: return PostQuerySet(cls, cls._get_collection()) class Post(Document): meta = { "collection": "posts", } objects = QuerySetManager() organization = fields.StringField() # --snip-- ``` ### replicating `@queryset_manager` behavior before: ```python from mongoengine import Document, QuerySet, queryset_manager, fields class UserQuerySet(QuerySet): def for_org(self, *, org: str) -> QuerySet: return self.filter(organization=org) class User(Document): meta = { "collection": "users", "queryset_class": UserQuerySet, } is_active = fields.BooleanField() # --snip-- @queryset_manager def objects(self, queryset: QuerySet) -> QuerySet: return queryset.filter(is_active=True) @queryset_manager def all_objects(self, queryset: QuerySet) -> QuerySet: return queryset maybe_user = User.all_objects.first() ``` after: ```python from __future__ import annotations from typing import Type from mongoengine import QuerySet, Document class UserQuerySet(QuerySet["User"]): def for_org(self, *, org: str) -> UserQuerySet: return self.filter(organization=org) class QuerySetManager: def __get__(self, instance: object, cls: Type[User]) -> UserQuerySet: return UserQuerySet(cls, cls._get_collection()).filter(is_active=True) class User(Document): meta = { "collection": "users", } is_active = fields.BooleanField() # --snip-- objects = QuerySetManager() @classmethod def all_objects(cls) -> UserQuerySet: return UserQuerySet(cls, cls._get_collection()) maybe_user = User.all_objects().first() ``` ### fixing "Model" has no attribute "id" Mongoengine will define an `id` field for you automatically. Mongo-types require you specify your `id` explicitly so that the types can be more strict. ```python class User(Document): meta = { "collection": "users", } # becomes class User(Document): meta = { "collection": "users", } id = fields.StringField(db_field="_id", primary_key=True, default=default_id) # or if you prefer ObjectIds class User(Document): meta = { "collection": "users", } id = fields.ObjectIdField(db_field="_id", primary_key=True, default=ObjectId) ``` ## dev ```shell poetry install # run formatting, linting, and typechecking s/lint # build poetry build -f wheel # build and publish poetry publish --build ``` [0]: https://github.com/MongoEngine/mongoengine [1]: https://github.com/mongodb/mongo-python-driver/tree/master/pymongo [2]: https://github.com/mongodb/mongo-python-driver/tree/master/bson ## related - - - - %package help Summary: Development documents and examples for mongo-types Provides: python3-mongo-types-doc %description help # mongo-types [![PyPI](https://img.shields.io/pypi/v/mongo-types.svg)](https://pypi.org/project/mongo-types/) Type stubs for [`mongoengine`][0] with some basic type stubs for [`pymongo`][1] and [`bson`][2]. Allows for autocomplete and static typing. ## install ```shell pip install mongo-types ``` Monkey patch mongoengine's `QuerySet` so we can type it with a generic argument at runtime: ```python import types from mongoengine.queryset.queryset import QuerySet def no_op(self, x): return self QuerySet.__class_getitem__ = types.MethodType(no_op, QuerySet) ``` ## usage After installing and monkey patching, the types should work for the most part, but you'll probably need to change how you write some things. ### getting `objects` to work By default, the base document is typed to not have an `objects` property so that each document can type it properly. Here's a helper class that's useful for simple cases which don't modify the `QuerySet`. ```python from typing import Generic, Type, TypeVar from mongoengine import QuerySet, Document U = TypeVar("U", bound=Document) class QuerySetManager(Generic[U]): def __get__(self, instance: object, cls: Type[U]) -> QuerySet[U]: return QuerySet(cls, cls._get_collection()) class Page(Document): meta = { "collection": "pages", } objects = QuerySetManager["Page"]() organization = fields.StringField() ``` ### replacing usages of `queryset_class` before: ```python from typing import Type from mongoengine import QuerySet, Document class PostQuerySet(QuerySet): def for_org(self, *, org: str) -> QuerySet: return self.filter(organization=org) def exists(self) -> bool: return self.count() > 0 class Post(Document): meta = { "collection": "posts", "queryset_class": SMSLogQuerySet, } organization = fields.StringField() # --snip-- ``` after: ```python from typing import Type from mongoengine import QuerySet, Document class PostQuerySet(QuerySet["Post"]): def for_org(self, *, org: str) -> QuerySet["Post"]: return self.filter(organization=org) def exists(self) -> bool: return self.count() > 0 class QuerySetManager: def __get__(self, instance: object, cls: Type[Post]) -> PostQuerySet: return PostQuerySet(cls, cls._get_collection()) class Post(Document): meta = { "collection": "posts", } objects = QuerySetManager() organization = fields.StringField() # --snip-- ``` ### replicating `@queryset_manager` behavior before: ```python from mongoengine import Document, QuerySet, queryset_manager, fields class UserQuerySet(QuerySet): def for_org(self, *, org: str) -> QuerySet: return self.filter(organization=org) class User(Document): meta = { "collection": "users", "queryset_class": UserQuerySet, } is_active = fields.BooleanField() # --snip-- @queryset_manager def objects(self, queryset: QuerySet) -> QuerySet: return queryset.filter(is_active=True) @queryset_manager def all_objects(self, queryset: QuerySet) -> QuerySet: return queryset maybe_user = User.all_objects.first() ``` after: ```python from __future__ import annotations from typing import Type from mongoengine import QuerySet, Document class UserQuerySet(QuerySet["User"]): def for_org(self, *, org: str) -> UserQuerySet: return self.filter(organization=org) class QuerySetManager: def __get__(self, instance: object, cls: Type[User]) -> UserQuerySet: return UserQuerySet(cls, cls._get_collection()).filter(is_active=True) class User(Document): meta = { "collection": "users", } is_active = fields.BooleanField() # --snip-- objects = QuerySetManager() @classmethod def all_objects(cls) -> UserQuerySet: return UserQuerySet(cls, cls._get_collection()) maybe_user = User.all_objects().first() ``` ### fixing "Model" has no attribute "id" Mongoengine will define an `id` field for you automatically. Mongo-types require you specify your `id` explicitly so that the types can be more strict. ```python class User(Document): meta = { "collection": "users", } # becomes class User(Document): meta = { "collection": "users", } id = fields.StringField(db_field="_id", primary_key=True, default=default_id) # or if you prefer ObjectIds class User(Document): meta = { "collection": "users", } id = fields.ObjectIdField(db_field="_id", primary_key=True, default=ObjectId) ``` ## dev ```shell poetry install # run formatting, linting, and typechecking s/lint # build poetry build -f wheel # build and publish poetry publish --build ``` [0]: https://github.com/MongoEngine/mongoengine [1]: https://github.com/mongodb/mongo-python-driver/tree/master/pymongo [2]: https://github.com/mongodb/mongo-python-driver/tree/master/bson ## related - - - - %prep %autosetup -n mongo-types-0.15.1 %build %py3_build %install %py3_install install -d -m755 %{buildroot}/%{_pkgdocdir} if [ -d doc ]; then cp -arf doc %{buildroot}/%{_pkgdocdir}; fi if [ -d docs ]; then cp -arf docs %{buildroot}/%{_pkgdocdir}; fi if [ -d example ]; then cp -arf example %{buildroot}/%{_pkgdocdir}; fi if [ -d examples ]; then cp -arf examples %{buildroot}/%{_pkgdocdir}; fi pushd %{buildroot} if [ -d usr/lib ]; then find usr/lib -type f -printf "/%h/%f\n" >> filelist.lst fi if [ -d usr/lib64 ]; then find usr/lib64 -type f -printf "/%h/%f\n" >> filelist.lst fi if [ -d usr/bin ]; then find usr/bin -type f -printf "/%h/%f\n" >> filelist.lst fi if [ -d usr/sbin ]; then find usr/sbin -type f -printf "/%h/%f\n" >> filelist.lst fi touch doclist.lst if [ -d usr/share/man ]; then find usr/share/man -type f -printf "/%h/%f.gz\n" >> doclist.lst fi popd mv %{buildroot}/filelist.lst . mv %{buildroot}/doclist.lst . %files -n python3-mongo-types -f filelist.lst %dir %{python3_sitelib}/* %files help -f doclist.lst %{_docdir}/* %changelog * Fri May 05 2023 Python_Bot - 0.15.1-1 - Package Spec generated