You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello! With Pylance I get a typing error highlighted for the following code:
line='NAME|a=2345|b=SDFdss sdfsdf|ignored|c=another'parts=line.strip().split('|')
attrs=dict(p.split('=', 1) forpinpartsif'='inp) # < HERE
No overloads for "__init__" match the provided arguments
Argument types: (Generator[list[str], None, ...
I got the code working like that for aaaages!! But I see: a list can have different lengths and the ways I am making sure
it is like len == 2 is not obvious to the type checker, right?
I figured adding this into the builtins.pyi under class dict... makes the error go away:
There is no way to say "a list with exactly 2 elements", but we could add another __init__ overload for lists in general. It would mean that mypy won't complain if you do dict([[1, 2], [3, 4, 5]]), but that's fine. We generally prefer false negatives (type checker doesn't complain about wrong code) over false positives (type checker complains about correct code).
See also python/typing#592 and python/typing#786 for suggestions to add fixed-length sequences. I agree that we should probably add another overload, although I would suggest to allow sequences in general, not only lists.
Hello! With Pylance I get a typing error highlighted for the following code:
I got the code working like that for aaaages!! But I see: a list can have different lengths and the ways I am making sure
it is like
len == 2
is not obvious to the type checker, right?I figured adding this into the
builtins.pyi
underclass dict...
makes the error go away:But is there another way to make this more type safe?
Cheers:
ëRiC
The text was updated successfully, but these errors were encountered: