fix: replace protocol with direct subclass #2029
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Python typing protocols make it possible to enforce duck-typing at type-check time, i.e. ensure that an object has the appropriate methods and attributes rather than whether it inherits from another class. To partially check this at runtime there is a
runtime_checkable
decorator that simply test whether a given object implements the same methods (ignoring their types).However, this apparently is much slower than I'd anticipated #2028. This issue is only a problem because we need to sanitise the user inputs to our layouts. Otherwise, once
mypy
is fully covered through the code-base, we could just remove our runtime input validation in most cases. Given that we want to provide helpful user errors, we need runtime checks to work. We could fast-path this withbut in this case, we don't truly need to duck-type backends; they're a limited abstraction that we can forgo the flexibility of protocols.
Fixes #2028