Comments (3)
Sorry I didn't see this earlier, I don't seem to be getting any notifications for this repo.
The problem is, type checking in Python currently works very badly with any dynamic tricks, and Parsy uses enough to defeat type checkers.
Of course, you can add some basic types for methods, but nothing that will really help you. I mean, a type checker could make sure that you were only calling .parse()
or .parse_partial()
on something that had those methods, but you typically call those very few times.
What you actually want is that in the following code, my_val
should be accepted, but my_val_2
should be rejected.
import parsy
my_val: int = parsy.regex(r"\d+").map(int).parse("1234")
my_val_2: int = parsy.regex(r"\d+").parse("1234")
Similarly, parser_1
should be accepted but parser_2
should not:
from dataclasses import dataclass
@dataclass
class Foo:
str: val
parser_1 = parsy.string("test").map(Foo)
parser_2 = parsy.regex(r"\d+").map(int).map(Foo)
For this, we need parsers that are generic in their output types. Without this, type annotations are not very useful - yes we can say that we've got some Parser
instances, but we don't know what they produce or how they can be chained together, which is 90% of what you do with Parsy code.
You can see my attempt to do this in #58 I got quite far, but then it all comes unstuck for seq
, which is pretty essential in many cases.
If you can do better, please let me know!
from parsy.
I've created an experimental fork of parsy with an attempt at this: https://github.com/python-parsy/typed-parsy
The results are interesting - it's impressive what you can get mypy/pyright to check for you, but the code is much less readable. I'll be blogging about it soon.
from parsy.
See my blog post:
https://lukeplant.me.uk/blog/posts/python-type-hints-parsy-case-study/
I've also added basic type hints to master in #69 but not a py.typed
file, as described in the blog post.
For these reasons I'm closing the ticket, but your feedback would still be appreciated.
from parsy.
Related Issues (20)
- >>= for bind HOT 14
- Missing documentation for eof parser? HOT 1
- Improve debugging: peek show next data in errors HOT 5
- Recompute line number for ParseError passed up from .bind
- Improve debugging ergonomics HOT 3
- Inline (explicit) and implicit tracing
- Missing seq import statement in tutorial HOT 1
- Help with parsing that "hangs" HOT 3
- Bug with backtracking and generate? HOT 4
- Parsy 1.3.0 fails to support 'group' keyword of `regex` function HOT 1
- async/await support in generate() HOT 6
- Release HOT 2
- Allow providing a default to optional() HOT 2
- maintaining state while parsing HOT 3
- combine fails when nothing is produced by many HOT 2
- [bug] Parser.desc() causes loss of error information, simple fix HOT 2
- alt doesn't use fallback parsers when initial parser has .many()/.sep_by() HOT 1
- Processing list of tokens HOT 1
- Seeking Guidance on Implementing Parser Autocomplete HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from parsy.