Comments (8)
I don't think this is doable. Currently map()
is defined as:
def map(self, call: Callable[[_T_co], _U_co]) -> Receiver[_U_co]:
Where _T_co
is the type parameter of the Receiver
that defines this map()
.
To be able to return a type that has all the methods of the receiver subclass (like Timer
), besides the implementation details on how we create such a wrapper, type-system-wise we should be able to do something like:
def map(self, call: Callable[[_T_co], _U_co]) -> Self[_U_co]:
I.e. we would have to return the concrete receiver type (like Timer
) but instead of being parametrized with _T_co
as the value type, it should be _U_co
. This doesn't seem to be supported by Python.
And even if it were, it is basically impossible for all types that don't even have a type parameter. For example, Timer
is not parametrized, it just inherits from Receiver[timedelta]
, so the mapper receiver should be somehow Timer[_U_co]
, which doesn't make any sense as Timer
doesn't have a type parameter.
An alternative would be to just return Any
, but that is probably much worse than just asking the users to save the original receiver if they want to call any extra methods from it at some point.
I suggest improving the documentation to inform the user about this issue and just recommend them to save a reference to the original receiver if they need to do something else with it.
from frequenz-channels-python.
Clarification done in #271. Closing as invalid.
from frequenz-channels-python.
This might actually become possible once we have default generics: https://peps.python.org/pep-0696/
from frequenz-channels-python.
How could that help?
from frequenz-channels-python.
- type return value with
Self
- add
__getattr__
in_Map
that retrieves functions from underlying object. - Change
Timer
etc to have default generic argumenttimedelta
, etc., so that users don't have to specify it.
If we skip 3, it might be possible even now.
from frequenz-channels-python.
Yeah, actually when bouncing this with ChatGPT, it suggested that option, but I think it is too "infectious" to be a reasonable solution. We need to make all sub-classes of Receiver
generic just to be able to return the correct type in map()
.
from frequenz-channels-python.
yes, that's why I think with default generics this can be a more transparent change.
from frequenz-channels-python.
Still it is not transparent at all to the implementator, and depending on how good/bad the error message is when trying to map()
a receiver that is not parametrized, this requirement to implement all receivers as generics could even transpire to the end user.
In any case, I guess we agree at least that we shouldn't do this until defaults for types is implemented and we bump the minimum supported Python version to a version having that feature, which I guess it could take a long while....
from frequenz-channels-python.
Related Issues (20)
- Improve composability of receivers HOT 4
- `FileWatcher` should expose more `awatch` configuration options
- Write introductory documentation
- Many class attributes should be private or read-only
- Consolidate and make channels `name`s (`client_id` / `service_id`) optional
- Remove `Peekable`
- Remove `Bidirectional`
- Remove `MergeNamed`
- Expose `Merge` as a `merge()` function
- Backport timer `reset()` fix to v0.16 HOT 2
- `Timer`: Remove `periodic()` and `timeout()`
- Filewatcher is not reporting events in CIFS mounts HOT 3
- Documentation and code cleanup for 1.0 HOT 1
- Remove uses of `BaseException`
- Find a way to make using select less likely to starve a receiver HOT 1
- Channel that doesn't remove the oldest messages when receiver buffer is full HOT 2
- Implement `SyncSender` and `AsyncSender` HOT 5
- Add a periodic timer
- Refactor event type and initialization in FileWatcher
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from frequenz-channels-python.