I have code that runs on Python 3.8 and Python 3.7, but I'd rather not install typing_extensions when it isn't need.
Code:
from typing_extensions import TypedDict
# try:
# from typing import TypedDict
# except ImportError:
# from typing_extensions import TypedDict
class MyDict(TypedDict):
name: str
id: int
payload: MyDict = {
"id": 1,
"name": "test",
}
Environment:
>>python --version
Python 3.7.4
>> pip list
Package Version
----------------- -------
mypy 0.740
mypy-extensions 0.4.3
pip 19.3.1
setuptools 42.0.1
typed-ast 1.4.0
typing-extensions 3.7.4.1
The above code passes with mypy. However, if you reverse the commented out import statements, mypy fails.
>> mypy typeddict.py
typeddict.py:4: error: Module 'typing' has no attribute 'TypedDict'; maybe "_TypedDict"?
typeddict.py:10: error: Variable "typeddict.MyDict" is not valid as a type
Found 2 errors in 1 file (checked 1 source file)
I was able to use this same import pattern successfully for Protocol, but for some reason it fails for TypedDict.
I have code that runs on Python 3.8 and Python 3.7, but I'd rather not install
typing_extensionswhen it isn't need.Code:
Environment:
The above code passes with mypy. However, if you reverse the commented out import statements, mypy fails.
I was able to use this same import pattern successfully for
Protocol, but for some reason it fails forTypedDict.