Skip to content

Commit

Permalink
Add unittest
Browse files Browse the repository at this point in the history
  • Loading branch information
koxudaxi committed Apr 15, 2021
1 parent 93fb36d commit 00b58b8
Show file tree
Hide file tree
Showing 4 changed files with 197 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/com/koxudaxi/pydantic/PydanticDataclassTypeProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class PydanticDataclassTypeProvider : PyTypeProviderBase() {
private fun getDataclassCallableType(
referenceTarget: PsiElement,
context: TypeEvalContext,
callSite: PyCallExpression? = null,
callSite: PyCallExpression?,
): PyCallableType? {
return pyDataclassTypeProvider.getReferenceType(
referenceTarget,
Expand Down
68 changes: 66 additions & 2 deletions testData/mock/pydanticv18/dataclasses.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,66 @@
def dataclass():
pass
from typing import TYPE_CHECKING, Any, Callable, Dict, Optional, Type, TypeVar, Union, overload

if TYPE_CHECKING:
from .main import BaseConfig, BaseModel # noqa: F401

DataclassT = TypeVar('DataclassT', bound='Dataclass')

class Dataclass:
__pydantic_model__: Type[BaseModel]
__initialised__: bool
__post_init_original__: Optional[Callable[..., None]]

def __init__(self, *args: Any, **kwargs: Any) -> None:
pass

def __call__(self: 'DataclassT', *args: Any, **kwargs: Any) -> 'DataclassT':
pass


@overload
def dataclass(
*,
init: bool = True,
repr: bool = True,
eq: bool = True,
order: bool = False,
unsafe_hash: bool = False,
frozen: bool = False,
config: Type[Any] = None,
) -> Callable[[Type[Any]], Type['Dataclass']]:
...


@overload
def dataclass(
_cls: Type[Any],
*,
init: bool = True,
repr: bool = True,
eq: bool = True,
order: bool = False,
unsafe_hash: bool = False,
frozen: bool = False,
config: Type[Any] = None,
) -> Type['Dataclass']:
...


def dataclass(
_cls: Optional[Type[Any]] = None,
*,
init: bool = True,
repr: bool = True,
eq: bool = True,
order: bool = False,
unsafe_hash: bool = False,
frozen: bool = False,
config: Type[Any] = None,
) -> Union[Callable[[Type[Any]], Type['Dataclass']], Type['Dataclass']]:

def wrap(cls: Type[Any]) -> Type['Dataclass']:
pass
if _cls is None:
return wrap

return wrap(_cls)
127 changes: 127 additions & 0 deletions testData/typeinspectionv18/dataclass.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
from typing import *

import pydantic

@pydantic.dataclasses.dataclass
class MyDataclass:
a: str
b: int

def func_a(self) -> str:
return self.a

def func_b(self) -> int:
return self.b

def func_c(self) -> int:
return <warning descr="Expected type 'int', got 'str' instead">self.a</warning>

def func_d(self) -> str:
return <warning descr="Expected type 'str', got 'int' instead">self.b</warning>

@pydantic.dataclasses.dataclass
class ChildDataclass(MyDataclass):
c: str
d: int

MyDataclass(a='apple', b=1)
MyDataclass(<warning descr="Expected type 'str', got 'int' instead">a=2</warning>, <warning descr="Expected type 'int', got 'str' instead">b='orange'</warning>)

ChildDataclass(a='apple', b=1, c='berry', d=3)
ChildDataclass(<warning descr="Expected type 'str', got 'int' instead">a=2</warning>, <warning descr="Expected type 'int', got 'str' instead">b='orange'</warning>, <warning descr="Expected type 'str', got 'int' instead">c=4</warning>, <warning descr="Expected type 'int', got 'str' instead">d='cherry'</warning>)


a: MyDataclass = MyDataclass()
b: Type[MyDataclass] = MyDataclass

c: MyDataclass = <warning descr="Expected type 'MyDataclass', got 'Type[MyDataclass]' instead">MyDataclass</warning>
d: Type[MyDataclass] = <warning descr="Expected type 'Type[MyDataclass]', got 'MyDataclass' instead">MyDataclass()</warning>

aa: Union[str, MyDataclass] = MyDataclass()
bb: Union[str, Type[MyDataclass]] = MyDataclass

cc: Union[str, MyDataclass] = <warning descr="Expected type 'Union[str, MyDataclass]', got 'Type[MyDataclass]' instead">MyDataclass</warning>
dd: Union[str, Type[MyDataclass]] = <warning descr="Expected type 'Union[str, Type[MyDataclass]]', got 'MyDataclass' instead">MyDataclass()</warning>

aaa: ChildDataclass = ChildDataclass()
bbb: Type[ChildDataclass] = ChildDataclass

ccc: ChildDataclass = <warning descr="Expected type 'ChildDataclass', got 'Type[ChildDataclass]' instead">ChildDataclass</warning>
ddd: Type[ChildDataclass] = <warning descr="Expected type 'Type[ChildDataclass]', got 'ChildDataclass' instead">ChildDataclass()</warning>


e: str = MyDataclass(a='apple', b=1).a
f: int = MyDataclass(a='apple', b=1).b

g: int = <warning descr="Expected type 'int', got 'str' instead">MyDataclass(a='apple', b=1).a</warning>
h: str = <warning descr="Expected type 'str', got 'int' instead">MyDataclass(a='apple', b=1).b</warning>


ee: str = ChildDataclass(a='apple', b=1, c='orange', d=2).a
ff: int = ChildDataclass(a='apple', b=1, c='orange', d=2).d

gg: int = <warning descr="Expected type 'int', got 'str' instead">ChildDataclass(a='apple', b=1, c='orange', d=2).a</warning>
hh: str = <warning descr="Expected type 'str', got 'int' instead">ChildDataclass(a='apple', b=1, c='orange', d=2).d</warning>

i: MyDataclass = MyDataclass(a='apple', b=1)
j: str = i.a
k: int = i.b

l: int = <warning descr="Expected type 'int', got 'str' instead">i.a</warning>
m: str = <warning descr="Expected type 'str', got 'int' instead">i.b</warning>


ii: ChildDataclass = ChildDataclass(a='apple', b=1, c='orange', d=2)
jj: str = i.a
kk: int = i.d

ll: int = <warning descr="Expected type 'int', got 'str' instead">ii.a</warning>
mm: str = <warning descr="Expected type 'str', got 'int' instead">ii.d</warning>

def my_fn_1() -> MyDataclass:
return MyDataclass()

def my_fn_2() -> Type[MyDataclass]:
return MyDataclass

def my_fn_3() -> MyDataclass:
return <warning descr="Expected type 'MyDataclass', got 'Type[MyDataclass]' instead">MyDataclass</warning>

def my_fn_4() -> Type[MyDataclass]:
return <warning descr="Expected type 'Type[MyDataclass]', got 'MyDataclass' instead">MyDataclass()</warning>

def my_fn_5() -> Union[str, MyDataclass]:
return MyDataclass()

def my_fn_6() -> Type[str, MyDataclass]:
return MyDataclass

def my_fn_7() -> Union[str, MyDataclass]:
return <warning descr="Expected type 'Union[str, MyDataclass]', got 'Type[MyDataclass]' instead">MyDataclass</warning>

def my_fn_8() -> Union[str, Type[MyDataclass]]:
return <warning descr="Expected type 'Union[str, Type[MyDataclass]]', got 'MyDataclass' instead">MyDataclass()</warning>

def my_fn_9() -> ChildDataclass:
return ChildDataclass()

def my_fn_10() -> Type[ChildDataclass]:
return ChildDataclass

def my_fn_11() -> ChildDataclass:
return <warning descr="Expected type 'ChildDataclass', got 'Type[ChildDataclass]' instead">ChildDataclass</warning>

def my_fn_12() -> Type[ChildDataclass]:
return <warning descr="Expected type 'Type[ChildDataclass]', got 'ChildDataclass' instead">ChildDataclass()</warning>

def my_fn_13() -> Union[str, ChildDataclass]:
return ChildDataclass()

def my_fn_14() -> Type[str, ChildDataclass]:
return ChildDataclass

def my_fn_7() -> Union[str, ChildDataclass]:
return <warning descr="Expected type 'Union[str, ChildDataclass]', got 'Type[ChildDataclass]' instead">ChildDataclass</warning>

def my_fn_8() -> Union[str, Type[ChildDataclass]]:
return <warning descr="Expected type 'Union[str, Type[ChildDataclass]]', got 'ChildDataclass' instead">ChildDataclass()</warning>
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ open class PydanticTypeInspectionV18Test : PydanticInspectionBase("v18") {
fun testDynamicModel() {
doTest()
}
fun testDataclass() {
doTest()
}
}

0 comments on commit 00b58b8

Please sign in to comment.