python / python/mypy

Protocol with nested class

Open
#15,835 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug question topic-protocols
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

Hey,

I don't know if this a bug report, feature request or simply a question about how to do it right 😄

For proper typing I need to overload the type annotation based on the argument types. One type involved is a Protocol. Everything works as expected as long as the Protocol consists of properties or method. But as soon as I'm looking for a class within the Protocol, mypy cannot find an overload variant. (See below)

(A clear and concise description of what the bug is.)

To Reproduce

This works:

https://mypy-play.net/?mypy=latest&python=3.11&gist=d750eefc830c2080d989a7437ca518b6

This fails:

https://mypy-play.net/?mypy=latest&python=3.11&gist=0e424fbd22d314764a0d13ac9f8fe0b5

main.py:40: error: No overload variant of "create" of "Client" matches argument type "Content"  [call-overload]
main.py:40: note: Possible overload variants:
main.py:40: note:     def [_SupportsConfig <: SupportsConfig] create(self, content: _SupportsConfig) -> _SupportsConfig
main.py:40: note:     def [_SupportsConfig <: SupportsConfig] create(self, content: _SupportsConfig, result_class: None) -> _SupportsConfig
main.py:40: note:     def [_SupportsConfig <: SupportsConfig, _T] create(self, content: _SupportsConfig, result_class: type[_T]) -> _T

So, do I have to achieve my goal?

fin swimmer

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

Start by comparing the working and failing mypy-play reproductions linked in the issue, using the reported Python 3.11 examples and overload diagnostic as the baseline. Trace how mypy handles a Protocol containing properties or methods versus a nested class, then establish whether the behavior is a bug or an unsupported pattern and document the result.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.