-
Notifications
You must be signed in to change notification settings - Fork 234
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Should int be compatible with float and mypy's @ducktype decorator #48
Comments
There's also the number module which defines ABCs corresponding to complex, float, int (named Complex, Real, Integral -- this was introduced in PEP 3141). So while int isn't a subclass of float, it's a subclass of number.Real. And we worked hard in Python 3 to make sure that an int is always acceptable where a float is expected, and an int or float is acceptable where a complex is expected. (This is why int division changed!) IOW I agree that we should somehow make sure that the type checker supports this. And I think it's acceptable despite the lack of int.hex(). (Maybe it's a bug?) I'm not sure that @ducktype is the right term, but I'm out of imagination. I think it should only be supported in stub modules. I think UserString is a losing proposition; the 3.x docs say it's deprecated. I'm not sure what to do about mocks -- they seem to open a whole different can of worms that I'd rather keep closed. |
I think there's a very limited set of such relationships that isn't extensible, so maybe instead of an extensible feature like
Are there any other such cases? Is there any reason a third-party library might want to add any? If you do want such a feature, maybe |
I recently removed mypy's What about promoting from Another similar issue is |
I like promoting from bytearray to bytes; can we also promote from memoryview to bytes? Maybe we can even fix I propose not to bother with Decimal -- it intentionally (if awkwardly) lives outside the numeric tower defined by PEP 3141. We'll see if the users push back. |
For what it's worth, I found it surprising that when I created a @dataclass(frozen=True)
class Position:
x: float
y: float
|
@johnthagen mypy considers |
Mypy treats
int
as compatible withfloat
(andfloat
as compatible withcomplex
) even though the types aren't ordinary subtypes of each other. This is implemented via a@ducktype
class decorator that allows specifying that a class should be treated essentially as a subclass of another class, even though there is no concrete subclass relationship and even if the interfaces aren't actually fully compatible. The mypy stub forint
looks like this (a bit simplified):This could also be useful for things like
UserString
(which is likestr
but not a subclass) and mock classes in tests.At the least, we should specify
int
as compatible withfloat
so that people won't have to write code like this:Of course, all stubs for built-in modules would have to use unions everywhere for floating point arguments for the above to work in practice, and I'd like to avoid that as it seems like extra complexity with little benefit. The above would not let use some
float
methods for values with the union type (such ashex()
which is only defined forfloat
objects but notint
objects).If using the
ducktype
approach (or special casing some classes to be compatible and not makingducktype
public) allfloat
methods would be available, but they might fail at runtime if the actual object is anint
. I think this is reasonable since these methods are pretty rarely used.As a bonus, the
ducktype
decorator would effectively make all classes abstract, hopefully helping address the abstract vs. concrete type problem (see #12, for example).The text was updated successfully, but these errors were encountered: