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