Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom overrites for package resolving and optional sys.path support #601

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from

Conversation

spezifant
Copy link

Motivation

Certain build systems or configurations have Python packages located in a non-default path. Those are not found by Codegen per default.

Content

Codegen now considers the PYTHONPATH environment variable when resolving packages.

Testing

Added unit tests which resolve packages only when the PYTHONPATH is set in a correct way.

Please check the following before marking your PR as ready for review

  • I have added tests for my changes
  • I have updated the documentation or added new documentation as needed

@spezifant spezifant requested review from codegen-team and a team as code owners February 21, 2025 15:06
@CLAassistant
Copy link

CLAassistant commented Feb 21, 2025

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
3 out of 4 committers have signed the CLA.

✅ spezifant
✅ tomcodgen
✅ christinewangcw
❌ Matthias Bartelt


Matthias Bartelt seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

Copy link
Contributor

@eacodegen eacodegen left a comment

Choose a reason for hiding this comment

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

Would it be possible for you to add two options:

  1. Make respecting sys.path opt in
  2. Add additional overrides

Example: if codegen is installed a virtual environment, we wouldn't want to make the user set PYTHONPATH/install it into the project environment. We'd also not want to accidentally include code from their virtual enviornment

Copy link
Contributor

@eacodegen eacodegen left a comment

Choose a reason for hiding this comment

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

Thanks for the PR by the way! Let us know if you ever have any questions.

@bagel897 bagel897 mentioned this pull request Feb 22, 2025
2 tasks
@spezifant spezifant changed the title Check PYTHONPATH value when resolving a package Allow custom overrites for package resolving and optional sys.path support Feb 24, 2025
# =====[ Check if we are importing an entire file with custom resolve path or sys.path enabled ]=====
if len(self.ctx.config.py_resolve_overrides) > 0 or self.ctx.config.py_resolve_syspath:
# Handle resolve overrides first if both is set
resolve_paths: list[str] = self.ctx.config.py_resolve_overrides + (sys.path if self.ctx.config.py_resolve_syspath else [])
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we refactor this to it's own function?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think its fine as is?

Copy link
Author

Choose a reason for hiding this comment

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

Both would be perfectly fine for me.

@@ -107,6 +108,13 @@ def resolve_import(self, base_path: str | None = None, *, add_module_name: str |
if file := self.ctx.get_file(filepath):
Copy link
Contributor

Choose a reason for hiding this comment

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

If we do have a sys.path/override configured, can we make sure to use that path to resolve imports in case of conflicts? Like if we have a/b.py and b.py and a path of ["a", ""], we should resolve the import from a if sys path/overrides are enabled.

Copy link
Author

Choose a reason for hiding this comment

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

Changed the code order and added a dedicated test.

# =====[ Check if we are importing an entire file with custom resolve path or sys.path enabled ]=====
if len(self.ctx.config.py_resolve_overrides) > 0 or self.ctx.config.py_resolve_syspath:
# Handle resolve overrides first if both is set
resolve_paths: list[str] = self.ctx.config.py_resolve_overrides + (sys.path if self.ctx.config.py_resolve_syspath else [])
Copy link
Contributor

Choose a reason for hiding this comment

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

I think its fine as is?

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.

7 participants