Skip to content

Commit 29b0fd4

Browse files
committed
fixes #262
1 parent 9083c87 commit 29b0fd4

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

fastcore/basics.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -293,10 +293,10 @@ def store_attr(names=None, self=None, but='', cast=False, store_args=None, **att
293293
if store_args is None: store_args = not hasattr(self,'__slots__')
294294
if store_args and not hasattr(self, '__stored_args__'): self.__stored_args__ = {}
295295
anno = annotations(self) if cast else {}
296-
if not attrs:
297-
if names and isinstance(names,str): names = re.split(', *', names)
298-
ns = names if names else getattr(self, '__slots__', args[1:])
299-
attrs = {n:fr.f_locals[n] for n in ns}
296+
if names and isinstance(names,str): names = re.split(', *', names)
297+
ns = names if names else getattr(self, '__slots__', args[1:])
298+
added = {n:fr.f_locals[n] for n in ns}
299+
attrs = {**attrs, **added}
300300
if isinstance(but,str): but = re.split(', *', but)
301301
attrs = {k:v for k,v in attrs.items() if k not in but}
302302
return _store_attr(self, anno, **attrs)

nbs/01_basics.ipynb

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1584,10 +1584,10 @@
15841584
" if store_args is None: store_args = not hasattr(self,'__slots__')\n",
15851585
" if store_args and not hasattr(self, '__stored_args__'): self.__stored_args__ = {}\n",
15861586
" anno = annotations(self) if cast else {}\n",
1587-
" if not attrs:\n",
1588-
" if names and isinstance(names,str): names = re.split(', *', names)\n",
1589-
" ns = names if names else getattr(self, '__slots__', args[1:])\n",
1590-
" attrs = {n:fr.f_locals[n] for n in ns}\n",
1587+
" if names and isinstance(names,str): names = re.split(', *', names)\n",
1588+
" ns = names if names else getattr(self, '__slots__', args[1:])\n",
1589+
" added = {n:fr.f_locals[n] for n in ns}\n",
1590+
" attrs = {**attrs, **added}\n",
15911591
" if isinstance(but,str): but = re.split(', *', but)\n",
15921592
" attrs = {k:v for k,v in attrs.items() if k not in but}\n",
15931593
" return _store_attr(self, anno, **attrs)"
@@ -1811,6 +1811,21 @@
18111811
"assert t.a==1 and t.b==3 and t.c==2"
18121812
]
18131813
},
1814+
{
1815+
"cell_type": "code",
1816+
"execution_count": null,
1817+
"metadata": {},
1818+
"outputs": [],
1819+
"source": [
1820+
"#hide\n",
1821+
"#ensure that kwargs work with names==None\n",
1822+
"class T:\n",
1823+
" def __init__(self, a,b,c,**kwargs): store_attr(**kwargs)\n",
1824+
"\n",
1825+
"t = T(1,c=2,b=3,d=4,e=-1)\n",
1826+
"assert t.a==1 and t.b==3 and t.c==2 and t.d==4 and t.e==-1"
1827+
]
1828+
},
18141829
{
18151830
"cell_type": "markdown",
18161831
"metadata": {},

0 commit comments

Comments
 (0)