Skip to content

Commit 639f272

Browse files
authored
Optimize CallableType.formal_arguments (#11543)
Don't use a generator, since they tend to be slow when compiled. This method came up as a hot spot when profiling compiled mypy.
1 parent 56a6bf7 commit 639f272

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

mypy/types.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from typing import (
99
Any, TypeVar, Dict, List, Tuple, cast, Set, Optional, Union, Iterable, NamedTuple,
10-
Sequence, Iterator
10+
Sequence
1111
)
1212
from typing_extensions import ClassVar, Final, TYPE_CHECKING, overload, TypeAlias as _TypeAlias
1313

@@ -1168,14 +1168,15 @@ def max_possible_positional_args(self) -> int:
11681168
return sys.maxsize
11691169
return sum([kind.is_positional() for kind in self.arg_kinds])
11701170

1171-
def formal_arguments(self, include_star_args: bool = False) -> Iterator[FormalArgument]:
1171+
def formal_arguments(self, include_star_args: bool = False) -> List[FormalArgument]:
11721172
"""Yields the formal arguments corresponding to this callable, ignoring *arg and **kwargs.
11731173
11741174
To handle *args and **kwargs, use the 'callable.var_args' and 'callable.kw_args' fields,
11751175
if they are not None.
11761176
11771177
If you really want to include star args in the yielded output, set the
11781178
'include_star_args' parameter to 'True'."""
1179+
args = []
11791180
done_with_positional = False
11801181
for i in range(len(self.arg_types)):
11811182
kind = self.arg_kinds[i]
@@ -1186,11 +1187,14 @@ def formal_arguments(self, include_star_args: bool = False) -> Iterator[FormalAr
11861187

11871188
required = kind.is_required()
11881189
pos = None if done_with_positional else i
1189-
yield FormalArgument(
1190+
arg = FormalArgument(
11901191
self.arg_names[i],
11911192
pos,
11921193
self.arg_types[i],
1193-
required)
1194+
required
1195+
)
1196+
args.append(arg)
1197+
return args
11941198

11951199
def argument_by_name(self, name: Optional[str]) -> Optional[FormalArgument]:
11961200
if name is None:

0 commit comments

Comments
 (0)