2121from nonebot .consts import ARG_KEY , RECEIVE_KEY
2222from nonebot .internal .params import DefaultParam
2323from nepattern import ANY , STRING , TPattern , AnyString
24+ from nonebot .internal .matcher .matcher import MatcherMeta
2425from _weakref import _remove_dead_weakref # type: ignore
2526from nonebot import require , get_driver , get_plugin_config
2627from nonebot .plugin .on import store_matcher , get_matcher_source
@@ -106,18 +107,47 @@ def __get__(self, instance, owner):
106107T1 = TypeVar ("T1" )
107108
108109
109- class AlconnaMatcher (Matcher ):
110+ class AlconnaMatcherMeta (MatcherMeta ):
111+ if TYPE_CHECKING :
112+ _command_path : str
113+
114+ def __repr__ (self ) -> str :
115+ return (
116+ f"{ self .__class__ .__name__ } (type={ self .type !r} "
117+ + (f", module={ self .module_name } " if self .module_name else "" )
118+ + (f", lineno={ self ._source .lineno } " if self ._source and self ._source .lineno is not None else "" )
119+ + f", command={ self ._command_path } "
120+ + ")"
121+ )
122+
123+
124+ class AlconnaMatcher (Matcher , metaclass = AlconnaMatcherMeta ):
110125 # command: ClassVar[weakref.ReferenceType[Alconna]]
111126 basepath : ClassVar [str ]
112127 executor : ClassVar [ExtensionExecutor ]
113- _command_path : ClassVar [str ]
128+ _command_path : ClassVar [str ] = ""
114129 _tests : ClassVar [list [tuple [UniMessage , dict [str , Any ] | None , bool ]]]
115130 _rule : ClassVar [AlconnaRule ]
116131
117132 @classmethod
118133 def command (cls ) -> Alconna :
119134 return cls ._rule .command () # type: ignore
120135
136+ @classmethod
137+ def clean (cls ) -> None :
138+ matchers [cls .priority ].remove (cls )
139+ cls ._rule .destroy ()
140+ cls .executor .destroy ()
141+
142+ def __repr__ (self ) -> str :
143+ return (
144+ f"{ self .__class__ .__name__ } (type={ self .type !r} "
145+ + (f", module={ self .module_name } " if self .module_name else "" )
146+ + (f", lineno={ self ._source .lineno } " if self ._source and self ._source .lineno is not None else "" )
147+ + f", command={ self ._command_path } "
148+ + ")"
149+ )
150+
121151 @classmethod
122152 @overload
123153 def shortcut (cls , key : str | TPattern , args : ShortcutArgs | None = None ) -> type [Self ]:
@@ -969,8 +999,7 @@ def on_alconna(
969999 command_manager .delete (command )
9701000 return _matcher
9711001 if conflict_resolver == "replace" :
972- _matcher .destroy ()
973- command_manager .delete (exist )
1002+ _matcher .clean ()
9741003 command_manager .register (command )
9751004 else :
9761005 exist .formatter .remove (command )
@@ -1033,7 +1062,6 @@ def on_alconna(
10331062 )
10341063 matcher : type [AlconnaMatcher ] = cast ("type[AlconnaMatcher]" , NewMatcher )
10351064 matcher .HANDLER_PARAM_TYPES = params
1036- log ("TRACE" , f"Define new matcher { NewMatcher } " )
10371065
10381066 matchers [priority ].append (NewMatcher )
10391067 store_matcher (matcher )
@@ -1044,6 +1072,8 @@ def on_alconna(
10441072 command .meta .extra ["matcher.source" ] = matcher ._source
10451073 command .meta .extra ["matcher.position" ] = (priority , len (matchers [priority ]) - 1 )
10461074
1075+ log ("TRACE" , f"Define new matcher { NewMatcher } " )
1076+
10471077 def remove (wr , selfref = weakref .ref (command .meta ), _atomic_removal = _remove_dead_weakref ):
10481078 self = selfref ()
10491079 if self is not None :
0 commit comments