Use covariant types for multibind collections/mappings#307
Use covariant types for multibind collections/mappings#307davidparsson merged 2 commits intomasterfrom
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #307 +/- ##
=======================================
Coverage 96.54% 96.54%
=======================================
Files 1 1
Lines 608 608
Branches 103 103
=======================================
Hits 587 587
Misses 15 15
Partials 6 6 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
To avoid the need for explicit typing when using the method.
c73087f to
fb10e3a
Compare
There was a problem hiding this comment.
Pull request overview
This PR updates the multibind method to accept covariant collection types (Collection and Mapping) instead of concrete types (list and dict). This change allows callers to pass subtypes like tuples or immutable mappings without explicit type declarations, improving type checking compatibility.
Key changes:
- Runtime type checks updated from
listtoCollectionanddicttoMapping - Type signatures updated to accept
Collection[Union[T, Type[T]]]andMapping[K, Union[V, Type[V]]]instead of concreteListandDicttypes
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| except IndexError: | ||
| raise InvalidInterface(f"Use typing.List[T] or list[T] to specify the element type of the list") | ||
| if isinstance(to, list): | ||
| if isinstance(to, Collection): |
There was a problem hiding this comment.
Using isinstance(to, Collection) will incorrectly match strings since strings are collections in Python. This could cause the code to iterate over individual characters instead of treating the string as an invalid input. Add an explicit check to exclude strings: if isinstance(to, Collection) and not isinstance(to, (str, bytes)):
| if isinstance(to, Collection): | |
| if isinstance(to, Collection) and not isinstance(to, (str, bytes)): |
To allow passing type checking without having to explicitly declare types.