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

Issue: FactoryMetaClass does not define __getitem__ #53

Open
Tsdevendra1 opened this issue Sep 11, 2023 · 2 comments
Open

Issue: FactoryMetaClass does not define __getitem__ #53

Tsdevendra1 opened this issue Sep 11, 2023 · 2 comments

Comments

@Tsdevendra1
Copy link

I'm not sure if I'm using this wrong but when I define a factory such as:


class UserFactory(factory.django.DjangoModelFactory[User]):
    class Meta:
        model = User

I get the following error:

Class 'FactoryMetaClass' does not define '__getitem__', so the '[]' operator cannot be used on its instances

I understand why the error occurs, but I'm not sure what the intended solution is. Since DjangoModelFactory is generic in the stubs, what's the intended usage if DjangoModelFactory isn't actually able to support getitem? Should I monkeypatch FactoryMetaClass?

@axieum
Copy link

axieum commented Nov 13, 2023

If you're also using typeddjango/django-stubs, a workaround is to piggy-back off of their monkey-patching:

import django_stubs_ext
import factory

# Monkeypatching Django, so stubs will work for all generics, see: https://github.com/typeddjango/django-stubs
django_stubs_ext.monkeypatch(
  extra_classes=[
    # https://github.com/youtux/types-factory-boy/issues/53
    factory.base.BaseFactory,
    factory.declarations.BaseDeclaration,
    factory.declarations.Trait,
    # ...
  ]
)

@youtux
Copy link
Owner

youtux commented Dec 2, 2023

yeah it looks like this is quite a big limitation, until the upstream (django) will fix their classes to be typed and generic

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

No branches or pull requests

3 participants