Django AutoField

2 views
Skip to first unread message

Patrick Marchand

unread,
Nov 26, 2025, 11:50:29 AM (7 days ago) Nov 26
to Hypothesis users
So I'm trying to generate a strategy for a django model with an AutoField, but I get the following error when I do `from_model(Actor)`

```

_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
/usr/local/lib/python3.9/site-packages/hypothesis/core.py:441: in process_arguments_to_given
    search_strategy.validate()
/usr/local/lib/python3.9/site-packages/hypothesis/strategies/_internal/strategies.py:378: in validate
    self.do_validate()
/usr/local/lib/python3.9/site-packages/hypothesis/strategies/_internal/strategies.py:675: in do_validate
    self.mapped_strategy.validate()
/usr/local/lib/python3.9/site-packages/hypothesis/strategies/_internal/strategies.py:378: in validate
    self.do_validate()
/usr/local/lib/python3.9/site-packages/hypothesis/strategies/_internal/collections.py:39: in do_validate
    s.validate()
/usr/local/lib/python3.9/site-packages/hypothesis/strategies/_internal/strategies.py:378: in validate
    self.do_validate()
/usr/local/lib/python3.9/site-packages/hypothesis/strategies/_internal/strategies.py:675: in do_validate
    self.mapped_strategy.validate()
/usr/local/lib/python3.9/site-packages/hypothesis/strategies/_internal/strategies.py:378: in validate
    self.do_validate()
/usr/local/lib/python3.9/site-packages/hypothesis/strategies/_internal/lazy.py:118: in do_validate
    w.validate()
/usr/local/lib/python3.9/site-packages/hypothesis/strategies/_internal/strategies.py:378: in validate
    self.do_validate()
/usr/local/lib/python3.9/site-packages/hypothesis/strategies/_internal/strategies.py:675: in do_validate
    self.mapped_strategy.validate()
/usr/local/lib/python3.9/site-packages/hypothesis/strategies/_internal/strategies.py:378: in validate
    self.do_validate()
/usr/local/lib/python3.9/site-packages/hypothesis/strategies/_internal/collections.py:39: in do_validate
    s.validate()
/usr/local/lib/python3.9/site-packages/hypothesis/strategies/_internal/strategies.py:378: in validate
    self.do_validate()
/usr/local/lib/python3.9/site-packages/hypothesis/strategies/_internal/lazy.py:116: in do_validate
    w = self.wrapped_strategy
/usr/local/lib/python3.9/site-packages/hypothesis/strategies/_internal/lazy.py:102: in wrapped_strategy
    unwrapped_kwargs = {
/usr/local/lib/python3.9/site-packages/hypothesis/strategies/_internal/lazy.py:103: in <dictcomp>
    k: unwrap_strategies(v) for k, v in self.__kwargs.items()
/usr/local/lib/python3.9/site-packages/hypothesis/strategies/_internal/lazy.py:45: in unwrap_strategies
    result = unwrap_strategies(s.wrapped_strategy)
/usr/local/lib/python3.9/site-packages/hypothesis/strategies/_internal/lazy.py:102: in wrapped_strategy
    unwrapped_kwargs = {
/usr/local/lib/python3.9/site-packages/hypothesis/strategies/_internal/lazy.py:103: in <dictcomp>
    k: unwrap_strategies(v) for k, v in self.__kwargs.items()
/usr/local/lib/python3.9/site-packages/hypothesis/strategies/_internal/lazy.py:45: in unwrap_strategies
    result = unwrap_strategies(s.wrapped_strategy)
/usr/local/lib/python3.9/site-packages/hypothesis/strategies/_internal/lazy.py:102: in wrapped_strategy
    unwrapped_kwargs = {
/usr/local/lib/python3.9/site-packages/hypothesis/strategies/_internal/lazy.py:103: in <dictcomp>
    k: unwrap_strategies(v) for k, v in self.__kwargs.items()
/usr/local/lib/python3.9/site-packages/hypothesis/strategies/_internal/lazy.py:45: in unwrap_strategies
    result = unwrap_strategies(s.wrapped_strategy)
/usr/local/lib/python3.9/site-packages/hypothesis/strategies/_internal/lazy.py:106: in wrapped_strategy
    base = self.function(*self.__args, **self.__kwargs)
/usr/local/lib/python3.9/site-packages/hypothesis/extra/django/_impl.py:115: in from_model
    field_strategies[name] = from_field(field)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

field = <django.db.models.fields.AutoField: actor_id>

    def from_field(field: F) -> st.SearchStrategy[Union[F, None]]:
        """Return a strategy for values that fit the given field.
   
        This function is used by :func:`~hypothesis.extra.django.from_form` and
        :func:`~hypothesis.extra.django.from_model` for any fields that require
        a value, or for which you passed :obj:`hypothesis.infer`.
   
        It's pretty similar to the core :func:`~hypothesis.strategies.from_type`
        function, with a subtle but important difference: ``from_field`` takes a
        Field *instance*, rather than a Field *subtype*, so that it has access to
        instance attributes such as string length and validators.
        """
        check_type((dm.Field, df.Field), field, "field")
        if getattr(field, "choices", False):
            choices = []  # type: list
            for value, name_or_optgroup in field.choices:
                if isinstance(name_or_optgroup, (list, tuple)):
                    choices.extend(key for key, _ in name_or_optgroup)
                else:
                    choices.append(value)
            # form fields automatically include an empty choice, strip it out
            if "" in choices:
                choices.remove("")
            min_size = 1
            if isinstance(field, (dm.CharField, dm.TextField)) and field.blank:
                choices.insert(0, "")
            elif isinstance(field, (df.Field)) and not field.required:
                choices.insert(0, "")
                min_size = 0
            strategy = st.sampled_from(choices)
            if isinstance(field, (df.MultipleChoiceField, df.TypedMultipleChoiceField)):
                strategy = st.lists(st.sampled_from(choices), min_size=min_size)
        else:
            if type(field) not in _global_field_lookup:
                if getattr(field, "null", False):
                    return st.none()
>               raise ResolutionFailed("Could not infer a strategy for %r", (field,))
E               hypothesis.errors.ResolutionFailed: ('Could not infer a strategy for %r', (<django.db.models.fields.AutoField: actor_id>,))

/usr/local/lib/python3.9/site-packages/hypothesis/extra/django/_fields.py:315: ResolutionFailed

```

