On Tue, Nov 30, 2021 at 09:17:13AM +0000, Paul Moore wrote:
> Also, related to the question Terry raised, IMO it would be useful to
> have a clear statement on code that *does* use type annotations, but
> violates them at runtime. To be specific, is the following considered
> as an error?
>
> >>> def muladd(x: int, y: int, z: int) -> int:
> ... return x * (y+z)
> ...
> >>> muladd(3.1459, 87.33, 2.7e2)
> 1124.124447
Isn't that just duck-typing? You've got a function that is documented as
taking arguments of one type (int), but actually will work with any
numeric type (and some non-numeric types) that quacks like an int.
muladd(2, 'ab', 'cd') # returns 'abcdabcd'
If you replaced the type annotations with a docstring that listed the
parameters x, y, z as int, would duck-typing be wrong? Maybe.
I don't think we can make any concrete claims here. Sometimes
duck-typing is perfectly fine. Sometimes its not. It depends on the
function's implementation and its semantics. Sometimes calling a
function with a duck-typed value seems to work fine but the result is
meaningless junk. (Garbage In, Garbage Out.)
I guess all we can say is that the Python language is agnostic and
neutral on this matter.
--
Steve
_______________________________________________
Python-Dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/W24VGDUH6F43C6VT4OUGERNEZFW223MP/
Code of Conduct: http://python.org/psf/codeofconduct/