Skip to content

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

Open
ilevkivskyi wants to merge 7 commits into
python:masterfrom
ilevkivskyi:faster-proto-impl
Open

ilevkivskyi wants to merge 7 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

Copy link
Copy Markdown
Contributor

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

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