Skip to content

Protocols for types with fields that can be verified #6656

Open
@quodlibetor

Description

@quodlibetor
$ pip freeze | rg mypy
mypy-extensions==0.4.1
mypy-mypyc==0.670

I have some complex objects that can be in one of several states once they have been verified, and I'd like to represent that verification as different types, but I'm having a hard time getting it to work with protocols.

I believe that something like this should work:

from __future__ import annotations
from typing import NamedTuple, Protocol, Optional

class Simple:
    two: Optional[int]

    def as_simpl_verified(self) -> SimpleVerified:
        assert self.two is not None
        return self

class SimpleVerified(Protocol):
    two: int

but I get this error:

$ mypy example_simple.py
example_simple.py:11: error: Incompatible return value type (got "Simple", expected "SimpleVerified")
example_simple.py:11: note: Following member(s) of "Simple" have conflicts:
example_simple.py:11: note:     two: expected "int", got "Optional[int]

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions