Add source names (via new Stream and SourceSpan classes) and .span() combinator (take 2)#85
Open
tsani wants to merge 4 commits intopython-parsy:masterfrom
Open
Conversation
For backwards compatibility, these are only used to wrap input data when a source is given to `parse()` or `parse_partial()`. When a source is given, the following behaviours change: - the primitive `line_info` parser returns a 3-tuple instead of a 2-tuple - ParseError objects will include the source
be70346 to
5c5b293
Compare
Author
|
Hi @spookylukey, I need your help getting this merged -- can you let me know when you'll be able to check out the PR? |
spookylukey
requested changes
Apr 12, 2025
Member
There was a problem hiding this comment.
This looks really good, thanks for the PR.
In addition to the comment above, please could you also:
- add a test that covers
make_stream()being passed bytes. Currently if I comment out or break those lines, no test fails. - for completeness, a test covering the exception in
make_stream()as well. - add an entry into history.rst, mentioning all the additions,
plus the change in behaviour forline_info_at()if the newsourceparameters is passed.
Thanks!
Luke
Comment on lines
+35
to
+51
| def make_stream(data: str | bytes, source: Any): | ||
| """Constructs an appropriate stream type for `data` when it's one of the | ||
| three core supported datatypes of parsy (viz. str, bytes, list). Otherwise, | ||
| the data is assumed to just support a minimum of __getitem__ and | ||
| __len__.""" | ||
| if isinstance(data, str): | ||
| return StrStream(data, source) | ||
|
|
||
| if isinstance(data, bytes): | ||
| return ByteStream(data, source) | ||
|
|
||
| raise RuntimeError( | ||
| "A Parsy stream can be formed only on str and bytes, but the given " | ||
| f"data has type {type(data)}. If you are separately tokenizing the " | ||
| "data to parse, consider instead equipping the tokens with source " | ||
| "location metadata.", | ||
| ) |
Member
There was a problem hiding this comment.
This doesn't look like it handles list, in contrast to its docstring. That's OK, but the docstring should be corrected, and preferably the limitation should be noted in the appropriate docs i.e. the parse method.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Following up from #83, here's a new PR:
strandbytessubclasses equipped with asourceattribute, wrapping the user's input only when asourceis provided to.parse()or.parse_partial().The test suite is virtually unchanged this time around. I took care to keep everything as backwards compatible as possible, including keeping
line_inforeturning a 2-tuple when no source is given.