-
Notifications
You must be signed in to change notification settings - Fork 6.7k
No-op message_handler (and sibling) decorators for abstract methods #5945
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Thanks for the PR! Could you try to remove this type ignore here and see if the type checking is happy:
|
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #5945 +/- ##
==========================================
- Coverage 75.77% 75.77% -0.01%
==========================================
Files 191 191
Lines 13100 13100
==========================================
- Hits 9927 9926 -1
- Misses 3173 3174 +1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
@ekzhu Ah, sorry, I think my note may have been unclear. This PR, by itself, won't make the typechecker happy anywhere that it wasn't before. The only impact it has is to prevent runtime errors from It does enable a solution to the instances you mention - you could decorate with The upshot being - this PR will not resolve any typechecking errors on the lines you linked, such that I could just remove the |
okay I think I get it. This PR allows for abstract message handlers. I think this is useful especially considering we want to support using Core RoutedAgent as participant in AgentChat teams. Having an abstract base class for participant and group chat manager with abstract message handlers can be helpful to enforce the contract. I will leave this to @jackgerrits to decide. It's not urgent to merge this now but let's come back to this next week. |
I found a potential issue. @nissa-seru The goal of making the decorators no-op for abstract methods to support type variables in signatures is a good one. However, I believe the current implementation doesn't quite achieve the intended effect. The To achieve the no-op behavior, the check for |
Why are these changes needed?
@message_handler
and sibling decorators are processed at time of declaration, including checking whether the type hints on decorated methods are valid (since they are used by the runtime for determining which type of message the method will handle.) This creates a catch-22 when declaring abstract message handlers:The abstract method can omit the
@message_handler
decorator, but this (justifiably) causes typechecking errors when adding the decorator to implementations of the abstract method (in subclasses) due to the implementation being decorated and the abstract method being undecoratedThe abstract method can have the
@message_handler
decorator and use a concrete type; this causes typechecking errors on implementations if they handle a different concrete type (of the flavorType parameter "ReceivesT@MessageHandler" is invariant, but "TextPayload" is not the same as "AbstractPayload"
)@message_handler
decorator and use a more appropriate typehint (ie a TypeVar) in the signature of the abstract method. This satisfies the typechecker, but currently raises at declaration time because the decorator is processing the abstract method's signature for validity as an instantiable method handler (even though it can never be directly instantiated due to being an abstract method.)I'm hopeful that in future we can explore how to make the type-hint-based routing mechanism more flexible, as
ReceivesT
being invariant results in poor ergonomics for message handlers (for example, I would like to be able to use generics in message handler type hints.) However, I'm not yet familiar enough with the code to confidently judge how much of this inconvenience is due to a knowledge gap on my end (either a specific gap, or just natural learning curve) versus an actual cause for change.Accordingly, this PR opts for the conservative band-aid of making the message_handler decorators no-op if the decorated method is abstract. This unblocks option 3 (as described above) to enable use of ABC with message handlers without requiring typechecking suppression on each implementation.
Checks
poe check
passes on my local clonemain
with Fixpoe check
on Windows #5942 merged. Without Fixpoe check
on Windows #5942, a variety of tests will fail on Windows for reasons detailed in that PR (and unrelated to this one.)