Set __file__ to the exported module path when testing notebooks#1597
Open
ncoop57 wants to merge 2 commits into
Open
Set __file__ to the exported module path when testing notebooks#1597ncoop57 wants to merge 2 commits into
ncoop57 wants to merge 2 commits into
Conversation
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…-file-in-tests # Conflicts: # nbdev/test.py # nbs/api/12_test.ipynb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Sets
__file__in the notebook execution namespace whennbdev-testruns an exported notebook, pointing at the module the notebook exports to. Path-dependent code likePath(__file__).parentnow behaves the same under test as it does in the exported module.Depends on AnswerDotAI/fastcore#835 — that PR must be merged and a fastcore release cut before this one can land.
Changes
__file__before running cells: if a notebook declares adefault_exp,test_nbsets__file__inCaptureShell.user_nsto the exported module's path beforerun_all.tests/file_var.ipynb: asserts__file__resolves tonbdev/file_var.pyand that an explicit assignment overrides the injected value; exercised fromnbs/api/12_test.ipynb.Motivation
Exported modules commonly locate resources relative to themselves via
__file__, which doesn't exist in the test namespace, so such code can't be exercised by notebook tests without workarounds. The full suite passing is meaningful coverage here: every notebook innbs/apideclares adefault_exp, so they all now run with__file__set.