Conversation
|
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
| if not isinstance(types, list): | ||
| typelist = list(types) | ||
| else: | ||
| typelist = cast("list[Type]", types) | ||
|
|
||
| # Fast path: most of the time there is nothing to flatten | ||
| if not any(isinstance(t, (TypeAliasType, UnionType)) for t in typelist): # type: ignore[misc] | ||
| return typelist |
There was a problem hiding this comment.
I think we only need to convert it to a list if we're taking the fast path, correct?
| if not isinstance(types, list): | |
| typelist = list(types) | |
| else: | |
| typelist = cast("list[Type]", types) | |
| # Fast path: most of the time there is nothing to flatten | |
| if not any(isinstance(t, (TypeAliasType, UnionType)) for t in typelist): # type: ignore[misc] | |
| return typelist | |
| # Fast path: most of the time there is nothing to flatten | |
| if not any(isinstance(t, (TypeAliasType, UnionType)) for t in types): # type: ignore[misc] | |
| if not isinstance(types, list): | |
| return list(types) | |
| return cast("list[Type]", types) |
There was a problem hiding this comment.
I'm first converting it to a list since iterating over a list if faster than iterating over a sequence, and since we need to return a list anyway, so it would only delay the construction of the list. Since we take the fast path the vast majority of the time, the redundant construction of a list in the slow path is not a big deal, and we save some of the cost through faster iteration. If mypyc was smarter about iterating over sequences, your suggested approach would be better.
There was a problem hiding this comment.
Thanks for the explanation!
Avoid constructing a new list if there is nothing to flatten (and the input is a list, which if often the case).