Skip to content

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

Closed

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!

Activity

  1. added a commit that references this issue on Dec 12, 2025
    ca85ad9
  2. added a commit that references this issue on Dec 12, 2025
    340b40e
  3. changed the title [-]Using av.open() as context manager to populate variables for later use results in 'possibly unbound' at use time[/-] [+]Typing: av.open() as context manager to populate variables for later use results in 'possibly unbound'[/+] on Dec 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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