Skip to content

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

Closed
@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