twm · GitHub

When a context manager exits without an exceptions the arguments to __exit__ are None, but the annotated type doesn't permit that:

def __exit__(
self, exc_type: type, exc_value: BaseException, tb: TracebackType
) -> None:
self.pop(exc_value)

I tried to pass the return value of app_context() to ExitStack.enter_context() and got this message from the Pyright type checker:

error: Argument of type "AppContext" cannot be assigned to parameter "cm" of type "AbstractContextManager" in function "enter_context"
    "AppContext" is incompatible with protocol "AbstractContextManager"
      "__exit__" is an incompatible type
        Type "(exc_type: type, exc_value: BaseException, tb: TracebackType) -> None" cannot be assigned to type "(__exc_type: Type[BaseException] | None, __exc_value: BaseException | None, __traceback: TracebackType | None) -> bool | None"
          Parameter 1: type "Type[BaseException] | None" cannot be assigned to type "type"
            Type "Type[BaseException] | None" cannot be assigned to type "type"
          Parameter 2: type "BaseException | None" cannot be assigned to type "BaseException"
            Type "BaseException | None" cannot be assigned to type "BaseException"
          Parameter 3: type "TracebackType | None" cannot be assigned to type "TracebackType" (reportGeneralTypeIssues)

Environment:

Python 3.9.5
Flask 2.0.1
Werkzeug 2.0.1

Read the original on github.com ↗