Skip to content

Typing: av.open() as context manager to populate variables for later use results in 'possibly unbound' #2053

@jdarena66

Description

@jdarena66

Problem: False positive typechecker error

MWE:

import av

with av.open('somefile') as cont:
    stream = cont.streams.video[0]

s = stream  # typechecker error: possibly unbound  (assuming pyright)

The issue seems that the typechecker doesn't know whether an exception will be raised that both (a) prevents variable assignment and (b) is later suppressed in the course of the context manager's execution. This suppression of a raised error could make any variables that were created by assignments in the context manager unbound, so the typechecker is trying to be conservative.

Proposed solution

The typing spec mentions that typecheckers must be able to distinguish between context managers that can suppress exceptions from those that can't; the spec says that the context manager's __exit__ method return type is used for this purpose; to signal that the context manager will not suppress exceptions (which, judging by av/container/core.pyx, is the case for with av.open()...) the __exit__ method type should not be bool or Literal[True]. The Cython code in core.pyx seems to show the actual return type is None, but the specified return type in core.pyi for Container.__exit__() is bool.

Can we change the Container.__exit__ return type in core.pyi from bool to None? Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions