Skip to content

Another two micro-optimizations #19633

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -2432,13 +2432,13 @@ def format_long_tuple_type(self, typ: TupleType) -> str:
"""Format very long tuple type using an ellipsis notation"""
item_cnt = len(typ.items)
if item_cnt > MAX_TUPLE_ITEMS:
return "tuple[{}, {}, ... <{} more items>]".format(
return '"tuple[{}, {}, ... <{} more items>]"'.format(
format_type_bare(typ.items[0], self.options),
format_type_bare(typ.items[1], self.options),
str(item_cnt - 2),
)
else:
return format_type_bare(typ, self.options)
return format_type(typ, self.options)

def generate_incompatible_tuple_error(
self,
Expand Down Expand Up @@ -2517,15 +2517,12 @@ def iteration_dependent_errors(self, iter_errors: IterationDependentErrors) -> N

def quote_type_string(type_string: str) -> str:
"""Quotes a type representation for use in messages."""
no_quote_regex = r"^<(tuple|union): \d+ items>$"
if (
type_string in ["Module", "overloaded function", "<deleted>"]
or type_string.startswith("Module ")
or re.match(no_quote_regex, type_string) is not None
or type_string.endswith("?")
):
# Messages are easier to read if these aren't quoted. We use a
# regex to match strings with variable contents.
# These messages are easier to read if these aren't quoted.
return type_string
return f'"{type_string}"'

Expand Down
37 changes: 20 additions & 17 deletions mypy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,7 @@
import sys
from abc import abstractmethod
from collections.abc import Iterable, Sequence
from typing import (
TYPE_CHECKING,
Any,
ClassVar,
Final,
NamedTuple,
NewType,
TypeVar,
Union,
cast,
overload,
)
from typing import TYPE_CHECKING, Any, ClassVar, Final, NewType, TypeVar, Union, cast, overload
from typing_extensions import Self, TypeAlias as _TypeAlias, TypeGuard

import mypy.nodes
Expand Down Expand Up @@ -1607,11 +1596,25 @@ def bound(self) -> bool:
return bool(self.items) and self.items[0].is_bound


class FormalArgument(NamedTuple):
name: str | None
pos: int | None
typ: Type
required: bool
class FormalArgument:
def __init__(self, name: str | None, pos: int | None, typ: Type, required: bool) -> None:
self.name = name
self.pos = pos
self.typ = typ
self.required = required

def __eq__(self, other: object) -> bool:
if not isinstance(other, FormalArgument):
return NotImplemented
return (
self.name == other.name
and self.pos == other.pos
and self.typ == other.typ
and self.required == other.required
)

def __hash__(self) -> int:
return hash((self.name, self.pos, self.typ, self.required))
Comment on lines +1599 to +1617
Copy link
Collaborator

Choose a reason for hiding this comment

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

Would a dataclass work here? According to the docs, the dataclass decorator is supported for native classes. https://mypyc.readthedocs.io/en/latest/native_classes.html#class-decorators

Not sure what the performance difference is though.

Copy link
Member Author

Choose a reason for hiding this comment

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

It is true that dataclasses are extension, but looking at generated C, their constructor still uses **kwargs (i.e. involves creating a dictionary, which is slow). I didn't make measurements, but I guess dataclasses will be somewhere in between.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Would slots=True work? There is also frozen which could be set to True as well.

Copy link
Member Author

Choose a reason for hiding this comment

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

No, not that dictionary :-) The one to pack keyword arguments to constructor. Anyway, just to be sure, I tried that, and (as expected) neither changed the constructor calling logic (which is there I guess because constructor is auto-generated not by us).



class Parameters(ProperType):
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-tuples.test
Original file line number Diff line number Diff line change
Expand Up @@ -1612,7 +1612,7 @@ t4: Tuple[int, int, int, int, int, int, int, int, int, int, int, int] = (1, 2, 3
t5: Tuple[int, int] = (1, 2, "s", 4) # E: Incompatible types in assignment (expression has type "tuple[int, int, str, int]", variable has type "tuple[int, int]")

# long initializer assignment with mismatched pairs
t6: Tuple[int, int, int, int, int, int, int, int, int, int, int, int] = (1, 2, 3, 4, 5, 6, 7, 8, "str", "str", "str", "str", 1, 1, 1, 1, 1) # E: Incompatible types in assignment (expression has type tuple[int, int, ... <15 more items>], variable has type tuple[int, int, ... <10 more items>])
t6: Tuple[int, int, int, int, int, int, int, int, int, int, int, int] = (1, 2, 3, 4, 5, 6, 7, 8, "str", "str", "str", "str", 1, 1, 1, 1, 1) # E: Incompatible types in assignment (expression has type "tuple[int, int, ... <15 more items>]", variable has type "tuple[int, int, ... <10 more items>]")

[builtins fixtures/tuple.pyi]

Expand Down
Loading