|
201 | 201 | "outputs": [],
|
202 | 202 | "source": [
|
203 | 203 | "#export\n",
|
204 |
| - "def _lenient_issubclass(cls, types):\n", |
| 204 | + "def lenient_issubclass(cls, types):\n", |
205 | 205 | " \"If possible return whether `cls` is a subclass of `types`, otherwise return False.\"\n",
|
| 206 | + " if cls is object and types is type: return False # treat `object` as higher level than `type`\n", |
206 | 207 | " try: return isinstance(cls, types) or issubclass(cls, types)\n",
|
207 | 208 | " except: return False"
|
208 | 209 | ]
|
|
213 | 214 | "metadata": {},
|
214 | 215 | "outputs": [],
|
215 | 216 | "source": [
|
216 |
| - "# depict the need for _lenient_issubclass:\n", |
| 217 | + "# depict the need for lenient_issubclass:\n", |
217 | 218 | "t = lambda: issubclass(typing.Collection, object)\n",
|
218 | 219 | "test_fail(t, contains='issubclass() arg 1 must be a class')"
|
219 | 220 | ]
|
|
224 | 225 | "metadata": {},
|
225 | 226 | "outputs": [],
|
226 | 227 | "source": [
|
227 |
| - "assert not _lenient_issubclass(typing.Collection, list)\n", |
228 |
| - "assert _lenient_issubclass(list, typing.Collection)\n", |
229 |
| - "assert _lenient_issubclass(typing.Collection, object)\n", |
230 |
| - "assert _lenient_issubclass(typing.List, typing.Collection)\n", |
231 |
| - "assert not _lenient_issubclass(typing.Collection, typing.List)" |
| 228 | + "assert not lenient_issubclass(typing.Collection, list)\n", |
| 229 | + "assert lenient_issubclass(list, typing.Collection)\n", |
| 230 | + "assert lenient_issubclass(typing.Collection, object)\n", |
| 231 | + "assert lenient_issubclass(typing.List, typing.Collection)\n", |
| 232 | + "assert not lenient_issubclass(typing.Collection, typing.List)" |
232 | 233 | ]
|
233 | 234 | },
|
234 | 235 | {
|
|
265 | 266 | "outputs": [],
|
266 | 267 | "source": [
|
267 | 268 | "td = {int:1, numbers.Number:2, numbers.Integral:3}\n",
|
268 |
| - "test_eq(sorted_topologically(td, cmp=_lenient_issubclass), [int, numbers.Integral, numbers.Number])" |
| 269 | + "test_eq(sorted_topologically(td, cmp=lenient_issubclass), [int, numbers.Integral, numbers.Number])" |
269 | 270 | ]
|
270 | 271 | },
|
271 | 272 | {
|
|
275 | 276 | "outputs": [],
|
276 | 277 | "source": [
|
277 | 278 | "td = [numbers.Integral, tuple, list, int, dict]\n",
|
278 |
| - "td = sorted_topologically(td, cmp=_lenient_issubclass)\n", |
| 279 | + "td = sorted_topologically(td, cmp=lenient_issubclass)\n", |
279 | 280 | "assert td.index(int) < td.index(numbers.Integral)"
|
280 | 281 | ]
|
281 | 282 | },
|
|
406 | 407 | " def __init__(self): self.d,self.cache = {},{}\n",
|
407 | 408 | "\n",
|
408 | 409 | " def _reset(self):\n",
|
409 |
| - " self.d = {k:self.d[k] for k in sorted_topologically(self.d, cmp=_lenient_issubclass)}\n", |
| 410 | + " self.d = {k:self.d[k] for k in sorted_topologically(self.d, cmp=lenient_issubclass)}\n", |
410 | 411 | " self.cache = {}\n",
|
411 | 412 | "\n",
|
412 | 413 | " def add(self, t, f):\n",
|
|
418 | 419 | " def all_matches(self, k):\n",
|
419 | 420 | " \"Find first matching type that is a super-class of `k`\"\n",
|
420 | 421 | " if k not in self.cache:\n",
|
421 |
| - " types = [f for f in self.d if k==f or (isinstance(k,type) and issubclass(k,f))]\n", |
| 422 | + " types = [f for f in self.d if lenient_issubclass(k,f)]\n", |
422 | 423 | " self.cache[k] = [self.d[o] for o in types]\n",
|
423 | 424 | " return self.cache[k]\n",
|
424 | 425 | "\n",
|
|
502 | 503 | " returns_none=\"Returns `None` if return type annotation is `None` or `NoneType`.\")"
|
503 | 504 | ]
|
504 | 505 | },
|
| 506 | + { |
| 507 | + "cell_type": "code", |
| 508 | + "execution_count": null, |
| 509 | + "metadata": {}, |
| 510 | + "outputs": [ |
| 511 | + { |
| 512 | + "data": { |
| 513 | + "text/plain": [ |
| 514 | + "<function __main__.a(z: type)>" |
| 515 | + ] |
| 516 | + }, |
| 517 | + "execution_count": null, |
| 518 | + "metadata": {}, |
| 519 | + "output_type": "execute_result" |
| 520 | + } |
| 521 | + ], |
| 522 | + "source": [ |
| 523 | + "def a(z:type): return 'a'\n", |
| 524 | + "def b(z:object): return 'b'\n", |
| 525 | + "\n", |
| 526 | + "t = TypeDispatch([a,b])\n", |
| 527 | + "\n", |
| 528 | + "t[TypeDispatch]" |
| 529 | + ] |
| 530 | + }, |
505 | 531 | {
|
506 | 532 | "cell_type": "markdown",
|
507 | 533 | "metadata": {},
|
|
0 commit comments