python / python/mypy

Feature request: Provide a type constructor that derives a Protocol from another type

Open
#7,894 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature priority-1-normal topic-protocols
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Please provide more information to help us understand the issue:

class Foo:
  num: int = 1
  msg: str = "hello"

FooProtocol = protocol(Foo, include_private=False)

FooLookalike = NamedTuple("FooLookalike", [("num", int), ("msg", str)])

# ok
maybe_foo: FooProtocol = FooLookalike(num=2, msg="bye")

# ok
maybe_foo = Foo()

# safely cast when using Fakes in a test
test_foo(cast(Foo, maybe_foo))

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue provides a Python example for deriving a Protocol from Foo and checking FooLookalike and Foo instances against it. No files, tests, or entry points are named, so first locate the existing Protocol and type-constructor handling; done means the shown assignments type-check and the cast example remains safe.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.