generated from pyiron/pyiron_module_template
-
Notifications
You must be signed in to change notification settings - Fork 0
Add import parser to deal with local imports #158
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
Open
samwaseda
wants to merge
15
commits into
main
Choose a base branch
from
import_parser
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+76
−2
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
a68639d
add import parser to deal with local imports
samwaseda 9e662ad
Make it more complicated
samwaseda ded7935
ruff
samwaseda 68cb97d
Update flowrep/models/parsers/import_parser.py
samwaseda 0c8a01a
ruff mypy
samwaseda 19f69d0
black
samwaseda bd628d7
Fail hard when importlib fails
samwaseda da36e9a
Merge remote-tracking branch 'origin' into import_parser
samwaseda e058027
correct import statement
samwaseda 612f769
Merge remote-tracking branch 'origin' into import_parser
samwaseda 15d384f
Merge remote-tracking branch 'origin' into import_parser
samwaseda 7c26842
use scope.register instead of creating an extra argument
samwaseda 3ede435
Update flowrep/models/parsers/import_parser.py
samwaseda 1070021
Implement Liam's suggestion (also corecting typo)
samwaseda 0312459
I'm a loss
samwaseda File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import ast | ||
| import importlib | ||
|
|
||
|
|
||
| def build_scope( | ||
| imports: list[ast.Import] | None = None, | ||
| import_froms: list[ast.ImportFrom] | None = None, | ||
| ) -> dict: | ||
samwaseda marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """ | ||
| Build a scope dictionary from a list of `import` and `from ... import ...` statements. | ||
|
|
||
| Args: | ||
| imports (list | None): A list of `ast.Import` nodes. | ||
| import_froms (list | None): A list of `ast.ImportFrom` nodes. | ||
|
|
||
| Returns: | ||
| dict: A dictionary representing the scope with imported modules and objects. | ||
| """ | ||
| scope = {} | ||
|
|
||
| imports = imports or [] | ||
| import_froms = import_froms or [] | ||
|
|
||
| # Handle `import` statements | ||
| for imp in imports: | ||
| for alias in imp.names: | ||
| asname = alias.asname or alias.name | ||
| module = importlib.import_module(alias.name) | ||
| scope[asname] = module | ||
|
|
||
| # Handle `from ... import ...` statements | ||
| for imp_from in import_froms: | ||
| level = imp_from.level | ||
samwaseda marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # Dynamically import the module (absolute or relative) | ||
| if imp_from.module is None or level > 0: | ||
| raise ValueError( | ||
| f"Relative imports are not supported in dependency parsing. " | ||
| f"Encountered importing from {imp_from.module}." | ||
| ) | ||
| module = importlib.import_module(imp_from.module) | ||
| for alias in imp_from.names: | ||
| name = alias.name | ||
| asname = alias.asname or name | ||
| obj = getattr(module, name) | ||
| scope[asname] = obj | ||
|
|
||
| return scope | ||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.