Skip to content

CliRunner.invoke raises a "ValueError: I/O operation on closed file" #3110

@getzze

Description

@getzze

Hi,
I am a maintainer of subliminal that uses click for its CLI. Some tests for the CLI are failing since the upgrade to click 8.3.0.
I tracked the failures to a regression in click 8.3.0 due to #2991. Reverting this commit makes my tests pass with click 8.3.0.

The failing pytest tests are of the following form:

def test_cli_download(tmp_path: os.PathLike[str]) -> None:
    runner = CliRunner()

    result = runner.invoke(subliminal_cli, ['download', '-l', 'en', '-p', 'podnapisi', 'video.mkv'])

    # Curiously this test is not failing, subtitle is even downloaded
    assert result.exit_code == 0

    # The following two asserts are failing because `result.output` show a long error message.
    assert result.output.startswith('Collecting videos')
    assert result.output.endswith('Downloaded 1 subtitle\n')

When I only run this test function with pytest, all the assertions pass, but when I run the whole file that has plenty of test functions with the same structure, all the functions fail at the result.output assert (although not consistently...)

The error message in result.output is like this:

--- Logging error ---
Traceback (most recent call last):
  File "/usr/lib/python3.13/logging/__init__.py", line 1154, in emit
    stream.write(msg + self.terminator)
    ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
ValueError: I/O operation on closed file.
[...]
  File "src/subliminal/score.py", line 157, in compute_score
    logger.debug('Found matches %r', matches)
Message: 'Found matches %r'
Arguments: ({'country', 'episode', 'season', 'series', 'year'},)

It seems the logging files are closed after one CliRunner.invoke call and its giving rise to failures in the other tests.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions