Skip to content

Mypy failing to understand ProgressBar and contextlib.nullcontext() are both context managers #16892

Closed as not planned
@jamesbraza

Description

@jamesbraza

Bug Report

Mypy 1.8 seems to be failing to combine prompt-toolkit's ProgressBar with contextlib.nullcontext() into a context manager object.

To Reproduce

import contextlib

from prompt_toolkit.shortcuts import ProgressBar

debug_print: bool = True
cm = ProgressBar() if debug_print else contextlib.nullcontext()
reveal_type(cm)  # Revealed type is "builtins.object"
with cm:
    pass

Expected Behavior

No type errors

Actual Behavior

a.py:7:13: note: Revealed type is "builtins.object"
a.py:8:6: error: "object" has no attribute "__enter__"  [attr-defined]
a.py:8:6: error: "object" has no attribute "__exit__"  [attr-defined]

Your Environment

  • Mypy version used: 1.8
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.12.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-join-v-unionUsing join vs. using unions

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions