Skip to content

Commit

Permalink
backsupport py310
Browse files Browse the repository at this point in the history
  • Loading branch information
calad0i committed Dec 2, 2023
1 parent 2b550e1 commit 7398640
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ version = "0.2.0b2"
authors = [{ name = "Chang Sun", email = "[email protected]" }]
description = "High Granularity Quantizarion"
readme = "README.md"
requires-python = ">=3.11,<3.12"
requires-python = ">=3.10,<3.12"
license = { file = "LICENSE" }

classifiers = [
Expand Down
2 changes: 2 additions & 0 deletions src/HGQ/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
__version__ = "unknown version"
version_tuple = (0, 0, "unknown version")

from . import _patch


class Shutup:
def write(self, s):
Expand Down
40 changes: 40 additions & 0 deletions src/HGQ/_patch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
def patch_singledispatch():
"Patch functools.singledispatch to support Union types. Invoked only for python<=3.10"
import sys
if sys.version_info >= (3, 11):
print("singledispatch patch not needed for python>=3.11")
return
import functools
from functools import singledispatch as _singledispatch
from types import UnionType

def singledispatch(func):
func = _singledispatch(func)
_register = func.register

def register(cls, func=None):
from typing import get_type_hints
if isinstance(cls, type):
return _register(cls, func)
if isinstance(cls, UnionType):
def register_seq(func):
for t in cls.__args__:
_register(t, func)
return func
return register_seq

func = cls
argname, cls = next(iter(get_type_hints(func).items()))
if isinstance(cls, type):
return _register(cls, func)
if isinstance(cls, UnionType):
for t in cls.__args__:
func = _register(t, func)
return func

func.register = register # type: ignore
return func
setattr(functools, 'singledispatch', singledispatch)


patch_singledispatch()

0 comments on commit 7398640

Please sign in to comment.