Git Product home page Git Product logo

Comments (3)

spookylukey avatar spookylukey commented on July 18, 2024

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.

spookylukey avatar spookylukey commented on July 18, 2024

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.

spookylukey avatar spookylukey commented on July 18, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.