From e34de65480b2da631d18435272dacd8a69105b41 Mon Sep 17 00:00:00 2001 From: Maarten ter Huurne Date: Mon, 20 Mar 2023 18:01:55 +0100 Subject: [PATCH 1/2] Fix error message of `UrlsafeTokenField`'s `factory` arg check --- model_utils/fields.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/model_utils/fields.py b/model_utils/fields.py index e04ada2b..216ec857 100644 --- a/model_utils/fields.py +++ b/model_utils/fields.py @@ -347,7 +347,7 @@ def __init__(self, editable=False, max_length=128, factory=None, **kwargs): """ if factory is not None and not isinstance(factory, Callable): - raise TypeError("'factory' should either be a callable not 'None'") + raise TypeError("'factory' should either be a callable or 'None'") self._factory = factory kwargs.pop('default', None) # passing default value has not effect. From 6c5ed66ef29dd96d9b04350267cda7cbf1b8eaab Mon Sep 17 00:00:00 2001 From: Maarten ter Huurne Date: Mon, 20 Mar 2023 18:37:49 +0100 Subject: [PATCH 2/2] Use `callable()` builtin function over `isinstance(..., Callable)` This is slightly more compact, but the main motivation is to work around the following mypy bug: https://github.com/python/mypy/issues/3060 --- model_utils/fields.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/model_utils/fields.py b/model_utils/fields.py index 216ec857..10e22fa3 100644 --- a/model_utils/fields.py +++ b/model_utils/fields.py @@ -1,7 +1,6 @@ import secrets import uuid import warnings -from collections.abc import Callable from django.conf import settings from django.core.exceptions import ValidationError @@ -346,7 +345,7 @@ def __init__(self, editable=False, max_length=128, factory=None, **kwargs): non-callable value for factory is not supported. """ - if factory is not None and not isinstance(factory, Callable): + if factory is not None and not callable(factory): raise TypeError("'factory' should either be a callable or 'None'") self._factory = factory