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

Missing a "Resources" or "More examples" section/page #160

Open
espetro opened this issue Oct 6, 2021 · 8 comments
Open

Missing a "Resources" or "More examples" section/page #160

espetro opened this issue Oct 6, 2021 · 8 comments

Comments

@espetro
Copy link

espetro commented Oct 6, 2021

Hello! I'm willing to try this library with some dummy code I have with Pydantic and other serialization/validation libraries. However, I cannot get pass the example of using str. For example, I don't get why the following happens:

from typing import List
from python.sized import NonEmpty

it_works = NonEmpty[str].parse("hello")
it_doesnt_work = NonEmpty[List[int]].parse([1,2])

Is there any additional example with container types, or a recorded talk/workshop showing how this works? Thanks in advance!

@antonagestam
Copy link
Owner

Hi 👋

I'm happy that you're trying out phantom-types!

I definitely think a resource/more examples/recipes section would be a good idea. I think the Sized collections section in the docs would also be improved with a few more examples. I've been meaning to put together a blog post about phantom types and I know @sobolevn have too, for me I just haven't been able to find the time ... yet.

As to why you're example is failing, there are two problems in the code. Since the collection you are passing to .parse() is mutable, it's refused to be interpreted as having a fixed length of 0 <. Consider this code as to why:

a_list = [1, 2]
parsed = NonEmpty[int].parse(a_list)
a_list.clear()  # the list is now empty
reveal_type(a_list)  # but mypy would reveal NonEmpty

So mutable collections are checked for and refused by the sized collection types. The Sized collections section in the docs and this recently added section about mutability mentions this. There is an open issue to change the behavior of PhantomSized.parse() to recursively cast its input to immutable datatypes, I've actually just started working on an implementation for that.

The other problem in your code is NonEmpty[List[int]], where the type parameter should be the inner type of the collection, so in this case it should be NonEmpty[int].

Putting it all together, your example would become:

from typing import List
from python.sized import NonEmpty

it_works = NonEmpty[str].parse("hello")
and_this_too = NonEmpty[int].parse((1, 2))

@sobolevn
Copy link
Contributor

sobolevn commented Oct 6, 2021

a blog post about phantom types and I know @sobolevn have too

I haven't released it yet 😢

@espetro
Copy link
Author

espetro commented Oct 6, 2021

Thank you for such a detailed explanation 🙏🏼 😊 I'm eager to try #110 once it's out. Yet I found the syntax less intuitive to use (closer to Haskell's NonEmpty than to Scala's NonEmpty[Container[_]]. I guess this can be solved with more examples.

As for the resources & examples, I can work on that if that's okay.
I will also speak about this library in an event here in Spain (probably in Spanish tho 😢 ); incidentally, I created a similar library while searching for refinement types in Python 😂 but I see this one is a better fit.

@antonagestam
Copy link
Owner

@espetro Cool! I just think it's nice that there are multiple things happening in this space within Python.

Just a note on the @refined decorator in your library, I've deliberately left that out of phantom-types in favor of letting other libraries solve that specific problem, such as typeguard and beartype.

I'll dig deeper into your library for inspiration! 👍

@antonagestam
Copy link
Owner

@espetro I presented phantom-types to typing-sig, the group that brings forth most of the typing features that makes it into Python. There's a recorded presentation with slides, that I mention here: #178

I was very nervous giving the talk, so I haven't watched it myself yet 🙈 But thought it might help in approaching this library.

@sobolevn
Copy link
Contributor

@antonagestam it was a great intro! 👏
Thanks for doing it!

@espetro
Copy link
Author

espetro commented Mar 22, 2022

@antonagestam I'm watching it right now, great talk by the way! 🚀
I didn't do the talk in Spanish yet, so I might expand on some ideas presented in your talk as well.

@antonagestam
Copy link
Owner

I've published a blog post about phantom types today, it doesn't go that much into detail about what you can do with the library though, and is deliberately more a raison d'être for phantom types in general. Please let me know if you have any feedback, small or big! 🙂

https://www.agest.am/phantom-types-in-python

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

No branches or pull requests

3 participants