Closed
Description
There is a great deal of confusion about handling the Any
type within an Intersection
.
In Python, Any
is both a top type (a supertype of all types), and a bottom type (a subtype of all types). Python has a gradual typing system, meaning that it will never be required that everything is typed. Everything that is not typed is considered Any
.
We examine five ways to handle intersections with Any
:
Any
is removed from intersections:T & Any = T
.- Intersections containing
Any
becomeAny
:T & Any = Any
. Any
is forbidden in intersections:T & Any
is an error.Any
is not reduced within intersections.Any
is only considered in an intersection in deference to non-gradual types.
Remove Any from Intersections
Arguments in favour
- Suggested by PEP 483.
Arguments against
- @CarliJoy shows an example where an interface disappears.
- @ippeiukai shows a similar a example where an interface disappears.
- @erictraut argues that there would need to be other special cases were this accepted.
- @erictraut analyzes this case.
An Intersection containing Any
becomes Any
Arguments in favour
- @antonagestam mentions that TypeScript does this, although he finds it surprising.
- @ippeiukai argues that this is consistent with PEP 483.
Arguments against
- @mikeshardmind argues that this reduces the effectiveness of gradual typing by discarding constraints.
Disallow Any
in Intersection
Arguments in favour
- @mikeshardmind argues that it is ambiguous.
- @mikeshardmind argues that "it is possible that
Any & T
is inherently unsafe".
Arguments against
- @randolf-scholz argues that they can arise from sequential instance-checks.
- @randolf-scholz argues that code could "synthesize an intersection type dynamically".
- @erictraut argues that "intersections with
Any
will arise, often in the bowels of the type checker's logic where there's no good way to report an error to the user".
Treat T & Any
as irreducible in general
Arguments in favour
- @NeilGirdhar argues that changing a type annotation from
T
toAny
should not cause type errors to appear, and by examining supertypes and interfaces. - @randolf-scholz argues from the various definitions of
Any
. - @JelleZijlstra reasons about setting and getting a variable.
- @erictraut reasons by duality with union.
- @mikeshardmind reasons by subtyping in a related thread.
- @mniip argues by interpreting
Any
as a wildcard in gradual typing. - @kmillikin seems to agree.
Arguments against
- at least @CarliJoy has no idea how that should work in the reference, how should a type checker handle it in detail?
- @DiscordLiz and @mikeshardmind each argued that functionally this has all the same issues as treating it as Any
Any
is only considered in an intersection in deference to non-gradual types.
Arguments for
- @mikeshardmind reasoned that this prevents gradual typing from becoming gradual untyping due to intersection being a widening of interfaces and that in cases where the wider type is desired, it is still directly expressible but requires being explicit about it.
Arguments against
- @NeilGirdhar argues that it breaks logical consistency with unions, which are the dual of intersections.
⚠️ Rules for contribution to this Issue
- Do not post, if your argument is already handled within the description
- Posts should include include a proper header i.e.
## Arguments in favour to Disallow Any in Intersection
- If you want to correct or specify things in the description write a comment in the format "Old" > "New"
The general idea is that I will update the description, allowing the discussion to be included in the PEP and prevent a discussion going in circles.
I will react with 🚀 once I included something in the description.
Metadata
Metadata
Assignees
Labels
No labels