KEMBAR78
Do not use dictionary in CallableType by ilevkivskyi · Pull Request #19580 · python/mypy · GitHub
Skip to content

Conversation

ilevkivskyi
Copy link
Member

This is mypyc-specific optimization: dict creation is quite slow, so we should not create a dict in the CallableType constructor. Instead, I store the required info on the relevant FuncDef object (and restore the link to definition in fixup.py). Quite surprisingly, this gives 2% speedup on my desktop.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@ilevkivskyi
Copy link
Member Author

Oh, hm, this uncovers an inconsistency in how we format constructors, depending on whether they are decorated or not. IMO

def [T] list(self) -> list[T]

looks better than

def [T] __init__(self) -> list[T]

I will see how many tests needs updating to converge on this option, and if not too many, I am going to do it in this PR.

@ilevkivskyi
Copy link
Member Author

Yeah, no, 15 tests need to be updated, this will also probably cause some noisy mypy_primer, so better to do in a separate PR. (Also maybe others have a different idea on which way looks better)

@github-actions
Copy link
Contributor

github-actions bot commented Aug 4, 2025

Diff from mypy_primer, showing the effect of this PR on open source code:

pydantic (https://github.com/pydantic/pydantic)
- pydantic/v1/validators.py:615: note:     def NamedTuple(self, str, Iterable[tuple[str, Any]], /) -> NamedTupleT
+ pydantic/v1/validators.py:615: note:     def __init__(self, str, Iterable[tuple[str, Any]], /) -> NamedTupleT
- pydantic/v1/validators.py:615: note:     def NamedTuple(self, str, None = ..., /, **kwargs: Any) -> NamedTupleT
+ pydantic/v1/validators.py:615: note:     def __init__(self, str, None = ..., /, **kwargs: Any) -> NamedTupleT

Copy link
Collaborator

@sterliakov sterliakov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LG, thanks! I personally prefer def __init__(...) by a huge margin - def ClassName(...) looks confusing, trying to untangle the latter always takes me a few extra seconds.

@ilevkivskyi ilevkivskyi merged commit 5051a48 into python:master Aug 4, 2025
20 checks passed
@ilevkivskyi ilevkivskyi deleted the tmp-mes branch August 4, 2025 12:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants