Skip to content

PLATFORM-5905 - Make AttributeError handling more precise#25

Merged
andrsdt merged 2 commits intomasterfrom
fix/PLATFORM-5905_make_attribute_error_handling_more_precise
Apr 1, 2025
Merged

PLATFORM-5905 - Make AttributeError handling more precise#25
andrsdt merged 2 commits intomasterfrom
fix/PLATFORM-5905_make_attribute_error_handling_more_precise

Conversation

@andrsdt
Copy link
Copy Markdown
Contributor

@andrsdt andrsdt commented Jan 14, 2025

PLATFORM-5905

💡 Context

When Pypendency fails to load a module with an AttributeError, it assumes that it was because the DI file didn’t have a load() method. This isn’t always the case. For example, this code:

def load(container_builder: ContainerBuilder):
    container_builder.set_definition(
        Definition('example.A', 'tests.resources.class_a.A')
    )

    x = 42
    x.append(1)

Would raise an exception like this one:

Resource 'test_di_with_generic_attribute_error.py' requires loader method

When the problem is really that the method append doesn't exist in numbers.

📖 Summary

This PR improves the error handling mechanism by raising the unknown AttributeError in case it isn't caused by a missing load function

⚡️Performance Considerations

Negligible. Adds a string check when the module loading fails.

🧪 How should this be manually tested?

  • Copy the src/pypendency folder and paste it the src/ folder of a project that uses Pypendency. This will be enough for "patching" it
  • Modify any python dependency injection file and add these changes inside the load method:
x = 42
x.append(1)
  • Start the project and check that the error raised now is the AttributeError pointing at the append(), instead of mistakenly saying that the load() function is missing

@andrsdt andrsdt self-assigned this Jan 14, 2025
@andrsdt andrsdt requested a review from a team as a code owner January 14, 2025 12:39
@andrsdt andrsdt requested review from acrusella and kb05 and removed request for a team and acrusella January 14, 2025 12:39
Comment thread src/pypendency/loaders/py_loader.py Outdated
@andrsdt andrsdt requested a review from kb05 April 1, 2025 10:00
Copy link
Copy Markdown

@kb05 kb05 left a comment

Choose a reason for hiding this comment

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

image
🚀 🎸

@andrsdt andrsdt merged commit 781018e into master Apr 1, 2025
3 checks passed
andrsdt added a commit that referenced this pull request Apr 7, 2025
@andrsdt andrsdt deleted the fix/PLATFORM-5905_make_attribute_error_handling_more_precise branch April 7, 2025 09:10
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