Skip to content

Commit 7c3fa46

Browse files
committed
fixes #145
1 parent 9336a59 commit 7c3fa46

File tree

4 files changed

+56
-10
lines changed

4 files changed

+56
-10
lines changed

fastcore/_nbdev.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@
179179
"PrePostInitMeta": "06_meta.ipynb",
180180
"NewChkMeta": "06_meta.ipynb",
181181
"BypassNewMeta": "06_meta.ipynb",
182+
"empty2none": "06_meta.ipynb",
183+
"anno_dict": "06_meta.ipynb",
182184
"use_kwargs_dict": "06_meta.ipynb",
183185
"use_kwargs": "06_meta.ipynb",
184186
"delegates": "06_meta.ipynb",

fastcore/meta.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# AUTOGENERATED! DO NOT EDIT! File to edit: nbs/06_meta.ipynb (unless otherwise specified).
22

3-
__all__ = ['test_sig', 'FixSigMeta', 'PrePostInitMeta', 'NewChkMeta', 'BypassNewMeta', 'use_kwargs_dict', 'use_kwargs',
4-
'delegates', 'method', 'funcs_kwargs']
3+
__all__ = ['test_sig', 'FixSigMeta', 'PrePostInitMeta', 'NewChkMeta', 'BypassNewMeta', 'empty2none', 'anno_dict',
4+
'use_kwargs_dict', 'use_kwargs', 'delegates', 'method', 'funcs_kwargs']
55

66
# Cell
77
from .imports import *
@@ -57,6 +57,16 @@ def __call__(cls, x=None, *args, **kwargs):
5757
if cls!=x.__class__: x.__class__ = cls
5858
return x
5959

60+
# Cell
61+
def empty2none(p):
62+
"Replace `Parameter.empty` with `None`"
63+
return None if p==inspect.Parameter.empty else p
64+
65+
# Cell
66+
def anno_dict(f):
67+
"`__annotation__ dictionary with `empty` cast to `None`, returning empty if doesn't exist"
68+
return {k:empty2none(v) for k,v in getattr(f, '__annotations__', {}).items()}
69+
6070
# Cell
6171
def _mk_param(n,d=None): return inspect.Parameter(n, inspect.Parameter.KEYWORD_ONLY, default=d)
6272

nbs/02_utils.ipynb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@
296296
{
297297
"data": {
298298
"text/plain": [
299-
"<__main__._t at 0x7fd8de963850>"
299+
"<__main__._t at 0x7f2feeb9ec10>"
300300
]
301301
},
302302
"execution_count": null,
@@ -1977,7 +1977,7 @@
19771977
{
19781978
"data": {
19791979
"text/plain": [
1980-
"{0: 'A', 1: 'B', 2: 'C', 5: 'F', 6: 'G'}"
1980+
"{70: 'F', 71: 'G'}"
19811981
]
19821982
},
19831983
"execution_count": null,
@@ -2009,7 +2009,7 @@
20092009
{
20102010
"data": {
20112011
"text/plain": [
2012-
"{0: 'A', 1: 'B', 2: 'C'}"
2012+
"{}"
20132013
]
20142014
},
20152015
"execution_count": null,
@@ -2041,7 +2041,7 @@
20412041
{
20422042
"data": {
20432043
"text/plain": [
2044-
"{5: 'F', 6: 'G'}"
2044+
"{70: 'F', 71: 'G'}"
20452045
]
20462046
},
20472047
"execution_count": null,
@@ -2362,7 +2362,7 @@
23622362
{
23632363
"data": {
23642364
"text/plain": [
2365-
"['c', 'a', 'f', 'g', 'h', 'd', 'e', 'b']"
2365+
"['e', 'd', 'h', 'f', 'c', 'b', 'g', 'a']"
23662366
]
23672367
},
23682368
"execution_count": null,
@@ -3121,7 +3121,7 @@
31213121
{
31223122
"data": {
31233123
"text/plain": [
3124-
"Path('.gitattributes')"
3124+
"Path('.ipynb_checkpoints')"
31253125
]
31263126
},
31273127
"execution_count": null,
@@ -3155,7 +3155,7 @@
31553155
{
31563156
"data": {
31573157
"text/plain": [
3158-
"(Path('../fastcore/all.py'), Path('00_test.ipynb'))"
3158+
"(Path('../fastcore/logargs.py'), Path('04_transform.ipynb'))"
31593159
]
31603160
},
31613161
"execution_count": null,
@@ -3719,7 +3719,7 @@
37193719
{
37203720
"data": {
37213721
"text/plain": [
3722-
"8"
3722+
"64"
37233723
]
37243724
},
37253725
"execution_count": null,

nbs/06_meta.ipynb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,40 @@
690690
"## Metaprogramming"
691691
]
692692
},
693+
{
694+
"cell_type": "code",
695+
"execution_count": null,
696+
"metadata": {},
697+
"outputs": [],
698+
"source": [
699+
"#export\n",
700+
"def empty2none(p):\n",
701+
" \"Replace `Parameter.empty` with `None`\"\n",
702+
" return None if p==inspect.Parameter.empty else p"
703+
]
704+
},
705+
{
706+
"cell_type": "code",
707+
"execution_count": null,
708+
"metadata": {},
709+
"outputs": [],
710+
"source": [
711+
"#export\n",
712+
"def anno_dict(f):\n",
713+
" \"`__annotation__ dictionary with `empty` cast to `None`, returning empty if doesn't exist\"\n",
714+
" return {k:empty2none(v) for k,v in getattr(f, '__annotations__', {}).items()}"
715+
]
716+
},
717+
{
718+
"cell_type": "code",
719+
"execution_count": null,
720+
"metadata": {},
721+
"outputs": [],
722+
"source": [
723+
"def _f(a:int, b:L)->str: ...\n",
724+
"test_eq(anno_dict(_f), {'a': int, 'b': L, 'return': str})"
725+
]
726+
},
693727
{
694728
"cell_type": "code",
695729
"execution_count": null,

0 commit comments

Comments
 (0)