Skip to content

Commit 6322daf

Browse files
committed
use inspect in Foundation
1 parent 0201917 commit 6322daf

File tree

3 files changed

+11
-36
lines changed

3 files changed

+11
-36
lines changed

fastcore/foundation.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from contextlib import contextmanager
1111
from copy import copy
1212
from configparser import ConfigParser
13-
import random,pickle
13+
import random,pickle,inspect
1414

1515
# Cell
1616
@contextmanager
@@ -222,7 +222,10 @@ def setattrs(self, attr, val): [setattr(o,attr,val) for o in self]
222222

223223
# Cell
224224
#hide
225-
L.__signature__ = pickle.loads(b'\x80\x03cinspect\nSignature\nq\x00(cinspect\nParameter\nq\x01X\x05\x00\x00\x00itemsq\x02cinspect\n_ParameterKind\nq\x03K\x01\x85q\x04Rq\x05\x86q\x06Rq\x07}q\x08(X\x08\x00\x00\x00_defaultq\tNX\x0b\x00\x00\x00_annotationq\ncinspect\n_empty\nq\x0bubh\x01X\x04\x00\x00\x00restq\x0ch\x03K\x02\x85q\rRq\x0e\x86q\x0fRq\x10}q\x11(h\th\x0bh\nh\x0bubh\x01X\x08\x00\x00\x00use_listq\x12h\x03K\x03\x85q\x13Rq\x14\x86q\x15Rq\x16}q\x17(h\t\x89h\nh\x0bubh\x01X\x05\x00\x00\x00matchq\x18h\x14\x86q\x19Rq\x1a}q\x1b(h\tNh\nh\x0bubtq\x1c\x85q\x1dRq\x1e}q\x1fX\x12\x00\x00\x00_return_annotationq h\x0bsb.')
225+
# Here we are fixing the signature of L. What happens is that the __call__ method on the MetaClass of L shadows the __init__
226+
# giving the wrong signature (https://stackoverflow.com/questions/49740290/call-from-metaclass-shadows-signature-of-init).
227+
def _f(items=None, *rest, use_list=False, match=None): ...
228+
L.__signature__ = inspect.signature(_f)
226229

227230
# Cell
228231
Sequence.register(L);

nbs/02_foundation.ipynb

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"from contextlib import contextmanager\n",
2323
"from copy import copy\n",
2424
"from configparser import ConfigParser\n",
25-
"import random,pickle"
25+
"import random,pickle,inspect"
2626
]
2727
},
2828
{
@@ -33,8 +33,7 @@
3333
"source": [
3434
"from fastcore.test import *\n",
3535
"from nbdev.showdoc import *\n",
36-
"from fastcore.nb_imports import *\n",
37-
"import inspect"
36+
"from fastcore.nb_imports import *"
3837
]
3938
},
4039
{
@@ -681,39 +680,14 @@
681680
"cell_type": "code",
682681
"execution_count": null,
683682
"metadata": {},
684-
"outputs": [
685-
{
686-
"data": {
687-
"text/plain": [
688-
"b'\\x80\\x03cinspect\\nSignature\\nq\\x00(cinspect\\nParameter\\nq\\x01X\\x05\\x00\\x00\\x00itemsq\\x02cinspect\\n_ParameterKind\\nq\\x03K\\x01\\x85q\\x04Rq\\x05\\x86q\\x06Rq\\x07}q\\x08(X\\x08\\x00\\x00\\x00_defaultq\\tNX\\x0b\\x00\\x00\\x00_annotationq\\ncinspect\\n_empty\\nq\\x0bubh\\x01X\\x04\\x00\\x00\\x00restq\\x0ch\\x03K\\x02\\x85q\\rRq\\x0e\\x86q\\x0fRq\\x10}q\\x11(h\\th\\x0bh\\nh\\x0bubh\\x01X\\x08\\x00\\x00\\x00use_listq\\x12h\\x03K\\x03\\x85q\\x13Rq\\x14\\x86q\\x15Rq\\x16}q\\x17(h\\t\\x89h\\nh\\x0bubh\\x01X\\x05\\x00\\x00\\x00matchq\\x18h\\x14\\x86q\\x19Rq\\x1a}q\\x1b(h\\tNh\\nh\\x0bubtq\\x1c\\x85q\\x1dRq\\x1e}q\\x1fX\\x12\\x00\\x00\\x00_return_annotationq h\\x0bsb.'"
689-
]
690-
},
691-
"execution_count": null,
692-
"metadata": {},
693-
"output_type": "execute_result"
694-
}
695-
],
683+
"outputs": [],
696684
"source": [
685+
"#export\n",
697686
"#hide\n",
698687
"# Here we are fixing the signature of L. What happens is that the __call__ method on the MetaClass of L shadows the __init__\n",
699688
"# giving the wrong signature (https://stackoverflow.com/questions/49740290/call-from-metaclass-shadows-signature-of-init).\n",
700-
"#\n",
701-
"# The solution adopted tries to not import the inspect module inside the lib code (notice that this cell is not exported)\n",
702-
"# because it's really slow, but instead relies on pickling the correct signature and loading it.\n",
703-
"\n",
704689
"def _f(items=None, *rest, use_list=False, match=None): ...\n",
705-
"pickle.dumps(inspect.signature(_f))"
706-
]
707-
},
708-
{
709-
"cell_type": "code",
710-
"execution_count": null,
711-
"metadata": {},
712-
"outputs": [],
713-
"source": [
714-
"#export\n",
715-
"#hide\n",
716-
"L.__signature__ = pickle.loads(b'\\x80\\x03cinspect\\nSignature\\nq\\x00(cinspect\\nParameter\\nq\\x01X\\x05\\x00\\x00\\x00itemsq\\x02cinspect\\n_ParameterKind\\nq\\x03K\\x01\\x85q\\x04Rq\\x05\\x86q\\x06Rq\\x07}q\\x08(X\\x08\\x00\\x00\\x00_defaultq\\tNX\\x0b\\x00\\x00\\x00_annotationq\\ncinspect\\n_empty\\nq\\x0bubh\\x01X\\x04\\x00\\x00\\x00restq\\x0ch\\x03K\\x02\\x85q\\rRq\\x0e\\x86q\\x0fRq\\x10}q\\x11(h\\th\\x0bh\\nh\\x0bubh\\x01X\\x08\\x00\\x00\\x00use_listq\\x12h\\x03K\\x03\\x85q\\x13Rq\\x14\\x86q\\x15Rq\\x16}q\\x17(h\\t\\x89h\\nh\\x0bubh\\x01X\\x05\\x00\\x00\\x00matchq\\x18h\\x14\\x86q\\x19Rq\\x1a}q\\x1b(h\\tNh\\nh\\x0bubtq\\x1c\\x85q\\x1dRq\\x1e}q\\x1fX\\x12\\x00\\x00\\x00_return_annotationq h\\x0bsb.')"
690+
"L.__signature__ = inspect.signature(_f)"
717691
]
718692
},
719693
{

nbs/04_dispatch.ipynb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@
2626
{
2727
"cell_type": "code",
2828
"execution_count": null,
29-
"metadata": {
30-
"tags": []
31-
},
29+
"metadata": {},
3230
"outputs": [],
3331
"source": [
3432
"from nbdev.showdoc import *\n",

0 commit comments

Comments
 (0)