Skip to content

Commit

Permalink
fixes #560
Browse files Browse the repository at this point in the history
  • Loading branch information
jph00 committed May 29, 2024
1 parent 50a1581 commit 678806d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 20 deletions.
2 changes: 1 addition & 1 deletion fastcore/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.5.41"
__version__ = "1.5.42"
18 changes: 11 additions & 7 deletions fastcore/basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,13 @@ def tonull(x):
return null if x is None else x

# %% ../nbs/01_basics.ipynb 41
def get_class(nm, *fld_names, sup=None, doc=None, funcs=None, **flds):
def get_class(nm, *fld_names, sup=None, doc=None, funcs=None, anno=None, **flds):
"Dynamically create a class, optionally inheriting from `sup`, containing `fld_names`"
attrs = {}
for f in fld_names: attrs[f] = None
if not anno: anno = {}
for f in fld_names:
attrs[f] = None
if f not in anno: anno[f] = typing.Any
for f in listify(funcs): attrs[f.__name__] = f
for k,v in flds.items(): attrs[k] = v
sup = ifnone(sup, ())
Expand All @@ -111,22 +114,23 @@ def _init(self, *args, **kwargs):
for i,v in enumerate(args): setattr(self, list(attrs.keys())[i], v)
for k,v in kwargs.items(): setattr(self,k,v)

all_flds = [*fld_names,*flds.keys()]
attrs['_fields'] = [*fld_names,*flds.keys()]
def _eq(self,b):
return all([getattr(self,k)==getattr(b,k) for k in all_flds])
return all([getattr(self,k)==getattr(b,k) for k in self._fields])

if not sup: attrs['__repr__'] = basic_repr(all_flds)
if not sup: attrs['__repr__'] = basic_repr(attrs['_fields'])
attrs['__init__'] = _init
attrs['__eq__'] = _eq
if anno: attrs['__annotations__'] = anno
res = type(nm, sup, attrs)
if doc is not None: res.__doc__ = doc
return res

# %% ../nbs/01_basics.ipynb 45
def mk_class(nm, *fld_names, sup=None, doc=None, funcs=None, mod=None, **flds):
def mk_class(nm, *fld_names, sup=None, doc=None, funcs=None, mod=None, anno=None, **flds):
"Create a class using `get_class` and add to the caller's module"
if mod is None: mod = sys._getframe(1).f_locals
res = get_class(nm, *fld_names, sup=sup, doc=doc, funcs=funcs, **flds)
res = get_class(nm, *fld_names, sup=sup, doc=doc, funcs=funcs, anno=anno, **flds)
mod[nm] = res

# %% ../nbs/01_basics.ipynb 50
Expand Down
30 changes: 19 additions & 11 deletions nbs/01_basics.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
"from __future__ import annotations\n",
"from fastcore.test import *\n",
"from nbdev.showdoc import *\n",
"from fastcore.nb_imports import *"
"from fastcore.nb_imports import *\n",
"from inspect import get_annotations"
]
},
{
Expand Down Expand Up @@ -553,10 +554,13 @@
"outputs": [],
"source": [
"#|export\n",
"def get_class(nm, *fld_names, sup=None, doc=None, funcs=None, **flds):\n",
"def get_class(nm, *fld_names, sup=None, doc=None, funcs=None, anno=None, **flds):\n",
" \"Dynamically create a class, optionally inheriting from `sup`, containing `fld_names`\"\n",
" attrs = {}\n",
" for f in fld_names: attrs[f] = None\n",
" if not anno: anno = {}\n",
" for f in fld_names:\n",
" attrs[f] = None\n",
" if f not in anno: anno[f] = typing.Any\n",
" for f in listify(funcs): attrs[f.__name__] = f\n",
" for k,v in flds.items(): attrs[k] = v\n",
" sup = ifnone(sup, ())\n",
Expand All @@ -566,13 +570,14 @@
" for i,v in enumerate(args): setattr(self, list(attrs.keys())[i], v)\n",
" for k,v in kwargs.items(): setattr(self,k,v)\n",
"\n",
" all_flds = [*fld_names,*flds.keys()]\n",
" attrs['_fields'] = [*fld_names,*flds.keys()]\n",
" def _eq(self,b):\n",
" return all([getattr(self,k)==getattr(b,k) for k in all_flds])\n",
" return all([getattr(self,k)==getattr(b,k) for k in self._fields])\n",
"\n",
" if not sup: attrs['__repr__'] = basic_repr(all_flds)\n",
" if not sup: attrs['__repr__'] = basic_repr(attrs['_fields'])\n",
" attrs['__init__'] = _init\n",
" attrs['__eq__'] = _eq\n",
" if anno: attrs['__annotations__'] = anno\n",
" res = type(nm, sup, attrs)\n",
" if doc is not None: res.__doc__ = doc\n",
" return res"
Expand All @@ -592,7 +597,8 @@
"\n",
"### get_class\n",
"\n",
"> get_class (nm, *fld_names, sup=None, doc=None, funcs=None, **flds)\n",
"> get_class (nm, *fld_names, sup=None, doc=None, funcs=None, anno=None,\n",
"> **flds)\n",
"\n",
"*Dynamically create a class, optionally inheriting from `sup`, containing `fld_names`*"
],
Expand All @@ -603,7 +609,8 @@
"\n",
"### get_class\n",
"\n",
"> get_class (nm, *fld_names, sup=None, doc=None, funcs=None, **flds)\n",
"> get_class (nm, *fld_names, sup=None, doc=None, funcs=None, anno=None,\n",
"> **flds)\n",
"\n",
"*Dynamically create a class, optionally inheriting from `sup`, containing `fld_names`*"
]
Expand Down Expand Up @@ -634,7 +641,7 @@
}
],
"source": [
"_t = get_class('_t', 'a', b=2)\n",
"_t = get_class('_t', 'a', b=2, anno={'b':int})\n",
"t = _t()\n",
"test_eq(t.a, None)\n",
"test_eq(t.b, 2)\n",
Expand All @@ -645,6 +652,7 @@
"test_eq(t.a, 1)\n",
"test_eq(t.b, 3)\n",
"test_eq(t, pickle.loads(pickle.dumps(t)))\n",
"test_eq(get_annotations(_t), {'b':int, 'a':typing.Any})\n",
"repr(t)"
]
},
Expand All @@ -662,10 +670,10 @@
"outputs": [],
"source": [
"#|export\n",
"def mk_class(nm, *fld_names, sup=None, doc=None, funcs=None, mod=None, **flds):\n",
"def mk_class(nm, *fld_names, sup=None, doc=None, funcs=None, mod=None, anno=None, **flds):\n",
" \"Create a class using `get_class` and add to the caller's module\"\n",
" if mod is None: mod = sys._getframe(1).f_locals\n",
" res = get_class(nm, *fld_names, sup=sup, doc=doc, funcs=funcs, **flds)\n",
" res = get_class(nm, *fld_names, sup=sup, doc=doc, funcs=funcs, anno=anno, **flds)\n",
" mod[nm] = res"
]
},
Expand Down
2 changes: 1 addition & 1 deletion settings.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ author = Jeremy Howard and Sylvain Gugger
author_email = infos@fast.ai
copyright = fast.ai
branch = master
version = 1.5.41
version = 1.5.42
min_python = 3.7
audience = Developers
language = English
Expand Down

0 comments on commit 678806d

Please sign in to comment.