Clarifying the float/int/complex special case

Numeric Generics - Where do we go from PEP 3141 and present day Mypy? this thread discusses numbers related classes. There have been couple other lengthy discussions elsewhere too.

At it’s core type system relies on two mechanisms,

  1. Nominal types: This is normal subclass relation. class Bar(Foo), Bar is a subclass of Foo.
  2. Structural types: This is protocols/duck like typing. Here we define interface.

The numeric types mostly do not offer a useful nominal relationship. Basic subclass relationships don’t exist across most of them and are missing a few methods. For structural side there’s little that can be usefully said with existing abcs and adding something meaningful there breaks backward compatibility. So they’re mostly unusable classes today and fit in very badly with two type system approaches. You could invent approach 3 only for sake of numbers system, but given it’s status today would still be major lift to define reasonable rules/add stubs/hints everywhere across ecosystem.

I think main way to get something like them is to use protocols with specific methods you want (multiply/add/etc), but main friction there is most typing ecosystem is not written with that in mind. That is not a change for type checkers, but change for stubs/libraries throughout to agree on protocol(s) and use it consistently.

1 Like