This is the class
```


class Actor(models.Model):
    """Actor (Person or Organization)"""

    class Meta:
        verbose_name = _("actor")
        verbose_name_plural = _("actors")

    actor_id = models.AutoField(primary_key=True)

    @property
    def is_person(self):
        return hasattr(self, 'person')

    @property
    def is_organization(self):
        return hasattr(self, 'organization')

    def get_person(self):
        if self.is_person:
            return self.person
        return Person.objects.none()

    def get_organization(self):
        if self.is_organization:
            return self.organization
        return Organization.objects.none()

    def __str__(self):
        if self.is_person:
            return self.get_person().__str__()
        elif self.is_organization:
            return self.get_organization().__str__()
        else:
            return "Unknown Actor: %i" % self.actor_id

```

https://hypothesis.readthedocs.io/en/latest/reference/strategies.html#hypothesis.extra.django.from_model says that no strategy is inferred for AutoField, so why am I getting this error ? Shouldnt it just let the DB create it ?

Thanks

Patrick Marchand

unread,
Nov 26, 2025, 12:36:21 PM (7 days ago) Nov 26
to Hypothesis users
Seems like this was fixed by https://github.com/HypothesisWorks/hypothesis/pull/3984 I'm running hypothesis-5.49.0, so I figure it should be okay, but maybe I'm missing something.

Patrick Marchand

unread,
Nov 26, 2025, 12:58:48 PM (7 days ago) Nov 26
to Hypothesis users
Nevermind, I still had hypothesis[django] as a dependency and it was holding hypothesis back. Now that I'm on version 6 it works.
Reply all
Reply to author
Forward
0 new messages