Skip to content

Handling of Any within an Intersection #1

Closed
@CarliJoy

Description

@CarliJoy

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 become Any: 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

Arguments against

An Intersection containing Any becomes Any

Arguments in favour

Arguments against

  • @mikeshardmind argues that this reduces the effectiveness of gradual typing by discarding constraints.

Disallow Any in Intersection

Arguments in favour

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

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

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

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions