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://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).-- TeddyOn 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.-BrettI 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:```pythondef spam(a: str, b: str, c: str, d: int = 42): ...```Now if I call that with:```pythonspam(*"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?)
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