-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Open
Labels
bugmypy got something wrongmypy got something wrongtopic-descriptorsProperties, class vs. instance attributesProperties, class vs. instance attributestopic-protocols
Description
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).
Metadata
Metadata
Assignees
Labels
bugmypy got something wrongmypy got something wrongtopic-descriptorsProperties, class vs. instance attributesProperties, class vs. instance attributestopic-protocols