Skip to content

report an instance file that cannot be read as a usage error - #753

Closed
qianeric-backup wants to merge 1 commit into
python-jsonschema:mainfrom
qianeric-backup:fix/report-unreadable-instance-file
Closed

qianeric-backup wants to merge 1 commit into
python-jsonschema:mainfrom
qianeric-backup:fix/report-unreadable-instance-file

Conversation

@qianeric-backup

Copy link
Copy Markdown

BinaryFileInput.check_can_open() stats the path to decide how to open it, and BinaryFileInputParam.convert() calls it without a guard, so a path that does not exist (or is a directory) escapes as an unhandled exception instead of the click usage error that other bad option values get:

$ check-jsonschema --schemafile schema.json does-not-exist.yaml
Traceback (most recent call last):
  File ".../click/core.py", line 2593, in invoke
  ...
  File ".../check_jsonschema/file_wrapper.py", line 38, in check_can_open
    if not stat.S_ISFIFO(os.stat(self.name).st_mode):
FileNotFoundError: [Errno 2] No such file or directory: 'does-not-exist.yaml'

Compare an invalid --output-format, which is reported and exits 2:

$ check-jsonschema --schemafile schema.json -o bogus file.yaml
Usage: python -m check_jsonschema [OPTIONS] INSTANCEFILES...
Error: Invalid value for '-o' / '--output-format': 'bogus' is not one of ...

Change

convert() catches OSError from check_can_open() and reports it with the self.fail() helper that the rest of the module already uses:

$ check-jsonschema --schemafile schema.json does-not-exist.yaml ; echo $?
Usage: python -m check_jsonschema [OPTIONS] INSTANCEFILES...
Error: Invalid value for 'INSTANCEFILES...': 'does-not-exist.yaml' cannot be read: [Errno 2] No such file or directory: 'does-not-exist.yaml'
2
case before after
instance file missing traceback, exit 1 Invalid value for 'INSTANCEFILES...', exit 2
instance file is a directory IsADirectoryError traceback, exit 1 same usage error, exit 2
instance file readable validated unchanged
instance file is - (stdin) reads stdin unchanged (no stat performed)
invalid --output-format usage error unchanged

Tests

test_missing_instance_file_is_a_usage_error in tests/unit/cli/test_parse.py. pytest tests/unit here: 341 passed / 4 failed with the change, 340 passed / 5 failed on the current tree — the difference is the new test; the other 4 (test_supports_common_option[cmd_args0], test_color_cli_option[...]) also fail on the current tree in this sandbox.

`BinaryFileInput.check_can_open()` stats the path to decide how to open it, and
`BinaryFileInputParam.convert()` calls it without a guard, so a path that does
not exist (or is a directory) escapes as an unhandled exception instead of the
`click` usage error that other bad option values get:

    $ check-jsonschema --schemafile schema.json does-not-exist.yaml
    Traceback (most recent call last):
      File ".../click/core.py", line 2593, in invoke
      ...
      File ".../check_jsonschema/file_wrapper.py", line 38, in check_can_open
        if not stat.S_ISFIFO(os.stat(self.name).st_mode):
    FileNotFoundError: [Errno 2] No such file or directory: 'does-not-exist.yaml'

Compare an invalid `--output-format`, which is reported and exits 2:

    $ check-jsonschema --schemafile schema.json -o bogus file.yaml
    Usage: python -m check_jsonschema [OPTIONS] INSTANCEFILES...
    Error: Invalid value for '-o' / '--output-format': 'bogus' is not one of ...

## Change

`convert()` catches `OSError` from `check_can_open()` and reports it with the
`self.fail()` helper that the rest of the module already uses:

    $ check-jsonschema --schemafile schema.json does-not-exist.yaml ; echo $?
    Usage: python -m check_jsonschema [OPTIONS] INSTANCEFILES...
    Error: Invalid value for 'INSTANCEFILES...': 'does-not-exist.yaml' cannot be read: [Errno 2] No such file or directory: 'does-not-exist.yaml'
    2

| case | before | after |
|---|---|---|
| instance file missing | traceback, exit 1 | `Invalid value for 'INSTANCEFILES...'`, exit 2 |
| instance file is a directory | `IsADirectoryError` traceback, exit 1 | same usage error, exit 2 |
| instance file readable | validated | unchanged |
| instance file is `-` (stdin) | reads stdin | unchanged (no stat performed) |
| invalid `--output-format` | usage error | unchanged |

## Tests

`test_missing_instance_file_is_a_usage_error` in `tests/unit/cli/test_parse.py`.
`pytest tests/unit` here: 341 passed / 4 failed with the change, 340 passed /
5 failed on the current tree - the difference is the new test, the other 4
(`test_supports_common_option[cmd_args0]`, `test_color_cli_option[...]`) fail on
the current tree too in this sandbox.
@sirosen

sirosen commented Nov 23, 2026

Copy link
Copy Markdown
Member

This PR violates our AI policy.

Please read and take time to digest and understand the policy. Sending us LLM outputs without any investment of real effort is actively harmful to the project maintainers and therefore harmful to the project.

@sirosen sirosen closed this Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants