Skip to content

Safer treatment of @runtime protocols in isinstance() #3827

@ilevkivskyi

Description

@ilevkivskyi

Currently, isinstance with protocols uses the same logic as with nominal classes, i.e. only erases the type variables, but sometimes this may be not safe (as discussed in PEP 544), for example:

@runtime
class P(Protocol):
    attr: int

class C:
    attr = 'Hi!'

if isinstance(C(), P):
    # this branch is considered unreachable by mypy, but will be executed

The idea is to perform a "deeper" erasure (setting all attribute types to Any) when considering isinstance() with protocols, thus mimicking the runtime behaviour.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy