Skip to content

Fix test_flags_missing_selector in tests/test_generator.py - #75

Closed
byMeysaM wants to merge 1 commit into
openzim:mainfrom
byMeysaM:fix-argparse-test-ci-pytest
Closed

byMeysaM wants to merge 1 commit into
openzim:mainfrom
byMeysaM:fix-argparse-test-ci-pytest

Conversation

@byMeysaM

@byMeysaM byMeysaM commented Nov 25, 2026 •

Copy link
Copy Markdown

Summary

Fixes the failing "test_flags_missing_selector" test in "tests/test_generator.py".

hatch_test

The test previously expected an "argparse.ArgumentError", but "argparse" raises "SystemExit" with exit code "2" when a required mutually exclusive argument group is missing.

Updated the test to correctly assert "SystemExit" and verify the exit code.

Testing

  • Full test suite: 64 passed
  • Black: passed
  • Ruff: passed
  • Pyright: 0 errors, 0 warnings, 0 informations
fix-argparse-test-ci-pytest

Fixes #74

@benoit74

Copy link
Copy Markdown
Contributor

Why only "related" to #74? Isn't this fixing the issue (so should be Fix #74, or something like this so PR and issue are properly linked and PR merging causes the issue to automatically close)?

@byMeysaM
byMeysaM marked this pull request as ready for review November 28, 2026 05:43
@byMeysaM byMeysaM changed the title Fix missing selector test Fix test_flags_missing_selector in tests/test_generator.py Nov 28, 2026
@byMeysaM byMeysaM closed this Nov 28, 2026
@byMeysaM
byMeysaM deleted the fix-argparse-test-ci-pytest branch November 28, 2026 07:02
@byMeysaM
byMeysaM restored the fix-argparse-test-ci-pytest branch November 28, 2026 07:03
@byMeysaM byMeysaM reopened this Nov 28, 2026
@benoit74
benoit74 self-requested a review November 30, 2026 10:39
@benoit74

benoit74 commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Thanks for digging into this, but I don't think there's a bug here — the test passes on main for every Python version this project supports, and this patch would actually break it.

devdocs2zim pins requires-python = ">=3.12,<3.13", and CI resolves its interpreter from that via python-version-file: pyproject.toml. The behaviour you're describing changed in Python 3.12: a batch of self.error(...) calls inside _parse_known_args — including the missing-required-mutually-exclusive-group check — were refactored into raise ArgumentError(None, ...), so they now honour exit_on_error=False instead of calling parser.error() and exiting.

Python parse_args([]) with exit_on_error=False
3.11.17 SystemExit: 2
3.12.15 argparse.ArgumentError
3.13.16 argparse.ArgumentError
3.14.7 argparse.ArgumentError

So the existing assertRaises(argparse.ArgumentError, ...) is correct, and SystemExit: 2 only happens on 3.11 — below our floor. Applying this patch on top of main and running on 3.12.15:

FAILED tests/test_generator.py::TestDocFilter::test_flags_missing_selector
E   argparse.ArgumentError: one of the arguments --all --slug --first is required

Whereas main unmodified gives 64 passed, matching the green Tests run on main.

My guess is pytest ran against a 3.11 outside the managed env, or on an older hatch. Could you share hatch --version and hatch run python -V?

Correcting something I said in an earlier version of this comment: I claimed hatch will use whatever interpreter it finds unless one is pinned. That's not right on current hatch. I checked — hatch 1.18.1 resolves the env interpreter from requires-python and selects 3.12.15 here even with no pin at all (and it ignores .python-version outright). On top of that, uv pip install -e '.[test]' on 3.11 is refused at resolution time:

Because only devdocs2zim[test]==0.2.2.dev0 is available and the current Python version (3.11.17)
does not satisfy Python>=3.12,<3.13, ...

So the toolchain does already guard this, and a stray 3.11 points at an older hatch or a pytest invocation outside the env rather than at anything missing in the repo.

Closing this and #74.

@benoit74 benoit74 closed this Nov 30, 2026

@byMeysaM byMeysaM left a comment •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@benoit74
Thank you for the clarification. After looking into the CPython changes and the project's configuration, I believe I have identified the reason for the discrepancy.

versions

I tested this on Python 3.12.3, as shown in the screenshots attached to this PR. With exit_on_error=False, the test raises SystemExit: 2 in that version. I also reproduced the same behavior in a clean clone of the upstream repository.

The difference appears to be related to subsequent changes in argparse:

  • CPython Issue #121018 reported cases where exit_on_error=False did not prevent the parser from exiting.
  • CPython commit 8ea6cc1 introduced changes to additional error-handling paths, replacing calls to self.error() with ArgumentError.
  • These changes are included in Python 3.12.5, released on August 6, 2024.

This explains why Python 3.12.3 raises SystemExit, while newer versions such as 3.12.15 raise argparse.ArgumentError.

The project's pyproject.toml specifies requires-python = ">=3.12,<3.13".
The Hatch test matrix also specifies Python 3.12, and both GitHub Actions workflows use python-version-file: pyproject.toml.
Therefore, the declared supported range includes both the earlier and newer Python 3.12 releases.

So, I think the CI should either account for the different behavior across all Python 3.12 patch versions, or specify Python 3.12.5 as the minimum supported version, since that's where these changes were introduced.

Thanks again for looking into this and helping clarify the issue.

@byMeysaM
byMeysaM deleted the fix-argparse-test-ci-pytest branch December 2, 2026 00:51
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.

Fix test expectation in test_flags_missing_selector

2 participants