mypy m2m iterator fix (#655)

* feat: add type hint for items in iterators for m2m relations

* chore: make flake8 happy
- rename some variables
- reorder some imports

* switch to typing.List

Co-authored-by: Benjamin Mollier <benjamin.mollier@meetap.de>
Co-authored-by: collerek <collerek@gmail.com>
This commit is contained in:
Boemjay
2022-07-22 15:24:24 +02:00
committed by GitHub
parent 991d4a2a2c
commit 54305c25d1
5 changed files with 7 additions and 8 deletions

View File

@ -1,7 +1,7 @@
import datetime import datetime
import decimal import decimal
import uuid import uuid
from enum import EnumMeta, Enum as E from enum import Enum as E, EnumMeta
from typing import Any, Optional, Set, TYPE_CHECKING, Type, TypeVar, Union, overload from typing import Any, Optional, Set, TYPE_CHECKING, Type, TypeVar, Union, overload
import pydantic import pydantic

View File

@ -79,9 +79,9 @@ class SavePrepareMixin(RelationMixin, AliasMixin):
@classmethod @classmethod
def translate_enum_columns(cls, new_kwargs: dict) -> dict: def translate_enum_columns(cls, new_kwargs: dict) -> dict:
for k, v in new_kwargs.items(): for key, value in new_kwargs.items():
if isinstance(v, Enum): if isinstance(value, Enum):
new_kwargs[k] = v.name new_kwargs[key] = value.name
return new_kwargs return new_kwargs
@classmethod @classmethod

View File

@ -22,8 +22,6 @@ from typing import (
import databases import databases
import pydantic import pydantic
import sqlalchemy import sqlalchemy
from pydantic import BaseModel from pydantic import BaseModel

View File

@ -1,4 +1,4 @@
from typing import Any, Generic, Optional, TYPE_CHECKING, Type, TypeVar from typing import Any, Generic, List, Optional, TYPE_CHECKING, Type, TypeVar
import ormar import ormar
from ormar.exceptions import NoMatch, RelationshipInstanceError from ormar.exceptions import NoMatch, RelationshipInstanceError
@ -13,7 +13,7 @@ else:
T = TypeVar("T", bound="Model") T = TypeVar("T", bound="Model")
class RelationProxy(Generic[T], list): class RelationProxy(Generic[T], List[T]):
""" """
Proxy of the Relation that is a list with special methods. Proxy of the Relation that is a list with special methods.
""" """

View File

@ -86,6 +86,7 @@ async def test_types() -> None:
# reveal_type(publisher) # model method # reveal_type(publisher) # model method
# reveal_type(publishers) # many to many # reveal_type(publishers) # many to many
# reveal_type(publishers[0]) # item in m2m list # reveal_type(publishers[0]) # item in m2m list
# reveal_type(next(p for p in publishers)) # item in m2m iterator
# # getting relation without __getattribute__ # # getting relation without __getattribute__
# reveal_type(authors) # reverse many to many # TODO: wrong # reveal_type(authors) # reverse many to many # TODO: wrong
# reveal_type(book2) # queryset get # reveal_type(book2) # queryset get