Skip to content

Commit

Permalink
Fix get_field_type & generalize extract_id_class (#657)
Browse files Browse the repository at this point in the history
* Return field.outer_type_ instead of type_ for pydantic v1

* Relax extract_id_class to support arbitrary generic types

* version: 1.22.2

---------

Co-authored-by: Roman <[email protected]>
  • Loading branch information
gsakkis and roman-right authored Sep 14, 2023
1 parent 894a473 commit 6b3a7e6
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 22 deletions.
2 changes: 1 addition & 1 deletion beanie/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from beanie.odm.views import View
from beanie.odm.union_doc import UnionDoc

__version__ = "1.22.1"
__version__ = "1.22.2"
__all__ = [
# ODM
"Document",
Expand Down
2 changes: 1 addition & 1 deletion beanie/odm/utils/pydantic.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def get_field_type(field):
if IS_PYDANTIC_V2:
return field.annotation
else:
return field.type_
return field.outer_type_


def get_model_fields(model):
Expand Down
26 changes: 9 additions & 17 deletions beanie/odm/utils/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,18 @@
else:
from typing_extensions import get_args, get_origin

from typing import Optional, Union, Type, Any
from typing import Type, Any
import inspect


def extract_id_class(annotation) -> Type[Any]:
if get_origin(annotation) is not None:
try:
annotation = next(
arg for arg in get_args(annotation) if arg is not type(None)
)
except StopIteration:
annotation = None
if inspect.isclass(annotation):
return annotation

elif get_origin(annotation) is Union:
args = get_args(annotation)
for arg in args:
if inspect.isclass(arg) and arg is not type(None):
return arg
raise ValueError("Unknown annotation: {}".format(annotation))

elif get_origin(annotation) is Optional:
arg = get_args(annotation)[0]
if inspect.isclass(arg):
return arg
else:
raise ValueError("Unknown annotation: {}".format(annotation))
else:
raise ValueError("Unknown annotation: {}".format(annotation))
raise ValueError("Unknown annotation: {}".format(annotation))
10 changes: 9 additions & 1 deletion docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@

Beanie project

## [1.22.2] - 2023-09-13
### Fix get_field_type & Generalize extract_id_class
- Author - [George Sakkis](https://github.com/gsakkis)
- PR <https://github.com/roman-right/beanie/pull/657>

[1.22.2]: https://pypi.org/project/beanie/1.22.2

## [1.22.1] - 2023-09-13
### Fix | List_Collection_Names Requires Unnecessary Privileges
### Fix | list_collection_names Requires Unnecessary Privileges
- Author - [Marina](https://github.com/marinashe)
- PR <https://github.com/roman-right/beanie/pull/681>
- Issues:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "flit_core.buildapi"

[project]
name = "beanie"
version = "1.22.1"
version = "1.22.2"
description = "Asynchronous Python ODM for MongoDB"
readme = "README.md"
requires-python = ">=3.7,<4.0"
Expand Down
7 changes: 7 additions & 0 deletions tests/odm/test_typing_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
from typing import Optional, Union

from beanie.odm.utils.typing import extract_id_class
from beanie import Document, Link


class Lock(Document):
k: int


class TestTyping:
Expand All @@ -11,3 +16,5 @@ def test_extract_id_class(self):
assert extract_id_class(Union[str, None, int]) == str
# Optional
assert extract_id_class(Optional[str]) == str
# Link
assert extract_id_class(Link[Lock]) == Lock
2 changes: 1 addition & 1 deletion tests/test_beanie.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@


def test_version():
assert __version__ == "1.22.1"
assert __version__ == "1.22.2"

0 comments on commit 6b3a7e6

Please sign in to comment.