Skip to content

Conversation

julzt0244
Copy link

@julzt0244 julzt0244 commented Sep 8, 2025

Currently factory_boy only supports proper return type hints only when using the .create() method. This PR is meant to address the type hint that gets inferred when calling the factory class itself, and this works fine for VSCode. With this change, the concrete type provided is inferred instead of "NoReturn" which is caused by the raise errors.FactoryError('You cannot instantiate BaseFactory') in BaseFactory.__new__

This is the smallest drop-in fix that doesn't require larger changes to the codebase. One could possibly create a similar annotation on the subclass Factory as well, and that might be more "correct" since BaseFactory's __new__ is technically never actually reached in typical (compliant) code as the subclass Factory is using the custom metaclass FactoryMetaClass which overloads the __new__ and __call__ functions.

Before changes:
After Changes

After changes:
Before Changes

Possible points of exploration:

  • Perhaps Pycharm needs something else eg type hinting at the __call__ level of certain classes

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.

1 participant