-
Notifications
You must be signed in to change notification settings - Fork 38
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
add some helpers to deal with streams of bytes #81
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
❗ Your organization needs to install the Codecov GitHub app to enable full functionality. Additional details and impacted files@@ Coverage Diff @@
## master #81 +/- ##
==========================================
+ Coverage 94.44% 94.57% +0.12%
==========================================
Files 8 8
Lines 1026 1050 +24
==========================================
+ Hits 969 993 +24
Misses 57 57 ☔ View full report in Codecov by Sentry. |
This looks like a useful addition. Some questions (to which I don't know the answer, just thinking out loud):
|
Unfortunately this requires manually writing out the mapped function into the resulting parser, as the `join` method needs to be bound to the same type as the elements to be joined. Because these elements can be an empty list, this type needs to be inferred from the type of the input stream.
Note that this already worked, except the type annotation was wrong and there were no tests.
@wbolster 's comment makes sense! So I've just amended this commit to remove In a similar vein, I changed |
return self.map("".join) | ||
|
||
@Parser | ||
def parser(stream: bytes | str, index: int) -> Result: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this could be typing.AnyStr
btw. (that would retain the type if Result
would become a generic type)
@spookylukey any chance you can have another look? in its current form this PR does not change any API, and just makes more stuff transparently work with both unicode strings ( |
This looks good, thanks! |
I'm afraid I had to revert this. Somehow in the middle of the night I realised the approach in this PR can't work. It makes the assumption that the type of the current parser's return values will be the type of the input stream, but there is no reason why this should be the case. Looking again, I see that this is documented in the new docstring, but it's a backwards incompatible breaking change. I've added a test which demonstrates this - on reverting the change in this PR, the test passes, but not otherwise. We can't assume that no-one is writing code like in the test. I can't see there is any way to get this to work. You can't test the type of the current return value either, as you might have zero items - an empty list of A backwards compatible alternative would be to do: def concat(self, joiner="") -> Parser:
return self.map(joiner.join) Would that work for you? Otherwise it's back to thinking about |
hmmm 🤔 adding an argument to parser.map(b"".join) … instead, which is more explicit, more flexible, and also shorter than the suggested parser.concat(self, joiner=b"") |
So we could go with
Which would give people a clue about how to easily implement |
This PR adds two little helpers that make it easier to parse streams of bytes.
If this is something that stands a chance of being merged, I can also spend some time on adding docs.
(It also contains an unrelated update to the pre-commit config to make the flake8 plugin work again.)