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

Fix: InsertValue will error if input is None #18

Conversation

mikenerone
Copy link
Contributor

@mikenerone mikenerone commented Jan 20, 2024

Title says most of it. Related, Metronome handled the no input case correctly, but there wasn't a test for it, so I added one for that, too. I also took the liberty of making the Metronome tests fast with autojump_clock, if that's ok (they were the only ones that slowed the test run).

@mikenerone mikenerone force-pushed the mikenerone/fix-producer-inputs-errors-if-none branch 2 times, most recently from 9da517d to ebfc6c9 Compare January 20, 2024 23:19
@mikenerone mikenerone force-pushed the mikenerone/fix-producer-inputs-errors-if-none branch from ebfc6c9 to 14176ba Compare January 20, 2024 23:24
@andersea
Copy link
Owner

Ok, so, interesting...

My original intended use case for InsertValue assumes that it will receive further values, in other words it was supposed to not be used as the first section and thus would always get an input. I was never really intending it to be used as first section.

Naively I actually expected that this might cause bad things to happen, like the pipeline might hang after single value was emitted, because there wasn't a source stream to exhaust of values. But, luckily the weld function actually knows how to handle this situation and will simply close the output memory channel, once the pump function exits, if there is no input. So, this turns out to be safe.

I think there should be a test for this. Maybe:

async def test_insert_value_without_input():
    async with Pipeline.create(
        InsertValue('a')
    ) as pipeline, pipeline.tap() as aiter:
        results = [v async for v in aiter]
        assert results == ['a']

And maybe a tiny note should be added to the documentation specifying the expected behaviour for this corner case.

@mikenerone
Copy link
Contributor Author

mikenerone commented Jan 21, 2024

My original intended use case for InsertValue assumes that it will receive further values, in other words it was supposed to not be used as the first section and thus would always get an input. I was never really intending it to be used as first section.

I kinda figured, but the type annotation says it's optional, so either the no-input case should be handled or the type annotation should be fixed. The latter opens its own can of worms because we'd then be overriding refine() with an incompatible signature, which is another thing the type-checkers will rightfully complain about. It seemed to me that the former option was simply easier. And it's kindof a feature, as I can't think of a better way to make a single-value source in one line, which I can imagine there are uses for.

I think there should be a test for this. Maybe:

Heh, this PR already contains one. Funnily enough, our test implementations are identical, except for the name and the fact that I used "n" as my input value instead of "a", simply because that's the character the existing test_insert_value() uses. Edit: Ooo, and my extra indentation on the InsertValue() line. How embarrassing.

async def test_insert_value_no_input(autojump_clock):
    async with Pipeline.create(
            InsertValue('n')
    ) as pipeline, pipeline.tap() as aiter:
        results = [v async for v in aiter]
        assert results == ['n']

And maybe a tiny note should be added to the documentation specifying the expected behaviour for this corner case.

Good call. I'll add something.

@mikenerone
Copy link
Contributor Author

mikenerone commented Jan 21, 2024

@andersea Looking at the docs, I noticed that all other sections that can be used as starting sections say "can be used as a starting section, if a source is provided." Should we add support for a source parameter to InsertValue for consistency (though it would be optional in this case)? (I'll wait for your answer before making the doc update.)

@andersea
Copy link
Owner

andersea commented Jan 22, 2024

@mikenerone Ok, I got through the review. Looks good. I haven't used the monkeypatch module, but I see how it helps a lot in this case. But I had to go through everything step by step to make sure I understood what was going on.

If you want to add an optional source parameter to InsertValue, go ahead. I am fine with it either way.

@mikenerone
Copy link
Contributor Author

Sorry for the delay, I'll get to it eventually. :P

@mikenerone
Copy link
Contributor Author

@andersea Ok, ready now. In the end I felt like the source parameter didn't really fit, so I just added the small doc update.

@andersea andersea merged commit c2efbc2 into andersea:master Jan 26, 2024
6 checks passed
@andersea
Copy link
Owner

Thank you!

@mikenerone mikenerone deleted the mikenerone/fix-producer-inputs-errors-if-none branch January 27, 2024 19:09
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.

2 participants