Skip to content

Do not explicitly reset PYTHONPATH for two tests#430

Merged
Erotemic merged 1 commit intopyutils:mainfrom
s-t-e-v-e-n-k:do-not-explicitly-reset-pythonpath
Apr 9, 2026
Merged

Do not explicitly reset PYTHONPATH for two tests#430
Erotemic merged 1 commit intopyutils:mainfrom
s-t-e-v-e-n-k:do-not-explicitly-reset-pythonpath

Conversation

@s-t-e-v-e-n-k
Copy link
Copy Markdown
Contributor

Two testcases in test_explicit_profile explicitly reset PYTHONPATH to just the current working directory, whereas other testcases in the same file make sure to respect the existing PYTHONPATH.

@Erotemic
Copy link
Copy Markdown
Member

Erotemic commented Apr 8, 2026

I don't see how this is causing problems. The PYTHONPATH modified is local to these tests, and it does not persist afterward. What is the use case where appending the existing PYTHONPATH is needed?

So first you need to convince me that this is needed. If it is, then there are also other issues with the patch. You are assuming a linux environment using the colon as a separator. You should be using os.pathsep. You also assume that PYTHONPATH exists in the dictionary, which it may not.

@s-t-e-v-e-n-k
Copy link
Copy Markdown
Contributor Author

If line_profiler is not located in the default sys.path when those two test cases are run, then they will fail:

[   92s] _____________ test_explicit_profile_ignores_inherited_owner_marker _____________
[   92s] 
[   92s]     def test_explicit_profile_ignores_inherited_owner_marker():
[   92s]         """
[   92s]         Standalone runs should not be blocked by an inherited owner marker.
[   92s]         """
[   92s]         with tempfile.TemporaryDirectory() as tmp:
[   92s]             temp_dpath = ub.Path(tmp)
[   92s]             env = os.environ.copy()
[   92s]             env['LINE_PROFILE'] = '1'
[   92s]             env['LINE_PROFILER_OWNER_PID'] = str(os.getpid() + 100000)
[   92s]             env['PYTHONPATH'] = os.getcwd()
[   92s]     
[   92s]             with ub.ChDir(temp_dpath):
[   92s]                 script_fpath = ub.Path('script.py')
[   92s]                 script_fpath.write_text(_demo_explicit_profile_script())
[   92s]     
[   92s]                 args = [sys.executable, os.fspath(script_fpath)]
[   92s]                 proc = ub.cmd(args, env=env)
[   92s]                 print(proc.stdout)
[   92s]                 print(proc.stderr)
[   92s] >               proc.check_returncode()
[   92s] 
[   92s] tests/test_explicit_profile.py:166: 
[   92s] _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
[   92s] 
[   92s] self = {'out': '', 'err': 'Traceback (most recent call last):\n  File "/tmp/tmpwrpbh7p8/script.py", line 1, in <module>\n    ...en: returncode: 1 args: ['/usr/bin/python3.11', 'script.py']>, 'cwd': None, 'command': '/usr/bin/python3.11 script.py'}
[   92s] 
[   92s]     def check_returncode(self) -> None:
[   92s]         """Raise CalledProcessError if the exit code is non-zero."""
[   92s]         import subprocess
[   92s]     
[   92s]         if self.returncode:
[   92s] >           raise subprocess.CalledProcessError(
[   92s]                 self.returncode, self.args, self.stdout, self.stderr
[   92s]             )
[   92s] E           subprocess.CalledProcessError: Command '['/usr/bin/python3.11', 'script.py']' returned non-zero exit status 1.
[   92s] 
[   92s] /usr/lib/python3.11/site-packages/ubelt/util_cmd.py:174: CalledProcessError
[   92s] ----------------------------- Captured stdout call -----------------------------
[   92s] 
[   92s] Traceback (most recent call last):
[   92s]   File "/tmp/tmpwrpbh7p8/script.py", line 1, in <module>
[   92s]     from line_profiler import profile
[   92s] ModuleNotFoundError: No module named 'line_profiler'
[   92s] 

Our build environment has the module installed in a path that isn't included in sys.path by default. I'm happy to modify the PR if you do wish to proceed.

@Erotemic
Copy link
Copy Markdown
Member

Erotemic commented Apr 9, 2026

Ok, that makes more sense now. I think something like this will be more robust:

env['PYTHONPATH'] = os.pathsep.join(
    [p for p in [os.getcwd(), env.get('PYTHONPATH')] if p]
)

Make those changes, and I'll run the workflow and merge.

Two testcases in test_explicit_profile explicitly reset PYTHONPATH to
just the current working directory, whereas other testcases in the same
file make sure to respect the existing PYTHONPATH.
@s-t-e-v-e-n-k s-t-e-v-e-n-k force-pushed the do-not-explicitly-reset-pythonpath branch from 471c275 to df3979e Compare April 9, 2026 03:34
@s-t-e-v-e-n-k
Copy link
Copy Markdown
Contributor Author

Make those changes, and I'll run the workflow and merge.

Done!

@codecov
Copy link
Copy Markdown

codecov bot commented Apr 9, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.02%. Comparing base (60e928f) to head (df3979e).
⚠️ Report is 35 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #430      +/-   ##
==========================================
+ Coverage   87.56%   89.02%   +1.45%     
==========================================
  Files          18       20       +2     
  Lines        1641     2250     +609     
  Branches      348      473     +125     
==========================================
+ Hits         1437     2003     +566     
- Misses        149      184      +35     
- Partials       55       63       +8     

see 4 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 3dd2008...df3979e. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Erotemic Erotemic merged commit 492a8c4 into pyutils:main Apr 9, 2026
57 checks passed
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