mypy incorrectly flags a type-var error when a descriptor overrides a protocol property in a different file.
Bug Report
To Reproduce
- Create an
app.pyfile 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).
Yeah, this is a known limitation, for technical reasons descriptors/protocols combo doesn't work during type variable bound/values checking (because it is performed during semantic analysis phase, before actual type checking starts), see second bullet point in https://github.com/python/mypy/issues/19299. To set correct expectations, this is not easy to fix properly, but we want to do this (mostly for other benefits, like lazy deserialization of symbols), probable somewhere in early 2026.