Skip to content

AppContext.__exit__ type annotation doesn't allow for None #4474

@twm

Description

@twm

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

flask/src/flask/ctx.py

Lines 269 to 272 in 8122973

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions