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

TypedDict should be imported from typing_extensions rather than typing in Python versions <=3.11 #2125

Open
ubiquitousbyte opened this issue Dec 9, 2024 · 2 comments

Comments

@ubiquitousbyte
Copy link

dlt version

1.4.0

Describe the problem

The TColumnType present in the dlt.common.schema module cannot be used as a property within a Pydantic model for Python versions before 3.12. The reason being is that TColumnType inherits from TypedDict, but that comes from the typing module. Python versions prior to 3.12 do not support this. The TypedDict type lives in the typing_extensions module.

As such, Pydantic fails to create a model.

pydantic.errors.PydanticUserError: Please use `typing_extensions.TypedDict` instead of `typing.TypedDict` on Python < 3.12.

Expected behavior

Expected behaviour is to change all imports of TypedDict to something like

import sys

if sys.version_info >= (3, 12):
    from typing import TypedDict
else:
    from typing_extensions import TypedDict

This would allow the usage of the very nice schema types that dlt exposes in custom Pydantic models.

Steps to reproduce

Create a simple type that inherits from pydantic.BaseModel and add a property of TColumnType in a Python interpreter that is running a version older than 3.12. Try to instantiate your model and see that it fails.

Operating system

macOS

Runtime environment

Local

Python version

3.11

dlt data source

No response

dlt destination

No response

Other deployment details

No response

Additional information

No response

@sh-rp
Copy link
Collaborator

sh-rp commented Dec 9, 2024

I think typeddict was added in 3.11 not 3.12. Apart from that we should fix this, yes.

@rudolfix
Copy link
Collaborator

hmmm TypedDict is in typing from 3.8... most probably Pydantic needs some features that were not implemented before 3.12. We had the same issues with Annotated which is now imported via extensions.
@sh-rp we import all such typings via dlt.common.typing (or at least we should) so let's import it there and then from there evrywhere else

@rudolfix rudolfix moved this from Todo to In Progress in dlt core library Dec 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: In Progress
Development

No branches or pull requests

3 participants