I think this would still be useful for replacing functions which
return `-> Any` currently (because of the burden of returning a Union
on callers - essentially a slightly-safer-but-still-unsafe-Any).
for example, I think it would be useful for a type checker / IDE to
produce an error on something like this:
resp = json.load(f)
print(resp.definitely_not_a_thing)
but allow any of the attributes / methods of the union of all of the
attributes / methods of the `AnyOf` type:
resp = json.load(f)
print(resp['this_is_a_thing'])
It's still unsound, but it's a strict improvement over Any
I guess for clarity in definition:
- Union[T1, T2] only allows attributes which are the _intersection_ of
those provided by T1 & T2
- AnyOf[T1, T2] allows attributes which are the _union_ of those
provided by T1 | T2
Anthony
On Wed, Sep 23, 2020 at 8:15 AM Guido van Rossum
Hm, I hadn’t thought of it that way, but you're right. But that would mean that using an Intersection type is usually a lie, right? (I.e. unsound, I believe is the term?)
On Wed, Sep 23, 2020 at 08:07 Jelle Zijlstra
wrote: El mié., 23 sept. 2020 a las 16:48, Guido van Rossum (
) escribió: So for a checker that interprets AnyOf fully, there would be no difference with Union, right? It’s just a spelling of Union with a default behavior of Any.
My understanding is that a type checker should accept an operation on a value of type `AnyOf[A, B]` if it is valid on either A or B, but an operation on a value of type `Union[A, B]` is acceptable only if it is valid on both A and B. So `len(x)` where `x: AnyOf[str, int]` would be valid.
That does sound similar to `Intersection`, now that I think of it.
Because clearly it can’t be meant to mean Intersection, right?
Still, the example of float**float concerns me. It returns AnyOf[float, complex] but the default should be float. Your proposed notation doesn’t have a way to specify that. (Another fallback could be the join of all the types, but that would be complex, which is undesirable for float**float for pragmatic reasons.)
—Guido
On Wed, Sep 23, 2020 at 02:13 Sebastian Rittau
wrote: Am 22.09.20 um 18:39 schrieb dimvar--- via Typing-sig:
Re: the use of AnyOf for typing.
I agree with the folks saying that it's probably not a good idea to introduce an unsafe union. In the cases where more type safety than just using Any is desired, one can use an ordinary union, and the callers of the function can cast the result to their desired type.
Unfortunately that is not true as the many examples in the originally
mentioned ticket prove. Experience has shown that safe unions are
inconvenient as return types in many cases, especially when typing
previously untyped code. Unsafe unions provide a substantial type safety
benefit over the only alternative of using Any.
- Sebastian
_______________________________________________
Typing-sig mailing list -- typing-sig@python.org
To unsubscribe send an email to typing-sig-leave@python.org
https://mail.python.org/mailman3/lists/typing-sig.python.org/
Member address: guido@python.org
-- --Guido (mobile)
_______________________________________________
Typing-sig mailing list -- typing-sig@python.org
To unsubscribe send an email to typing-sig-leave@python.org
https://mail.python.org/mailman3/lists/typing-sig.python.org/
Member address: jelle.zijlstra@gmail.com
-- --Guido (mobile) _______________________________________________ Typing-sig mailing list -- typing-sig@python.org To unsubscribe send an email to typing-sig-leave@python.org https://mail.python.org/mailman3/lists/typing-sig.python.org/ Member address: asottile@umich.edu