Skip to content

Speed up bind_self() in edge cases involving protocols - #22319

Merged
ilevkivskyi merged 8 commits into
python:masterfrom
ilevkivskyi:faster-proto-impl
Nov 2, 2026
Merged

ilevkivskyi merged 8 commits into
python:masterfrom
ilevkivskyi:faster-proto-impl

Conversation

@ilevkivskyi

Copy link
Copy Markdown
Member

Fixes #20828

This has no effect on self-check, but improves edge cases involving self-types and protocols where currently performance is very bad. For example, altair should be ~2x faster. Two things here:

  • Use bind_self_fast() in situations where type_checker is not set (e.g. late stages of semantic analysis).
  • Add lightweight cache for cases where full bind_self() is required.

cc @JukkaL

@github-actions

This comment has been minimized.

@JukkaL JukkaL left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Daemon probably needs to clear the cache more often. Here is a fine-grained incremental test case that now fails:

[case testProtocolGenericSelfTypeBaseChanged]
import a
[file a.py]
from b import P
from d import C
def f(x: P) -> None: pass
f(C())
[file b.py]
from typing import Protocol
class P(Protocol):
    def meth(self) -> int: ...
[file c.py]
from typing import Generic, TypeVar
T = TypeVar("T")
S = TypeVar("S")
class Base(Generic[T]):
    def meth(self: "Base[S]") -> S: ...
[file d.py]
from c import Base
class C(Base[int]): pass
[file d.py.2]
from c import Base
class C(Base[str]): pass
[file d.py.3]
from c import Base
class C(Base[int]): pass
[out]
==
a.py:4: error: Argument 1 to "f" has incompatible type "C"; expected "P"
a.py:4: note: Following member(s) of "C" have conflicts:
a.py:4: note:     Expected:
a.py:4: note:         def meth(self) -> int
a.py:4: note:     Got:
a.py:4: note:         def meth(self) -> str
==

@github-actions

This comment has been minimized.

@JukkaL JukkaL left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks good now. Here are a few check-protocols.test regression tests that cover some code paths not covered by our test suite that AI came up with (optional):

[case testProtocolGenericSelfTypeInTypeArgumentBound]
from typing import Generic, Protocol, TypeVar
from b import Base

class P(Protocol):
    def meth(self) -> int: ...

class C(Base[int]): pass
class D(Base[str]): pass

PT = TypeVar("PT", bound=P)
class Box(Generic[PT]): pass

x: Box[C]
y: Box[D]  # E: Type argument "D" of "Box" must be a subtype of "P"
[file b.pyi]
from typing import Generic, TypeVar
T = TypeVar("T")
S = TypeVar("S")
class Base(Generic[T]):
    def meth(self: Base[S]) -> S: ...

[case testProtocolImplicitClassmethodSelfTypeInTypeArgumentBound]
from typing import Callable, Generic, Protocol, TypeVar
from b import C

class P(Protocol):
    def __init_subclass__(cls, item: Callable[[str], None]) -> None: ...

PT = TypeVar("PT", bound=P)
class Box(Generic[PT]): pass

# This binds __init_subclass__ with is_classmethod=False before type checking (which
# misses the error); the protocol check below must not reuse that cached binding.
x: Box[C]
def f(x: P) -> None: pass
f(C())  # E: Argument 1 to "f" has incompatible type "C"; expected "P" \
        # N: Following member(s) of "C" have conflicts: \
        # N:     Expected: \
        # N:         def __init_subclass__(cls, item: Callable[[str], None]) -> None \
        # N:     Got: \
        # N:         def __init_subclass__(cls, item: Callable[[C], None]) -> None
[file b.pyi]
from typing import Callable, TypeVar
S = TypeVar("S")
class C:
    def __init_subclass__(cls: type[S], item: Callable[[S], None]) -> None: ...

@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@ilevkivskyi
ilevkivskyi merged commit ccbf32b into python:master Nov 2, 2026
25 checks passed
@ilevkivskyi
ilevkivskyi deleted the faster-proto-impl branch November 2, 2026 18:38
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.

Runs forever at 100% CPU for generic protocol with bounded TypeVar argument

2 participants