remove forwardref stub, change checks to __class__ check

This commit is contained in:
collerek
2021-01-12 16:01:24 +01:00
parent 3cf846ee23
commit aeb56363be
4 changed files with 24 additions and 37 deletions

View File

@ -3,13 +3,12 @@ from dataclasses import dataclass
from typing import Any, List, Optional, TYPE_CHECKING, Tuple, Type, Union from typing import Any, List, Optional, TYPE_CHECKING, Tuple, Type, Union
from pydantic import BaseModel, create_model from pydantic import BaseModel, create_model
from pydantic.typing import evaluate_forwardref from pydantic.typing import ForwardRef, evaluate_forwardref
from sqlalchemy import UniqueConstraint from sqlalchemy import UniqueConstraint
import ormar # noqa I101 import ormar # noqa I101
from ormar.exceptions import RelationshipInstanceError from ormar.exceptions import RelationshipInstanceError
from ormar.fields.base import BaseField from ormar.fields.base import BaseField
from ormar.protocols.forward_ref import ForwardRef
if TYPE_CHECKING: # pragma no cover if TYPE_CHECKING: # pragma no cover
from ormar.models import Model, NewBaseModel from ormar.models import Model, NewBaseModel
@ -170,13 +169,16 @@ def ForeignKey( # noqa CFQ002
owner = kwargs.pop("owner", None) owner = kwargs.pop("owner", None)
self_reference = kwargs.pop("self_reference", False) self_reference = kwargs.pop("self_reference", False)
if isinstance(to, ForwardRef): if to.__class__ == ForwardRef:
__type__ = to if not nullable else Optional[to] __type__ = to if not nullable else Optional[to]
constraints: List = [] constraints: List = []
column_type = None column_type = None
else: else:
__type__, constraints, column_type = populate_fk_params_based_on_to_model( __type__, constraints, column_type = populate_fk_params_based_on_to_model(
to=to, nullable=nullable, ondelete=ondelete, onupdate=onupdate to=to, # type: ignore
nullable=nullable,
ondelete=ondelete,
onupdate=onupdate,
) )
namespace = dict( namespace = dict(
@ -250,8 +252,12 @@ class ForeignKeyField(BaseField):
:return: None :return: None
:rtype: None :rtype: None
""" """
if isinstance(cls.to, ForwardRef): if cls.to.__class__ == ForwardRef:
cls.to = evaluate_forwardref(cls.to, globalns, localns or None) cls.to = evaluate_forwardref(
cls.to, # type: ignore
globalns,
localns or None,
)
( (
cls.__type__, cls.__type__,
cls.constraints, cls.constraints,
@ -398,7 +404,7 @@ class ForeignKeyField(BaseField):
:return: result of the check :return: result of the check
:rtype: bool :rtype: bool
""" """
return isinstance(cls.to, ForwardRef) return cls.to.__class__ == ForwardRef
@classmethod @classmethod
def expand_relationship( def expand_relationship(

View File

@ -1,10 +1,9 @@
from typing import Any, List, Optional, TYPE_CHECKING, Tuple, Type, Union from typing import Any, List, Optional, TYPE_CHECKING, Tuple, Type, Union
from pydantic.typing import evaluate_forwardref from pydantic.typing import ForwardRef, evaluate_forwardref
import ormar # noqa: I100 import ormar # noqa: I100
from ormar.fields import BaseField from ormar.fields import BaseField
from ormar.fields.foreign_key import ForeignKeyField from ormar.fields.foreign_key import ForeignKeyField
from ormar.protocols.forward_ref import ForwardRef
if TYPE_CHECKING: # pragma no cover if TYPE_CHECKING: # pragma no cover
from ormar.models import Model from ormar.models import Model
@ -73,7 +72,7 @@ def ManyToMany(
owner = kwargs.pop("owner", None) owner = kwargs.pop("owner", None)
self_reference = kwargs.pop("self_reference", False) self_reference = kwargs.pop("self_reference", False)
if isinstance(to, ForwardRef): if to.__class__ == ForwardRef:
__type__ = to if not nullable else Optional[to] __type__ = to if not nullable else Optional[to]
column_type = None column_type = None
else: else:
@ -151,7 +150,7 @@ class ManyToManyField(ForeignKeyField, ormar.QuerySetProtocol, ormar.RelationPro
:return: result of the check :return: result of the check
:rtype: bool :rtype: bool
""" """
return isinstance(cls.to, ForwardRef) or isinstance(cls.through, ForwardRef) return cls.to.__class__ == ForwardRef or cls.through.__class__ == ForwardRef
@classmethod @classmethod
def evaluate_forward_ref(cls, globalns: Any, localns: Any) -> None: def evaluate_forward_ref(cls, globalns: Any, localns: Any) -> None:
@ -165,8 +164,12 @@ class ManyToManyField(ForeignKeyField, ormar.QuerySetProtocol, ormar.RelationPro
:return: None :return: None
:rtype: None :rtype: None
""" """
if isinstance(cls.to, ForwardRef) or isinstance(cls.through, ForwardRef): if cls.to.__class__ == ForwardRef or cls.through.__class__ == ForwardRef:
cls.to = evaluate_forwardref(cls.to, globalns, localns or None) cls.to = evaluate_forwardref(
cls.to, # type: ignore
globalns,
localns or None,
)
(cls.__type__, cls.column_type,) = populate_m2m_params_based_on_to_model( (cls.__type__, cls.column_type,) = populate_m2m_params_based_on_to_model(
to=cls.to, nullable=cls.nullable, to=cls.to, nullable=cls.nullable,
) )

View File

@ -4,7 +4,7 @@ from typing import Dict, List, Optional, TYPE_CHECKING, Tuple, Type
import ormar # noqa: I100 import ormar # noqa: I100
from ormar.fields.foreign_key import ForeignKeyField from ormar.fields.foreign_key import ForeignKeyField
from ormar.models.helpers.pydantic import populate_pydantic_default_values from ormar.models.helpers.pydantic import populate_pydantic_default_values
from ormar.protocols.forward_ref import ForwardRef from pydantic.typing import ForwardRef
if TYPE_CHECKING: # pragma no cover if TYPE_CHECKING: # pragma no cover
from ormar import Model from ormar import Model
@ -22,7 +22,7 @@ def is_field_an_forward_ref(field: Type["BaseField"]) -> bool:
:rtype: bool :rtype: bool
""" """
return issubclass(field, ForeignKeyField) and ( return issubclass(field, ForeignKeyField) and (
isinstance(field.to, ForwardRef) or isinstance(field.through, ForwardRef) field.to.__class__ == ForwardRef or field.through.__class__ == ForwardRef
) )

View File

@ -1,22 +0,0 @@
import sys
from typing import Any, TYPE_CHECKING
if sys.version_info < (3, 7): # pragma: no cover
if TYPE_CHECKING:
class ForwardRef:
_gorg = None
def __init__(self, args: Any) -> None:
pass
def _eval_type(self, globalns: Any, localns: Any) -> Any:
pass
else:
from typing import _ForwardRef as ForwardRef
else:
from typing import ForwardRef
ForwardRef = ForwardRef