Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork34.3k
gh-146369: EnsurePYTHON_LAZY_IMPORTS=none overrides__lazy_modules__#146371
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:main
Are you sure you want to change the base?
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1088,6 +1088,49 @@ def test_env_var_lazy_imports_none_disables_all_lazy(self): | ||
| self.assertEqual(result.returncode, 0, f"stderr: {result.stderr}") | ||
| self.assertIn("EAGER", result.stdout) | ||
| def test_cli_lazy_imports_none_disables_dunder_lazy_modules(self): | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. You can merge the two tests since they have code in common: deftest_cli_disable_lazy_imports(self):code=textwrap.dedent(""" import sys __lazy_modules__ = ["json"] import json if 'json' in sys.modules: print("EAGER") else: print("LAZY") """)# -X lazy_imports=none should override __lazy_modules__result=subprocess.run( [sys.executable,"-X","lazy_imports=none","-c",code],capture_output=True,text=True, )self.assertEqual(result.returncode,0,f"stderr:{result.stderr}")self.assertIn("EAGER",result.stdout)# PYTHON_LAZY_IMPORTS=none should override __lazy_modules__env=dict(os.environ,PYTHON_LAZY_IMPORTS="none")result=subprocess.run( [sys.executable,"-c",code],capture_output=True,text=True,env=env, )self.assertEqual(result.returncode,0,f"stderr:{result.stderr}")self.assertIn("EAGER",result.stdout) MemberAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. The only common code is the example in They are testing two different scenarios, so separate cases are fine. It's similar to the preceding two tests, which also share Or we could use subtests? Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more.
It's just a suggestion, feel free to ignore it. Your PR was already approved ;-) | ||
| """-X lazy_imports=none should override __lazy_modules__.""" | ||
| code = textwrap.dedent(""" | ||
| import sys | ||
| __lazy_modules__ = ["json"] | ||
| import json | ||
| if 'json' in sys.modules: | ||
| print("EAGER") | ||
| else: | ||
| print("LAZY") | ||
| """) | ||
| result = subprocess.run( | ||
| [sys.executable, "-X", "lazy_imports=none", "-c", code], | ||
| capture_output=True, | ||
| text=True, | ||
| ) | ||
| self.assertEqual(result.returncode, 0, f"stderr: {result.stderr}") | ||
| self.assertIn("EAGER", result.stdout) | ||
| def test_env_var_lazy_imports_none_disables_dunder_lazy_modules(self): | ||
| """PYTHON_LAZY_IMPORTS=none should override __lazy_modules__.""" | ||
| code = textwrap.dedent(""" | ||
| import sys | ||
| __lazy_modules__ = ["json"] | ||
| import json | ||
| if 'json' in sys.modules: | ||
| print("EAGER") | ||
| else: | ||
| print("LAZY") | ||
| """) | ||
| import os | ||
| env = os.environ.copy() | ||
| env["PYTHON_LAZY_IMPORTS"] = "none" | ||
| result = subprocess.run( | ||
| [sys.executable, "-c", code], | ||
| capture_output=True, | ||
| text=True, | ||
| env=env, | ||
| ) | ||
| self.assertEqual(result.returncode, 0, f"stderr: {result.stderr}") | ||
| self.assertIn("EAGER", result.stdout) | ||
| def test_cli_overrides_env_var(self): | ||
| """Command-line option should take precedence over environment variable.""" | ||
| # PEP 810: -X lazy_imports takes precedence over PYTHON_LAZY_IMPORTS | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Ensure ``-X lazy_imports=none``` and ``PYTHON_LAZY_IMPORTS=none``` override | ||
| ``__lazy_modules__``. Patch by Hugo van Kemenade. |
Uh oh!
There was an error while loading.Please reload this page.