Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion fastcore/basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
# %% ../nbs/01_basics.ipynb
defaults = SimpleNamespace()

_allowed_types = (types.FunctionType, types.BuiltinFunctionType,
types.MethodType, types.ModuleType,
WrapperDescriptorType, MethodWrapperType, MethodDescriptorType)

# %% ../nbs/01_basics.ipynb
def ifnone(a, b):
"`b` if `a` is None else `a`"
Expand Down Expand Up @@ -352,7 +356,7 @@ def _eval_type(t, glb, loc):

def type_hints(f):
"Like `typing.get_type_hints` but returns `{}` if not allowed type"
if not isinstance(f, typing._allowed_types): return {}
if not isinstance(f, _allowed_types): return {}
ann,glb,loc = get_annotations_ex(f)
return {k:_eval_type(v,glb,loc) for k,v in ann.items()}

Expand Down
8 changes: 6 additions & 2 deletions nbs/01_basics.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@
"outputs": [],
"source": [
"#|export\n",
"defaults = SimpleNamespace()"
"defaults = SimpleNamespace()\n",
"\n",
"_allowed_types = (types.FunctionType, types.BuiltinFunctionType,\n",
" types.MethodType, types.ModuleType,\n",
" WrapperDescriptorType, MethodWrapperType, MethodDescriptorType)"
]
},
{
Expand Down Expand Up @@ -1870,7 +1874,7 @@
"\n",
"def type_hints(f):\n",
" \"Like `typing.get_type_hints` but returns `{}` if not allowed type\"\n",
" if not isinstance(f, typing._allowed_types): return {}\n",
" if not isinstance(f, _allowed_types): return {}\n",
" ann,glb,loc = get_annotations_ex(f)\n",
" return {k:_eval_type(v,glb,loc) for k,v in ann.items()}"
]
Expand Down
Loading