Repository navigation
importorskip: support use as a context manager to keep type information. - #15463
Meeth-webdev wants to merge 9 commits into
Conversation
Calling pytest.importorskip() without a module name now returns a context manager, so imports are written as normal import statements and type checkers see the real module types instead of Any. Fixes pytest-dev#15448
Credit AI assistance for the importorskip context manager change (pytest-dev#15448): the implementation, tests, docs and changelog were written with Claude Code and reviewed by the author. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
RonnyPfannschmidt
left a comment
There was a problem hiding this comment.
🤖 Written by Claude Opus 5.5 via Claude Code for the pytest maintainers; I prompted it, it did the work, I read it.
The API shape matches what was agreed in #15448. The volume around it doesn't, please trim it.
- The function form imports inside
warnings.catch_warnings()+simplefilter("ignore"). Do the same around theyieldso both forms behave alike. - Docs: one short docstring example, a few lines in
skipping.rst, a 2-3 line changelog without a code block. It's written out three times now. - Tests: missing skips, present runs body,
ImportErrorpropagates by default and skips withexc_type=ImportError, module-level skip withreason, typing check.test_skip_is_not_chainedandtest_import_error_integrationadd nothing. from Noneisn't a decision: the function form raises outside theexcept, so no chaining there either. Drop that section.versionadded:: 9.3: main targets 10.0, please check.- The naming question in #15448 is still open; mixed API is fine to start, not settled.
Generated by Claude Code
- Ignore warnings around the yield, like the function form, and raise Skipped outside the except block (no exception chaining). - Trim docstring, docs and changelog. - Reduce tests to the essential cases. - Use versionadded 10.0. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
RonnyPfannschmidt
left a comment
There was a problem hiding this comment.
🤖 Written by Claude Opus 5.5 via Claude Code for the pytest maintainers; I prompted it, it did the work, I read it.
This addresses the review, the code looks good now. Two small things left:
- codecov/patch fails because the
minversionwithoutmodnameTypeErrorbranch lost its test. The overloads already reject that call for type checkers, so I'd drop the runtime check rather than add the test back. - The PR description is out of date: please remove the "Decision for reviewers" section and update the test list and the
TypeErrorline to match the current diff.
Generated by Claude Code
The overloads already reject minversion without modname for type checkers, and the reviewer prefers less code here. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
The previous Read the Docs build was cancelled manually. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
|
Hi @RonnyPfannschmidt can you PTAL all the checks are passed , and the comments are addressed . Thanks !! |
RonnyPfannschmidt
left a comment
There was a problem hiding this comment.
🤖 Written by Claude Opus 5.5 via Claude Code for the pytest maintainers; I prompted it, it did the work, I read it.
Looks good now, all points addressed. The empty "Trigger docs build" commit goes away when squashing.
Generated by Claude Code
|
Hi @The-Compiler can you PTAL whenever you are free . Thanks !! |
|
@Meeth-webdev this is the weekend, a review request already is a platform native "please take a look" its kind of rude to double down after that |
|
Hi @RonnyPfannschmidt I would like to apologize for my actions , I will make sure to not repeat it 😅. |
|
@Meeth-webdev please stay excited tho - just be chill on the issue tracker. now that the day passed i also feel like i was a bit rough on you just for being excited (which is a good thing) the last few months of ai slop bots/farms have been quite a fight against the windmills |
|
Hi @RonnyPfannschmidt , I actually want to fight against the AI slop as well by using AI responsibly , and I totally get the frustration from your side as well because of the recent AI slop . I am excited to contribute to pytest because its a module which I have used for my past projects , I am really looking forward to making the module better and learn something new 😊. Thanks !! |
What changed
pytest.importorskip()can now be called without a module name to get acontext manager:
Because the user writes the
importstatements, type checkers see the realmodule types instead of
Any, andfromimports work.reasonandexc_typeare supported in this form, with the same defaultsas before. Warnings are ignored around the body, like the function form.
minversionis not supported in this form, since no module name isavailable. The overloads reject
minversionwithoutmodnamefor typecheckers, so there is no runtime check.
importorskip("name")form is unchanged.@overloads give type checkers the right return type for each form.As discussed in #15448, the naming question is still open; this mixed
function/context-manager API is a starting point.
Closes #15448.
Verification
TestImportOrSkipContextManagerintesting/test_runner.py:missing module skips, present module runs the body,
ImportErrorpropagates by default and skips with
exc_type=ImportError, andmodule-level skip with a custom
reason.testing/typing_checks.py.mypy --stricton the issue's reproduction now reportsModule has no attribute "getttt"instead of treating it asAny.testing/test_runner.pypasses, and ruff, mypy and the applicablepre-commit hooks pass (
zizmorskipped locally, it needs Python 3.11+).tox -e docs).AI assistance
I used Claude Code for this change. It wrote the implementation, tests,
docs and changelog, based on a design we worked out together (overloads,
no
minversionin the context manager form). I reviewed the changes,ran the tests locally, and can explain the code. My understanding of the
design is shown in #15448 (comment). AI use is credited in the
Co-authored-bytrailer on the commits.