Skip to content

improve diagnostics for possibly-unbound call-dunder with union #940

@carljm

Description

@carljm

When we have an implicit dunder call with a union type, where some types in the union implement the dunder and others do not, we currently report an error that the dunder methods are possibly unbound: https://play.ty.dev/e412c8da-cc72-47b5-af98-9db45800ff73

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

This is not very useful in identifying which element of the union type is the problem. It would be better if we would instead surface the underlying error directly (that NotContext does not implement __enter__ or __exit__), and add a note that NotContext appears in the union type Context | NotContext.

This issue applies at least to sync and async context managers; I think it likely applies to some other implicit dunder calls as well.

Metadata

Metadata

Assignees

Labels

callsIssues relating to call-signature checking and diagnosticsdiagnosticsRelated to reporting of diagnostics.

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions