Skip to content

[mypyc] add support for contextlib.contextmanager - #22267

Open
hauntsaninja wants to merge 8 commits into
python:masterfrom
hauntsaninja:context
Open

hauntsaninja wants to merge 8 commits into
python:masterfrom
hauntsaninja:context

Conversation

@hauntsaninja

@hauntsaninja hauntsaninja commented Oct 22, 2026 •

Copy link
Copy Markdown
Collaborator

Should make self check like 6-10% faster and torch 8-12% faster (compiled, clean cache, not parallel)

Fixes mypyc/mypyc#905

Co-authored by Codex

@hauntsaninja
hauntsaninja marked this pull request as ready for review October 22, 2026 04:10
@hauntsaninja
hauntsaninja requested a review from JukkaL October 22, 2026 04:42
@ilevkivskyi

Copy link
Copy Markdown
Member

I guess you can say that this fixes mypyc/mypyc#905, right? (Btw I didn't look in details, @JukkaL would be a better reviewer here.)

Comment thread mypyc/irbuild/statement.py Resolved
Comment thread mypyc/irbuild/statement.py Resolved
Comment on lines +1288 to +1295
wrapped_call = mypy.nodes.CallExpr(
mypy.nodes.MemberExpr(expr.callee, "__wrapped__"),
expr.args,
expr.arg_kinds,
expr.arg_names,
)
wrapped_call.line = line
gen = builder.accept(wrapped_call)

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.

i think this results in a generic Python attribute access + generic function call at runtime which we could optimize for compiled functions decorated with @contextmanager. for those we could call the generated C function directly.

assert isinstance(expr.callee, mypy.nodes.MemberExpr)
receiver = builder.accept(receiver_expr)
method = builder.py_get_attr(receiver, expr.callee.name, line)
wrapped = builder.py_get_attr(method, "__wrapped__", line)

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.

for non-final methods we should check and optimize only if __wrapped__ actually exists because it's legal to override a contextmanager method with a regular method in a subclass.

we might be able to optimize the call for final methods and for methods of compiled classes that don't allow interpreted subclasses by calling the C function directly.

Comment thread mypyc/test-data/run-functions.test Resolved
Comment thread mypyc/test-data/run-functions.test Resolved
Comment thread mypyc/test-data/run-functions.test Resolved
Comment thread mypyc/test-data/run-functions.test Resolved
@hauntsaninja

Copy link
Copy Markdown
Collaborator Author

Thanks for the review, will address!

I used prints instead of runtime asserts because the exception logic is actually kind of tricky and was a source of bugs for me, very easy to accidentally suppress an exception

I couldn't see an easy way to find the generated C function. Codex had a diff where it did something that created some new statics, it benchmarked only a little bit better, but you're right that it could be a little more robust. Let me investigate it.

@JukkaL

JukkaL commented Nov 12, 2026

Copy link
Copy Markdown
Collaborator

This is great, and the performance impact is amazing! I've wondered for a long time how much speeding contextmanager would help mypy performance.

There are still a few issues that I found using a coding agent. Inheritance seems to regress. Example that now fails at runtime:

from contextlib import contextmanager, _GeneratorContextManager
from typing import Iterator

class Base:
    @contextmanager
    def cm(self) -> Iterator[int]:
        yield 1

class Derived(Base):
    def cm(self) -> _GeneratorContextManager[int]:
        return super().cm()

def use(x: Base) -> int:
    with x.cm() as v:
        return v

print(use(Derived()))

Another example with inheritance that regresses:

import functools
from contextlib import contextmanager
from typing import Any, Callable, Iterator, TypeVar, cast

F = TypeVar("F", bound=Callable[..., Any])

def logged(f: F) -> F:
    @functools.wraps(f)
    def wrapper(*args: Any, **kwargs: Any) -> Any:
        print("wrapper called")
        return f(*args, **kwargs)
    return cast(F, wrapper)

class Base:
    @contextmanager
    def cm(self) -> Iterator[int]:
        yield 1

class Derived(Base):
    @logged
    @contextmanager
    def cm(self) -> Iterator[int]:
        yield 2

def use(x: Base) -> int:
    with x.cm() as v:
        return v

print(use(Derived()))

Also overriding in an interpreted subclass can fail at runtime:

# Compiled module
from contextlib import contextmanager
from typing import Iterator
from mypy_extensions import mypyc_attr

def arg() -> int:
    print("argument evaluated")
    return 5

@mypyc_attr(allow_interpreted_subclasses=True)
class Base:
    @contextmanager
    def cm(self, n: int) -> Iterator[int]:
        yield n

def use(x: Base) -> int:
    with x.cm(arg()) as v:
        return v

Now an interpreted class doesn't work as expected:

# Interpreted
from foo import Base, use

class Derived(Base):
    def cm(self, n):
        print("override called")
        return super().cm(n)

print(use(Derived()))

Finally, the optimization may not work for methods defined in stubs, since the contextmanager usage may be a lie:

import turtle

def use() -> None:
    with turtle.fill():  # Doesn't actually use @contextmanager at runtime
        print("in body")

try:
    use()
except AttributeError as e:
    print("AttributeError:", e)
except Exception as e:
    print("expected failure without a display:", type(e).__name__)

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.

Specialize contextlib.contextmanager

4 participants