1
- from typing import Callable as _Callable , Dict as _Dict , ClassVar as _ClassVar
1
+ from typing import (
2
+ Callable as _Callable ,
3
+ Dict as _Dict ,
4
+ ClassVar as _ClassVar ,
5
+ )
2
6
from misc .codegen .lib import schema as _schema
3
7
import inspect as _inspect
4
8
from dataclasses import dataclass as _dataclass
@@ -271,14 +275,16 @@ def __or__(self, other: _schema.PropertyModifier):
271
275
272
276
_ = _PropertyAnnotation ()
273
277
278
+ drop = object ()
274
279
275
- def annotate (annotated_cls : type ) -> _Callable [[type ], _PropertyAnnotation ]:
280
+
281
+ def annotate (annotated_cls : type , replace_bases : _Dict [type , type ] | None = None ) -> _Callable [[type ], _PropertyAnnotation ]:
276
282
"""
277
- Add or modify schema annotations after a class has been defined
278
- For the moment, only docstring annotation is supported. In the future, any kind of
279
- modification will be allowed .
283
+ Add or modify schema annotations after a class has been defined previously.
284
+
285
+ The name of the class used for annotation must be `_` .
280
286
281
- The name of the class used for annotation must be `_`
287
+ `replace_bases` can be used to replace bases on the annotated class.
282
288
"""
283
289
def decorator (cls : type ) -> _PropertyAnnotation :
284
290
if cls .__name__ != "_" :
@@ -287,11 +293,15 @@ def decorator(cls: type) -> _PropertyAnnotation:
287
293
annotated_cls .__doc__ = cls .__doc__
288
294
for p , v in cls .__dict__ .get ("_pragmas" , {}).items ():
289
295
_ClassPragma (p , value = v )(annotated_cls )
296
+ if replace_bases :
297
+ annotated_cls .__bases__ = tuple (replace_bases .get (b , b ) for b in annotated_cls .__bases__ )
290
298
for a in dir (cls ):
291
299
if a .startswith (_schema .inheritable_pragma_prefix ):
292
300
setattr (annotated_cls , a , getattr (cls , a ))
293
301
for p , a in cls .__annotations__ .items ():
294
- if p in annotated_cls .__annotations__ :
302
+ if a is drop :
303
+ del annotated_cls .__annotations__ [p ]
304
+ elif p in annotated_cls .__annotations__ :
295
305
annotated_cls .__annotations__ [p ] |= a
296
306
elif isinstance (a , (_PropertyAnnotation , _PropertyModifierList )):
297
307
raise _schema .Error (f"annotated property { p } not present in annotated class "
0 commit comments