Repository navigation
Fix instance factories rejecting implementers of a returned Protocol and accepting abstract classes - #1033
Conversation
…and accepting abstract classes
[Medium impact] The changes affect factory type-hint workflows across parsing and defaults. The PR appears safe to merge, with a remaining gap in callable protocol lambda defaults. Findings
SummaryLets instance factories accept classes and functions that implement their returned
Reviews (1) · Last reviewed commit: "Fix instance factories rejecting impleme..." · Reviewed by Greptile |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1033 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 27 27
Lines 10081 10091 +10
=========================================
+ Hits 10081 10091 +10 ☔ View full report in Codecov by Harness. |
|



What does this PR do?
Fixes instance factories whose return type is a
Protocol, e.g.Callable[[list[float]], SomeProtocol]or a callable protocol returning one. Unless the protocol wasruntime_checkable, these failed for every class and function, even ones inheriting from the protocol or annotated to return it, because the return type was checked withissubclass, which raises an error for other protocols. They now use the same signature-based check as plainProtocoltype hints, including for subscripted generic protocols, whereProto[int]andProto[str]accept different implementations. This check also applies when resolving defaults in signatures, solambda p: Impl(p, lr=0.5), andImpl(lr=0.5)for a plainProtocol, now become aclass_pathwithinit_args.Also,
Callableand callableProtocoltypes now reject abstract classes and protocols when parsing, with the same error as other class types. Previously, an abstract class only failed on instantiation or when calling the factory.A class with the wrong signature is now rejected even if the protocol is
runtime_checkable, which previously passed by matching method names only.Before submitting