Skip to content

Commit 2601660

Browse files
committed
Add failing test
1 parent e94f152 commit 2601660

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

nbs/03_dispatch.ipynb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,6 +1080,36 @@
10801080
"test_eq(a2.f(()), (1,))"
10811081
]
10821082
},
1083+
{
1084+
"cell_type": "markdown",
1085+
"metadata": {},
1086+
"source": [
1087+
"#### Using TypeDispatch With Class Methods\n",
1088+
"\n",
1089+
"You can use `TypeDispatch` when defining class methods too:"
1090+
]
1091+
},
1092+
{
1093+
"cell_type": "code",
1094+
"execution_count": null,
1095+
"metadata": {},
1096+
"outputs": [],
1097+
"source": [
1098+
"def m_nin(cls, x:(str,numbers.Integral)): return str(x)+'1'\n",
1099+
"def m_bll(cls, x:bool): cls.foo='a'\n",
1100+
"def m_num(cls, x:numbers.Number): return x*2\n",
1101+
"\n",
1102+
"t = TypeDispatch([m_nin,m_num,m_bll])\n",
1103+
"class A: f = t # set class attribute `f` equal to a TypeDispatch\n",
1104+
"\n",
1105+
"test_eq(A.f(1), '11') #dispatch to m_nin\n",
1106+
"test_eq(A.f(1.), 2.) #dispatch to m_num\n",
1107+
"test_is(A.f.owner, A)\n",
1108+
"\n",
1109+
"A.f(False) # this triggers t.m_bll to run, which sets A.foo to 'a'\n",
1110+
"test_eq(A.foo, 'a')"
1111+
]
1112+
},
10831113
{
10841114
"cell_type": "markdown",
10851115
"metadata": {},

0 commit comments

Comments
 (0)