Skip to content

Commit

Permalink
Merge pull request #646 from hugovk/fix-allowed-types
Browse files Browse the repository at this point in the history
Vendor `typing._allowed_types` removed in Python 3.14
  • Loading branch information
jph00 authored Nov 30, 2024
2 parents a868b7e + e34eb85 commit c060837
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
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

0 comments on commit c060837

Please sign in to comment.