mypy error "type[Provide] is not generic and not indexable" after upgrading dependency-injector to 4.47.0
Closed this issue · 3 comments
Description
After upgrading from dependency-injector version 4.46.0 to 4.47.0, mypy reports the following error:
The type "type[Provide]" is not generic and not indexable [misc]
This error occurs when using the common FastAPI dependency injection pattern:
file_download_uc: FileDownloadUseCase = Depends(Provide[Container.file_download_uc])This pattern worked correctly in previous versions, but fails under 4.47.0.
Environment
- dependency-injector: 4.47.0
- Python: 3.11.11
- FastAPI: 0.115.12
- mypy: 1.16.0
Reproduction Steps
Here's a minimal reproducible example:
from dependency_injector.wiring import Provide, inject
from fastapi import APIRouter, Depends
from my_app.containers import Container
from my_app.use_cases import FileDownloadUseCase
router = APIRouter()
@router.get("/files/{file_id}/download")
@inject
def download_file(
file_id: str,
# This line causes the mypy error:
file_download_uc: FileDownloadUseCase = Depends(Provide[Container.file_download_uc]),
):
return file_download_uc.download(file_id)To reproduce the issue:
- Install
dependency-injector==4.47.0 - Use
Provide[Container.some_dependency]inside a FastAPI route - Run
mypyon the file
Expected Behavior
mypy should accept the usage of Provide[...] without any errors, as it did in version 4.46.0.
Actual Behavior
mypy reports the following error:
error: The type "type[Provide]" is not generic and not indexable [misc]This error does not occur in version 4.46.0 and affects all usage of the Provide[...] pattern with FastAPI and static type checking.
Additional Note
This appears to be a regression in the type definitions for Provide introduced in version 4.47.0. In 4.46.0, the class was correctly defined to support type indexing (e.g., Provide[...]). In 4.47.0, this typing support seems to have been removed or broken, resulting in the mypy error. The issue is limited to static type checking and does not affect runtime behavior.
Root Cause
This issue appears to be caused by the removal of ClassGetItemMeta in version 4.47.0.
In version 4.46.0, the Provide class inherited a metaclass ClassGetItemMeta, which implemented the __getitem__ method. This enabled the use of subscripted types like Provide[Container.some_dependency], both for runtime behavior and static type checking.
class ClassGetItemMeta(GenericMeta):
def __getitem__(cls, item):
# Spike for Python 3.6
if isinstance(item, tuple):
return cls(*item)
return cls(item)
class _Marker(Generic[T], metaclass=ClassGetItemMeta):In 4.47.0, the metaclass was removed as part of a cleanup targeting EOL Python versions:
“Remove code for EOL Python versions”
Commit: 193249f
As a result, the Provide class no longer supports indexing via Provide[...], which breaks static type checking with tools like mypy.
This change does not affect runtime behavior, but leads to static typing errors such as:
error: The type "type[Provide]" is not generic and not indexable [misc]This issue affects multiple static type checkers, not just mypy. A similar issue has been reported for pyright as well:
As this regression affects a widely adopted and encouraged usage pattern, a timely fix would greatly contribute to preserving type safety and improving the developer experience throughout the ecosystem.
Thanks for the report, but we're already aware of the issue. Simply reverting the 6e4794b is sub optimal, since GenericMeta is simply not available in newer Pythons. We're working on an alternative fix now.
@ZipFile Thank you for the confirmation and your work on the fix. Please don’t hesitate to reach out if there’s anything I can assist with.