Skip to content

Instantly share code, notes, and snippets.

@tjpalmer
Last active October 2, 2024 21:31
Show Gist options
  • Save tjpalmer/ae73f8c465f42d23614af86a70b3b576 to your computer and use it in GitHub Desktop.
Save tjpalmer/ae73f8c465f42d23614af86a70b3b576 to your computer and use it in GitHub Desktop.
mypyc: Bad float type var in subtype
from abc import ABC, abstractmethod
from typing import Any, Generic, TypeVar
# from numbers import Real
T = TypeVar("T")
class JsonAdapter(ABC, Generic[T]):
@abstractmethod
def encode(self, obj: T) -> Any:
pass
@abstractmethod
def decode(self, obj: Any) -> T:
pass
class IntJsonAdapter(JsonAdapter[int]):
def encode(self, obj: int) -> int:
if not isinstance(obj, int):
raise TypeError("Object is not an int")
return obj
def decode(self, obj: Any) -> int:
return int(obj)
# class FloatJsonAdapter(JsonAdapter[float]):
# def encode(self, obj: float) -> float:
# if not isinstance(obj, float):
# raise TypeError("Object is not a Real")
# return obj
# def decode(self, obj: Any) -> float:
# return float(obj)
class FloatJsonAdapter(JsonAdapter[Any]):
def encode(self, obj: Any) -> Any:
if not isinstance(obj, float):
raise TypeError("Object is not a Real")
return obj
def decode(self, obj: Any) -> Any:
return float(obj)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment