[ty] Extend support for more detailed diagnostics on possibly unbound errors from implicit dunder calls against unions. by lerebear · Pull Request #24676 · astral-sh/ruff (original) (raw)

@astral-sh-bot Bot added the ty

Multi-file analysis & type inference

label

Apr 16, 2026

@lerebear lerebear changed the titleComplete support for more detailed diagnostics on possibly unbound errors from implicit dunder calls against unions. [ty] Complete support for more detailed diagnostics on possibly unbound errors from implicit dunder calls against unions.

Apr 16, 2026

lerebear added a commit that referenced this pull request

Apr 20, 2026

@lerebear

…m context manager dunder methods invoked on a union. (#24662)

Summary

As part of astral-sh/ty#940, this helps us emit more specific diagnostics for possibly unbound context manager dunders (e.g., __enter__, __exit__) invoked on a union type.

Where previously the following snippet would produce just the top-level diagnostic commented below:

class Context:
    def __enter__(self): ...
    def __exit__(self, *args): ...

class NotContext:
    pass

def _(x: Context | NotContext):
    # error: [invalid-context-manager] "Object of type `Context | NotContext` cannot be used with `with` because the methods `__enter__` and `__exit__` are possibly unbound"
    with x:
        pass

We will now produce two further "info" sub-diagnostics:

info: `NotContext` does not implement `__enter__`
info: `NotContext` does not implement `__exit__`

Approach

Test Plan

Please see updated mdtests and associated snapshots.

Base automatically changed from lerebear/push-tuskvpoxpzyx to main

April 20, 2026 18:40

lerebear

@lerebear lerebear marked this pull request as ready for review

April 21, 2026 14:07

charliermarsh

@lerebear lerebear deleted the lerebear/push-ypypplmwkqpy branch

April 24, 2026 16:14

@lerebear lerebear changed the title[ty] Complete support for more detailed diagnostics on possibly unbound errors from implicit dunder calls against unions. [ty] Extend support for more detailed diagnostics on possibly unbound errors from implicit dunder calls against unions.

Apr 24, 2026

carljm added a commit to tamird/ruff that referenced this pull request

Apr 27, 2026

@carljm

This was referenced

Apr 27, 2026

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters

[ Show hidden characters]({{ revealButtonHref }})