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.
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
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
NotContextdoes not implement__enter__or__exit__), and add a note thatNotContextappears in the union typeContext | 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.