The terminology used by dry-python is hard to follow for me, unfortunately (e.g. "monadic" does nothing for me :-). However there are some people on this list who are working on proposals for tensor types. I don't know if it would apply to the builtin List/list type, but using those proposals you could probably define a function like this ``` def zeros(n: int) -> List[n]: return [0]*n ``` The actual notation being proposed is a bit more complex, but you get the idea. On Tue, Sep 22, 2020 at 10:58 AM Никита Соболев <n.a.sobolev@gmail.com> wrote:
SizedList list is not just a type, it is a dependent type (in a way that the actual type is dependent on the concrete value). That's something very few languages have. And using a language with dependent types is a nightmare for a regular developer.
There are also two projects that can partially solve this problem (based on phantom types, which are just shallow / phantom wrappers for actual types): - https://github.com/antonagestam/phantom-types - https://pypi.org/project/phantoms/ (WIP in our dry-python family)
They have a notation like `NonEmpty[List[int]]`, that guarantees that list has at least one element. And can be extended to use something like `Sized[List[int], 5]` which guarantees that list has specifically 5 elements.
One more thing that might be helpful: monadic `NonEmptyList` type. I am not sure that there are any actual implementations in Python, but we are tracking this potential feature for dry-python/returns here: https://github.com/dry-python/returns/issues/257
вт, 22 сент. 2020 г. в 20:24, Teddy Sudol via Typing-sig < typing-sig@python.org>:
To be clear, my point was that the problem is a lot harder than it looks at first, not just saying your example was bad!
Do other languages have something like SizedList? I can't recall any off the top of my head.
-- Teddy
On Tue, Sep 22, 2020 at 10:21 AM Brett Cannon <brett@python.org> wrote:
On Mon, Sep 21, 2020 at 1:23 PM Teddy Sudol <tsudol@google.com> wrote:
Are you asking for a higher-kinded (I believe?) `split` that can specify in its return type how many elements it returns? A problem with that is `some_str.split(a_delimiter, 2)` is only guaranteed to return an array of *up to* 3 (maxsplit+1) elements. (In fact, your example only works by accident -- it should be `split("a/b", 1)` to guarantee the call will always succeed.)
Yep, I basically screwed up by misremembering that the count argument to str.split() is a max count, not a guaranteed count. Sorry about the noise on this (although I guess the question somewhat can still be asked had I chosen to create a function that guarantees the length of a list).
-- Teddy
On Mon, Sep 21, 2020 at 12:38 PM Guido van Rossum <guido@python.org> wrote:
But in a non-toy example how do you expect a static checker to know how many items "a/b".split("/", 2) returns? Hopefully you're not expecting the checker to evaluate "a/b".split("/", 2) at compile time?
If str.split() worked the way my brain told me it worked, I would have asked if `str.split(self, sep: str, maxsplit: int = Literal[2]) -> SizedList[2, str]: ...` made sense.
-Brett
I could only see making this pass by using cast(Tuple[str, str], "a/b".split("/", 2)).
On Mon, Sep 21, 2020 at 12:15 PM Brett Cannon <brett@python.org> wrote:
Let's say I have the following function:
```python
def spam(a: str, b: str, c: str, d: int = 42): ...
```
Now if I call that with: ```python spam(*"a/b".split("/", 2), "hello") ```
That will execute fine and actually result in appropriate types for those arguments.
Unfortunately both Pylance and mypy say that call is incompatible because of `str.split(...) -> List`. And since 'typing' lacks an concept of a sized list like it does for tuples, I don't know how to make this pass.
Am I overlooking something? Or is this a gap due to there not being an equivalent of 'SizedList' in 'typing'? _______________________________________________ Typing-sig mailing list -- typing-sig@python.org To unsubscribe send an email to typing-sig-leave@python.org https://mail.python.org/mailman3/lists/typing-sig.python.org/ Member address: guido@python.org
-- --Guido van Rossum (python.org/~guido) *Pronouns: he/him **(why is my pronoun here?)* <http://feministing.com/2015/02/03/how-using-they-as-a-singular-pronoun-can-change-the-world/> _______________________________________________ Typing-sig mailing list -- typing-sig@python.org To unsubscribe send an email to typing-sig-leave@python.org https://mail.python.org/mailman3/lists/typing-sig.python.org/ Member address: tsudol@google.com
_______________________________________________
Typing-sig mailing list -- typing-sig@python.org To unsubscribe send an email to typing-sig-leave@python.org https://mail.python.org/mailman3/lists/typing-sig.python.org/ Member address: n.a.sobolev@gmail.com
-- --Guido van Rossum (python.org/~guido) *Pronouns: he/him **(why is my pronoun here?)* <http://feministing.com/2015/02/03/how-using-they-as-a-singular-pronoun-can-change-the-world/>