Giter VIP home page Giter VIP logo

Comments (8)

llucax avatar llucax commented on June 3, 2024

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.

llucax avatar llucax commented on June 3, 2024

Clarification done in #271. Closing as invalid.

from frequenz-channels-python.

shsms avatar shsms commented on June 3, 2024

This might actually become possible once we have default generics: https://peps.python.org/pep-0696/

from frequenz-channels-python.

llucax avatar llucax commented on June 3, 2024

How could that help?

from frequenz-channels-python.

shsms avatar shsms commented on June 3, 2024
  1. type return value with Self
  2. add __getattr__ in _Map that retrieves functions from underlying object.
  3. Change Timer etc to have default generic argument timedelta, etc., so that users don't have to specify it.

If we skip 3, it might be possible even now.

from frequenz-channels-python.

llucax avatar llucax commented on June 3, 2024

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.

shsms avatar shsms commented on June 3, 2024

yes, that's why I think with default generics this can be a more transparent change.

from frequenz-channels-python.

llucax avatar llucax commented on June 3, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.