Plugin's get_type_analyze_hook is not used for Annotated type in type alias or generic specialization
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Bug Report
Mypy doesn't use plugin's get_type_analyze_hook for Annotated type used in type alias or generic specialization. However, the hook is used for custom types (not Annotated) used in these contexts, and it's also called for Annotated used in other contexts.
To Reproduce
plugin.py
from typing import Callable
from mypy.plugin import AnalyzeTypeContext, Plugin
from mypy.types import NoneType, Type
def plugin(version: str):
return MyPlugin
class MyPlugin(Plugin):
def get_type_analyze_hook(self, fullname: str) -> Callable[[AnalyzeTypeContext], Type] | None:
if fullname in {'typing.Annotated', 'typing_extensions.Annotated'}:
return _type_analyze
return None
def _type_analyze(ctx: AnalyzeTypeContext) -> Type:
ctx.api.note('Analyzing type of Annotated[...]', ctx.context)
return NoneType() # for the sake of example
module.py
from typing import Annotated, Generic, TypeAlias, TypeVar, reveal_type
AnnotatedTA: TypeAlias = Annotated[dict, None]
typed_with_alias: AnnotatedTA
reveal_type(typed_with_alias) # Revealed type is "builtins.dict[Any, Any]"
typed_directly: Annotated[dict, None] # Analyzing type of Annotated[...]
reveal_type(typed_directly) # Revealed type is "None", as expected
T = TypeVar('T')
class GenericType(Generic[T]):
dummy: T
class Specialization(GenericType[Annotated[dict, None]]):
...
reveal_type(Specialization().dummy) # Revealed type is "builtins.dict[Any, Any]"
Expected Behavior
All revealed types are None as requested by _type_analyze hook
Actual Behavior
Only directly typed variable typed_directly has the type requested by plugin
Your Environment
- Mypy version used: 1.10.1
- Mypy command-line flags: none
- Mypy configuration options from
mypy.ini(and other config files):plugins = plugin.py - Python version used: 3.8, 3.12
Background
I'm writing a plugin which generates TypedDictType for Annotated[dict, ...] annotations based on some external configuration files. The arguments to Annotated are string literals, so I can't really use generics. In runtime these are just normal dicts.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the plugin.py and module.py reproducer with the reported Python and mypy versions, then trace type analysis for Annotated in direct use, the TypeAlias, and generic specialization. Done means the hook is invoked in all three cases and each reveal_type reports None as requested by _type_analyze.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 48/100