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

Cast top_path to Path in walk_modules function. #138

Merged
merged 4 commits into from
Oct 21, 2024

Conversation

simonjharris
Copy link
Contributor

In some cases, the top_path variable is being set with a /./ in the path. It means that the string manipulation fails to extract the correct module name, and can lead to errors like the one below.

This PR casts the top_path to aPosixPath to match the base_path type. This strips out the additional /./ and means that the module name can be successfully extracted.

E.g.
top_path = "/var/task/./alembic_utils"

  File "/var/task/./alembic_utils/replaceable_entity.py", line 314, in compare_registered_entities
    if entity.__class__ not in registry.allowed_entity_types:
  File "/var/task/./alembic_utils/replaceable_entity.py", line 216, in allowed_entity_types
    return set(collect_subclasses(alembic_utils, ReplaceableEntity))
  File "/var/task/./alembic_utils/experimental/_collect_instances.py", line 78, in collect_subclasses
    for module_ in walk_modules(module):
  File "/var/task/./alembic_utils/experimental/_collect_instances.py", line 47, in walk_modules
    module = importlib.import_module(module_import_path)
  File "/var/lang/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 972, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 984, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'embic_utils

@@ -24,7 +24,7 @@ def walk_modules(module: ModuleType) -> Generator[ModuleType, None, None]:
# ...
"""
top_module = module
top_path = top_module.__path__[0] # type: ignore
top_path = PosixPath(top_module.__path__[0])
Copy link
Owner

Choose a reason for hiding this comment

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

Why is this PosixPath instead of Path?

won't that block windows support?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure. I don't have a Windows machine to test it on. But from my debugging, base_path is already a PosixPath--or do you think that is platform-dependent?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looks like you're right @olirice. Updated to use Path.

@simonjharris simonjharris force-pushed the cast-top-path-to-posixpath branch from 44a9c45 to b6bcc4c Compare October 18, 2024 09:16
@simonjharris simonjharris changed the title Cast top_path to PosixPath in walk_modules function. Cast top_path to Path in walk_modules function. Oct 18, 2024
@olirice olirice self-requested a review October 21, 2024 16:13
@olirice olirice merged commit e0a3bf8 into olirice:master Oct 21, 2024
1 of 13 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