Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use NamedTuple for multiple operator return values #329

Closed
wants to merge 4 commits into from

Conversation

davidselassie
Copy link
Contributor

@davidselassie davidselassie commented Dec 6, 2023

  • Custom operator multiple return values must be NamedTuples
  • Uses some Self annotations
  • Moves error on using MultiStream as a Stream into __getattr__ to not mess up protocol checking

So it turns out that heterogeneous tuple unpacking is a special case in typecheckers and not something you can add onto any class. microsoft/pyright#5770

So let's take advantage of that and use NamedTuples as the blessed class for "multiple return streams" from operators. They're very similar to using dataclasses as we can programmatically inspect them and you can access the values by name, but unpacking types work correctly.

@davidselassie
Copy link
Contributor Author

davidselassie commented Dec 6, 2023

Hmmm. Ok so my reading of python/cpython#88089 is that generic NamedTuples are not supported until 3.11, not even with the "backwards compatible subclass Generic[T] syntax.

I'm going to think a bit more about what the right thing to do here is, but it might be that we forgo either "multiple return stream unpacking" or "named return streams".

I'd err on the side of keeping "named return streams" because the whole point of this was to help with understanding the semantics of the returned streams, and having to do

branch_out = op.branch("my_branch", lambda x: x % 2 == 0)
evens = branch_out.trues
odds = branch_out.falses

is slightly more verbose, but very clear.

@davidselassie
Copy link
Contributor Author

Gonna do #331 instead.

@davidselassie davidselassie deleted the op-ret-namedtuple branch December 7, 2023 22:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant