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+ )
26from misc .codegen .lib import schema as _schema
37import inspect as _inspect
48from dataclasses import dataclass as _dataclass
@@ -271,14 +275,16 @@ def __or__(self, other: _schema.PropertyModifier):
271275
272276_ = _PropertyAnnotation ()
273277
278+ drop = object ()
274279
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 ]:
276282 """
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 `_` .
280286
281- The name of the class used for annotation must be `_`
287+ `replace_bases` can be used to replace bases on the annotated class.
282288 """
283289 def decorator (cls : type ) -> _PropertyAnnotation :
284290 if cls .__name__ != "_" :
@@ -287,11 +293,15 @@ def decorator(cls: type) -> _PropertyAnnotation:
287293 annotated_cls .__doc__ = cls .__doc__
288294 for p , v in cls .__dict__ .get ("_pragmas" , {}).items ():
289295 _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__ )
290298 for a in dir (cls ):
291299 if a .startswith (_schema .inheritable_pragma_prefix ):
292300 setattr (annotated_cls , a , getattr (cls , a ))
293301 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__ :
295305 annotated_cls .__annotations__ [p ] |= a
296306 elif isinstance (a , (_PropertyAnnotation , _PropertyModifierList )):
297307 raise _schema .Error (f"annotated property { p } not present in annotated class "
0 commit comments