0
FloatBaseT = TypeVar('FloatBaseT', bound='FloatBase')

class FloatBase(Generic[FloatBaseT], metaclass=ABCMeta):
...
@classmethod
def compose(cls: Type[FloatBaseT], sign_bin: 'bit', exponent_bin: 'bit', mantissa_bin: 'bit') -> FloatBaseT:
    sign, biased_exponent, mantissa = tuple(map(lambda x: int(x), (sign_bin, exponent_bin, mantissa_bin)))
    exponent = biased_exponent - cls._bias
    return cls(sign, exponent, mantissa)

...

class Float32(FloatBase[FloatBaseT]):...

When I use this Float32.compose method, it says:

Float32.compose(ret_sign, ret_exp, ret_mant)
Could not bind method "compose" because "type[Float32[FloatBaseT@Float32]]" 
is not assignable to parameter "cls"Type "type[Float32[FloatBaseT@Float32]]" 
cannot be assigned to type 
"type[FloatBaseT@Float32]"PylancereportGeneralTypeIssues

Is there any reason this error comes out in pylance? Is it correct to use generic class in this situation? In what circumstances do I need to use generic class?

Changing code with not to use generic class, but I've faced another issue in differnt method.

jmg2027
  • 1
  • 1
  • I think this class is probably not really generic (it's weird that its parameterized by its own type). I think you can simply omit the type annotation from `cls` arg of `classmethod`. Also mypy complains about _Missing type parameters for generic type "FloatBase"_ in your TypeVar currently. In Python 3.11 [you can use `Self` for the return value of the classmethod](https://peps.python.org/pep-0673/#use-in-classmethod-signatures). – Anentropic Aug 30 '23 at 09:48
  • Indeed. I think I’ve done this because I’m using 3.8, so that I can’t use Self annotation. Is there any solution to this? – jmg2027 Aug 31 '23 at 10:42
  • @jmg2027 you can `from typing_extensions import Self` instead as outlined [here](https://stackoverflow.com/a/71990006/14401160). (or just remove the generic like [this](https://mypy-play.net/?mypy=latest&python=3.11&gist=9bb778ec8c3813024b9524eba2c07ae0)) – STerliakov Sep 02 '23 at 20:42

0 Answers0