1
1
"""Metaclasses"""
2
2
3
- # AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/07_meta .ipynb.
3
+ # AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/05_meta .ipynb.
4
4
5
5
# %% auto 0
6
6
__all__ = ['test_sig' , 'FixSigMeta' , 'PrePostInitMeta' , 'AutoInit' , 'NewChkMeta' , 'BypassNewMeta' , 'empty2none' , 'anno_dict' ,
7
7
'use_kwargs_dict' , 'use_kwargs' , 'delegates' , 'method' , 'funcs_kwargs' ]
8
8
9
- # %% ../nbs/07_meta .ipynb
9
+ # %% ../nbs/05_meta .ipynb
10
10
from .imports import *
11
11
from .test import *
12
12
from contextlib import contextmanager
13
13
from copy import copy
14
14
import inspect
15
15
16
- # %% ../nbs/07_meta .ipynb
16
+ # %% ../nbs/05_meta .ipynb
17
17
def test_sig (f , b ):
18
18
"Test the signature of an object"
19
19
test_eq (str (inspect .signature (f )), b )
20
20
21
- # %% ../nbs/07_meta .ipynb
21
+ # %% ../nbs/05_meta .ipynb
22
22
def _rm_self (sig ):
23
23
sigd = dict (sig .parameters )
24
24
sigd .pop ('self' )
25
25
return sig .replace (parameters = sigd .values ())
26
26
27
- # %% ../nbs/07_meta .ipynb
27
+ # %% ../nbs/05_meta .ipynb
28
28
class FixSigMeta (type ):
29
29
"A metaclass that fixes the signature on classes that override `__new__`"
30
30
def __new__ (cls , name , bases , dict ):
31
31
res = super ().__new__ (cls , name , bases , dict )
32
32
if res .__init__ is not object .__init__ : res .__signature__ = _rm_self (inspect .signature (res .__init__ ))
33
33
return res
34
34
35
- # %% ../nbs/07_meta .ipynb
35
+ # %% ../nbs/05_meta .ipynb
36
36
class PrePostInitMeta (FixSigMeta ):
37
37
"A metaclass that calls optional `__pre_init__` and `__post_init__` methods"
38
38
def __call__ (cls , * args , ** kwargs ):
@@ -43,20 +43,20 @@ def __call__(cls, *args, **kwargs):
43
43
if hasattr (res ,'__post_init__' ): res .__post_init__ (* args ,** kwargs )
44
44
return res
45
45
46
- # %% ../nbs/07_meta .ipynb
46
+ # %% ../nbs/05_meta .ipynb
47
47
class AutoInit (metaclass = PrePostInitMeta ):
48
48
"Same as `object`, but no need for subclasses to call `super().__init__`"
49
49
def __pre_init__ (self , * args , ** kwargs ): super ().__init__ (* args , ** kwargs )
50
50
51
- # %% ../nbs/07_meta .ipynb
51
+ # %% ../nbs/05_meta .ipynb
52
52
class NewChkMeta (FixSigMeta ):
53
53
"Metaclass to avoid recreating object passed to constructor"
54
54
def __call__ (cls , x = None , * args , ** kwargs ):
55
55
if not args and not kwargs and x is not None and isinstance (x ,cls ): return x
56
56
res = super ().__call__ (* ((x ,) + args ), ** kwargs )
57
57
return res
58
58
59
- # %% ../nbs/07_meta .ipynb
59
+ # %% ../nbs/05_meta .ipynb
60
60
class BypassNewMeta (FixSigMeta ):
61
61
"Metaclass: casts `x` to this class if it's of type `cls._bypass_type`"
62
62
def __call__ (cls , x = None , * args , ** kwargs ):
@@ -66,20 +66,20 @@ def __call__(cls, x=None, *args, **kwargs):
66
66
if cls != x .__class__ : x .__class__ = cls
67
67
return x
68
68
69
- # %% ../nbs/07_meta .ipynb
69
+ # %% ../nbs/05_meta .ipynb
70
70
def empty2none (p ):
71
71
"Replace `Parameter.empty` with `None`"
72
72
return None if p == inspect .Parameter .empty else p
73
73
74
- # %% ../nbs/07_meta .ipynb
74
+ # %% ../nbs/05_meta .ipynb
75
75
def anno_dict (f ):
76
76
"`__annotation__ dictionary with `empty` cast to `None`, returning empty if doesn't exist"
77
77
return {k :empty2none (v ) for k ,v in getattr (f , '__annotations__' , {}).items ()}
78
78
79
- # %% ../nbs/07_meta .ipynb
79
+ # %% ../nbs/05_meta .ipynb
80
80
def _mk_param (n ,d = None ): return inspect .Parameter (n , inspect .Parameter .KEYWORD_ONLY , default = d )
81
81
82
- # %% ../nbs/07_meta .ipynb
82
+ # %% ../nbs/05_meta .ipynb
83
83
def use_kwargs_dict (keep = False , ** kwargs ):
84
84
"Decorator: replace `**kwargs` in signature with `names` params"
85
85
def _f (f ):
@@ -93,7 +93,7 @@ def _f(f):
93
93
return f
94
94
return _f
95
95
96
- # %% ../nbs/07_meta .ipynb
96
+ # %% ../nbs/05_meta .ipynb
97
97
def use_kwargs (names , keep = False ):
98
98
"Decorator: replace `**kwargs` in signature with `names` params"
99
99
def _f (f ):
@@ -107,7 +107,7 @@ def _f(f):
107
107
return f
108
108
return _f
109
109
110
- # %% ../nbs/07_meta .ipynb
110
+ # %% ../nbs/05_meta .ipynb
111
111
def delegates (to :FunctionType = None , # Delegatee
112
112
keep = False , # Keep `kwargs` in decorated function?
113
113
but :list = None , # Exclude these parameters from signature
@@ -135,13 +135,13 @@ def _f(f):
135
135
return f
136
136
return _f
137
137
138
- # %% ../nbs/07_meta .ipynb
138
+ # %% ../nbs/05_meta .ipynb
139
139
def method (f ):
140
140
"Mark `f` as a method"
141
141
# `1` is a dummy instance since Py3 doesn't allow `None` any more
142
142
return MethodType (f , 1 )
143
143
144
- # %% ../nbs/07_meta .ipynb
144
+ # %% ../nbs/05_meta .ipynb
145
145
def _funcs_kwargs (cls , as_method ):
146
146
old_init = cls .__init__
147
147
def _init (self , * args , ** kwargs ):
@@ -157,7 +157,7 @@ def _init(self, *args, **kwargs):
157
157
if hasattr (cls , '__signature__' ): cls .__signature__ = _rm_self (inspect .signature (cls .__init__ ))
158
158
return cls
159
159
160
- # %% ../nbs/07_meta .ipynb
160
+ # %% ../nbs/05_meta .ipynb
161
161
def funcs_kwargs (as_method = False ):
162
162
"Replace methods in `cls._methods` with those from `kwargs`"
163
163
if callable (as_method ): return _funcs_kwargs (as_method , False )
0 commit comments