最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Type annotate inside loop - Stack Overflow

programmeradmin1浏览0评论

The mypy error is

Need type annotation for "args"  [var-annotated]
Need type annotation for "kwargs"  [var-annotated]

and here is the piece of code

expected_args: Optional[Sequence[Tuple[Any, ...]]]
expected_kwargs: Optional[Sequence[Dict[str, Any]]]

...
expected_args_iter = iter(expected_args or ())
expected_kwargs_iter = iter(expected_kwargs or ())

...
for args, kwargs in itertools.zip_longest(
    expected_args_iter, expected_kwargs_iter, fillvalue={})

Where can I annotate "args" and "kwargs"?

The mypy error is

Need type annotation for "args"  [var-annotated]
Need type annotation for "kwargs"  [var-annotated]

and here is the piece of code

expected_args: Optional[Sequence[Tuple[Any, ...]]]
expected_kwargs: Optional[Sequence[Dict[str, Any]]]

...
expected_args_iter = iter(expected_args or ())
expected_kwargs_iter = iter(expected_kwargs or ())

...
for args, kwargs in itertools.zip_longest(
    expected_args_iter, expected_kwargs_iter, fillvalue={})

Where can I annotate "args" and "kwargs"?

Share Improve this question edited Jan 21 at 17:00 Sujay asked Jan 20 at 6:58 SujaySujay 3512 silver badges9 bronze badges 3
  • That's really weird... there is no way to annotate the iteration variables of a list comprehension. mypy shouldn't be telling you to do that. (Also None, with a comma is probably not what you want there, but fixing that doesn't make the "Need type annotation" messages go away.) – user2357112 Commented Jan 20 at 7:04
  • @user2357112 None, was used to set the default value in function definition. Sorry for the confusion, corrected it in my question. – Sujay Commented Jan 20 at 7:08
  • There is no list comprehension in your question, please clarify. There's no way to annotate inside listcomp, but it is possible with regular loop (variables can be annotated anywhere before the loop body). – STerliakov Commented Jan 20 at 23:21
Add a comment  | 

1 Answer 1

Reset to default 2

This appears to be a bug in mypy v1.14.0. For some reason, any iteration over itertools.zip_longest() with an empty sequence* in fillvalue will cause a similar issue. This is the simplest example I could construct:

import itertools

seq_a: list
seq_b: list

for a, b in itertools.zip_longest(seq_a, seq_b, fillvalue=[]): ...

This is specific to both mypy and empty sequences. Non-empty sequences, non-sequences, and using Pyright are all fine.

In your particular case, you can work around this issue by leaving fillvalue as its default (None) and hard-coding an exception into your comprehension.

pretty_unused_args = [
    ', '.join(itertools.chain(
        (repr(a) for a in args) if args is not None else [],
        ('%s=%r' % kwarg for kwarg in kwargs.items()) if kwargs is not None else []))
    for args, kwargs in itertools.zip_longest(
        expected_args_iter, expected_kwargs_iter)
]

*Besides (), but including tuple()

发布评论

评论列表(0)

  1. 暂无评论