-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Description
Bug Report
Given that :
MyType = dict[str, int]
This line raise an var-annotated error :
d = MyType()
I have to write :
d:MyType = MyType()
This is redundant. I think this is a bug because the following code works perfectly fine :
d = dict[str, int]()
And it is basically the same thing.
Side note : I know that the following code exists :
from typing import NewType
MyType2 = NewType("MyType2", dict[str, int])
d = MyType2({})
But I do not want to create a new type, I just want to create a specific annotation (a shortcut) and deal with multiple dictionaries that resemble to each other.
In my original code, my type is dict[tuple[int, Decimal], dict[SomeDataclass, dict[str, SomeOtherDataclass]]]. You might understand why I do which to use a shortcut.
To Reproduce
This is a complete example :
d1: dict[str, int] = {}
d2 = dict[str, int]()
MyType = dict[str, int] # This is the shortcut
d3: MyType = {}
d4 = MyType()
from typing import NewType
MyType2 = NewType("MyType2", dict[str, int])
d5: MyType2
d6 = MyType2({})Expected Behavior
mypy my_example.py should not return any errors.
Actual Behavior
mypy my_example.py returns :
my_example.py:7:1: error: Need type annotation for "d4" (hint: "d4: Dict[<type>, <type>] = ...") [var-annotated]
Your Environment
- Mypy version used: 1.5.1
- Mypy command-line flags: None
- Mypy configuration options from
mypy.ini(and other config files): None - Python version used: 3.10.12