From 52fd9a7b38566f77bc672176933be87827e6678a Mon Sep 17 00:00:00 2001 From: Erez Shinan Date: Fri, 23 Aug 2024 09:13:37 +0200 Subject: [PATCH] Small fixes --- runtype/dataclass.py | 11 ++++++----- runtype/dispatch.py | 7 +------ runtype/utils.py | 2 +- 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/runtype/dataclass.py b/runtype/dataclass.py index a935d7c..8153e29 100644 --- a/runtype/dataclass.py +++ b/runtype/dataclass.py @@ -371,15 +371,16 @@ def __post_init__(self): # __init__ may have been generated by the dataclass function if hasattr(c, "__init__"): # We will add it to the dispatch - if IS_BELOW_PY_310: - # XXX Hack for old Python versions, to fix __qualname__ - c.__init__ = init_dispatcher(c.__init__.__get__(cls)) - else: - c.__init__ = init_dispatcher(c.__init__) + c.__init__ = init_dispatcher(_fix_qualname(c.__init__, cls)) else: c.__init__ = init_f return c +def _fix_qualname(f, cls): + if IS_BELOW_PY_310: + # XXX Hack for old Python versions, to fix __qualname__ + f.__qualname__ = f"{cls.__qualname__}.{f.__name__}" + return f def _dataclass_getstate(self): return [getattr(self, f.name) for f in dataclasses.fields(self)] diff --git a/runtype/dispatch.py b/runtype/dispatch.py index 7a9fe22..4547e61 100644 --- a/runtype/dispatch.py +++ b/runtype/dispatch.py @@ -50,12 +50,7 @@ def __call__(self, func=None, *, priority=None): "Must either provide a function to decorate, or set a priority" ) - if IS_BELOW_PY_310 and hasattr(func, '__self__'): - # XXX hack for old Python versions - fname = func.__self__.__qualname__ + "." + func.__name__ - func = func.__func__ - else: - fname = func.__qualname__ + fname = func.__qualname__ try: tree = self.fname_to_tree[fname] diff --git a/runtype/utils.py b/runtype/utils.py index 676e318..e21fa19 100644 --- a/runtype/utils.py +++ b/runtype/utils.py @@ -3,7 +3,7 @@ import contextvars from contextlib import contextmanager -IS_BELOW_PY_310 = sys.version_info < (3, 10) +IS_BELOW_PY_310 = True #sys.version_info < (3, 10) def get_func_signatures(typesystem, f): sig = inspect.signature(f)