python / python/mypy

mypy incorrectly flags a type-var error when a descriptor overrides a protocol property in a different file.

Open
#20,317 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Bug Report

To Reproduce

  • Create an app.py file with this inside:
from __future__ import annotations

from typing import Protocol

# PROTOCOL
class ServiceProto(Protocol):
    @property
    def value(self) -> int: ...

# DESCRIPTOR
class Descriptor:
    def __get__(self, instance: ServiceImpl, owner: type[ServiceImpl]) -> int:
        return 5

# IMPLEMENTATION
class ServiceImpl:
    value = Descriptor()

class App[ServiceT: ServiceProto]:
    def __init__(self, service: ServiceT) -> None:
        self.service = service
  • create a second file (important) with this inside (mine is named run.py):
from __future__ import annotations

from .app import App, ServiceImpl

def bootstrap() -> App[ServiceImpl]:
    return App(ServiceImpl())

Expected Behavior
No errors

Actual Behavior

mypy src/
run.py:5: error: Type argument "ServiceImpl" of "App" must be a subtype of "ServiceProto"  [type-var]
run.py:6: error: Value of type variable "ServiceT" of "App" cannot be "ServiceImpl"  [type-var]
Found 2 errors in 1 file (checked 3 source files)

Your Environment

  • Mypy version used: mypy 1.18.2 (compiled: yes)
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: Python 3.13.7 (via uv)

Weird Behavior

Merge both files, remove redundant imports. No more errors.

PS: Feel free to edit the title. This was a hard one to describe in one sentence without knowing the root cause.
PPS: This is a toy example with integers to illustrate the bug. My real use case is more complex: the descriptor is a lazy instantiator that works like cached_property (the descriptor is given a class object on instantiation, it instantiates and caches an instance of that class on the owning service instance, then serves the cached instance on further accesses).

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

Reproduce the report using the separate app.py and run.py files, then run mypy src/ with Python 3.13.7 and mypy 1.18.2. Trace how the descriptor-backed ServiceImpl is checked against ServiceProto across the file boundary and how App[ServiceImpl] produces the type-var errors. Done means the two-file example reports no errors while the behavior remains covered by an appropriate regression test.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.