Skip to content

importorskip: support use as a context manager to keep type information. - #15463

Open
Meeth-webdev wants to merge 9 commits into
pytest-dev:mainfrom
Meeth-webdev:skip_to_import_type_fix
Open

Meeth-webdev wants to merge 9 commits into
pytest-dev:mainfrom
Meeth-webdev:skip_to_import_type_fix

Conversation

@Meeth-webdev

@Meeth-webdev Meeth-webdev commented Nov 26, 2026 •

Copy link
Copy Markdown

What changed

pytest.importorskip() can now be called without a module name to get a
context manager:

with pytest.importorskip():
    import requests
    from requests import RequestException

Because the user writes the import statements, type checkers see the real
module types instead of Any, and from imports work.

  • reason and exc_type are supported in this form, with the same defaults
    as before. Warnings are ignored around the body, like the function form.
  • minversion is not supported in this form, since no module name is
    available. The overloads reject minversion without modname for type
    checkers, so there is no runtime check.
  • The existing 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

  • Added TestImportOrSkipContextManager in testing/test_runner.py:
    missing module skips, present module runs the body, ImportError
    propagates by default and skips with exc_type=ImportError, and
    module-level skip with a custom reason.
  • Added a typing check in testing/typing_checks.py.
  • mypy --strict on the issue's reproduction now reports
    Module has no attribute "getttt" instead of treating it as Any.
  • testing/test_runner.py passes, and ruff, mypy and the applicable
    pre-commit hooks pass (zizmor skipped locally, it needs Python 3.11+).
  • I have not built the docs locally (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 minversion in 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-by trailer on the commits.

Meeth-webdev and others added 2 commits November 26, 2026 19:07
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]>
@pyba-chronographer pyba-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Nov 26, 2026

@RonnyPfannschmidt RonnyPfannschmidt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🤖 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 the yield so 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, ImportError propagates by default and skips with exc_type=ImportError, module-level skip with reason, typing check. test_skip_is_not_chained and test_import_error_integration add nothing.
  • from None isn't a decision: the function form raises outside the except, 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

Meeth-webdev and others added 2 commits November 27, 2026 15:26
- 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 RonnyPfannschmidt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🤖 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 minversion without modname TypeError branch 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 TypeError line to match the current diff.

Generated by Claude Code

Meeth-webdev and others added 4 commits November 27, 2026 16:17
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]>
@Meeth-webdev

Copy link
Copy Markdown
Author

Hi @RonnyPfannschmidt can you PTAL all the checks are passed , and the comments are addressed .

Thanks !!

@RonnyPfannschmidt RonnyPfannschmidt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🤖 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

@Meeth-webdev

Copy link
Copy Markdown
Author

Hi @The-Compiler can you PTAL whenever you are free . Thanks !!

@RonnyPfannschmidt

Copy link
Copy Markdown
Member

@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

@Meeth-webdev

Copy link
Copy Markdown
Author

Hi @RonnyPfannschmidt I would like to apologize for my actions , I will make sure to not repeat it 😅.

@RonnyPfannschmidt

Copy link
Copy Markdown
Member

@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

@Meeth-webdev

Copy link
Copy Markdown
Author

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 !!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make pytest.importorskip() a context manager to preserve typing

2 participants