Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trying to import TypedDict from typing of tpying_extensions fails #3500

Closed
plannigan opened this issue Nov 25, 2019 · 9 comments
Closed

Trying to import TypedDict from typing of tpying_extensions fails #3500

plannigan opened this issue Nov 25, 2019 · 9 comments

Comments

@plannigan
Copy link

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.

@Naddiseo
Copy link
Contributor

It looks like TypedDict was added to typing in python3.8: https://docs.python.org/3/whatsnew/3.8.html#typing

@plannigan
Copy link
Author

Yes, but my code also has to run on older versions of python.

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.

@ilevkivskyi
Copy link
Member

There is not much we can do here. Protocol works because it is exposed in typeshed in typing even in versions where it is in fact not available. I am going to transfer this issue to typeshed, so you can try to convince people there.

@ilevkivskyi ilevkivskyi transferred this issue from python/mypy Nov 25, 2019
@JelleZijlstra
Copy link
Member

typing.TypedDict doesn't exist at runtime in 3.7, so I'm not sure what the typeshed issue is here. You will need to install typing_extensions to use TypedDict in 3.7.

@ilevkivskyi
Copy link
Member

typing.TypedDict doesn't exist at runtime in 3.7, so I'm not sure what the typeshed issue is here.

The issue is that this causes mypy to not understand the try-except import. The first import makes TypedDict a variable with type Any, and this subsequently breaks all the call sites, turning types into variables with Any type.

There is a precedent with Protocol (in typeshed it is exposed as if it is always present), this is why I said "you can try to convince people there".

@vemel
Copy link
Contributor

vemel commented Dec 3, 2019

@plannigan you can check Python version instead

import sys

if sys.version_info >= (3, 8):
    from typing import TypedDict, Literal, overload  # pylint: disable=no-name-in-module
else:
    from typing_extensions import TypedDict, Literal, overload

@Caligatio
Copy link
Contributor

Caligatio commented Apr 25, 2020

To enforce what @vemel said, checking against the version is the preferred method per the documentation.

EDIT: @'ed the wrong person

@srittau
Copy link
Collaborator

srittau commented Apr 25, 2020

@vemel's version is the preferred idiom.

@Sushang-Agnihotri
Copy link

Try to Uninstall the related librarie(s) and install it again.
it worked for me.

webknjaz added a commit to ansible/ansible-navigator that referenced this issue Jan 27, 2022
This approach is preferred over the try/except one to help MyPy deal
with the compatibility quirks according to:
* ansible/ansible-lint#915 (comment)
* python/typeshed#3500 (comment)
ssbarnea pushed a commit to ssbarnea/ansible-navigator that referenced this issue Feb 12, 2022
This approach is preferred over the try/except one to help MyPy deal
with the compatibility quirks according to:
* ansible/ansible-lint#915 (comment)
* python/typeshed#3500 (comment)
Lin-jun-xiang added a commit to Lin-jun-xiang/gpt4free that referenced this issue Sep 11, 2023
unique0311 pushed a commit to unique0311/freeGPT4_Python-OpenAI that referenced this issue Nov 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants