Skip to content

Commit e8126ab

Browse files
committed
fix support for python 3.8 and 3.9 wrt to staticmethods
1 parent 062011c commit e8126ab

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

django_typer/__init__.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,28 @@
171171
_CACHE_KEY = "_register_typer"
172172

173173

174+
if sys.version_info < (3, 10):
175+
# todo - remove this when support for <=3.9 is dropped
176+
class static_factory(type):
177+
def __call__(self, *args, **kwargs):
178+
assert args
179+
if type(args[0]).__name__ == "staticmethod":
180+
return args[0]
181+
return super().__call__(*args, **kwargs)
182+
183+
class staticmethod(t.Generic[P, R], metaclass=static_factory):
184+
__func__: t.Callable[P, R]
185+
186+
def __init__(self, func: t.Callable[P, R]):
187+
self.__func__ = func
188+
189+
def __getattr__(self, name):
190+
return getattr(self.__func__, name)
191+
192+
def __call__(self, *args: P.args, **kwargs: P.kwargs) -> R:
193+
return self.__func__(*args, **kwargs)
194+
195+
174196
def model_parser_completer(
175197
model_cls: t.Type[Model],
176198
lookup_field: t.Optional[str] = None,
@@ -789,6 +811,11 @@ class Command(
789811
else:
790812
return Typer(**kwargs)
791813

814+
if sys.version_info < (3, 9):
815+
# this is a workaround for a bug in python 3.8 that causes multiple
816+
# values for cls error. 3.8 support is sunsetting soon so we just punt
817+
# on this one - REMOVE in next version
818+
kwargs.pop("cls", None)
792819
return super().__call__(*args, **kwargs)
793820

794821

django_typer/utils.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,10 @@ def is_method(
190190
:return: True if the function should be considered a method, False if not and None
191191
if undetermined.
192192
"""
193+
##############
194+
# Remove when python 3.8 support is dropped
195+
func_or_params = getattr(func_or_params, "__func__", func_or_params)
196+
##############
193197
if func_or_params:
194198
params = (
195199
list(inspect.signature(func_or_params).parameters)

0 commit comments

Comments
 (0)