Skip to content

Error type hints in Python3.12 (#1147) #1839

@954-Ivory

Description

@954-Ivory

Initial Checks

  • I have read and followed the docs and still think this is a bug

Description

I encountered the following issue when using langchain in Python 3.12:

langchain-ai/langchain#15016

    return Generic.__class_getitem__.__func__(cls, item)  # type: ignore
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'builtin_function_or_method' object has no attribute '__func__'. Did you mean: '__doc__'?

It caused by this PR (#1147)

I think it can be changed like this:

    def __class_getitem__(cls, item: Union[Type[BaseDocument], TypeVar, str]):
        if not isinstance(item, type):
            try:
                return Generic.__class_getitem__.__func__(cls, item)  # type: ignore
                # this do nothing that checking that item is valid type var or str
                # Keep the approach in #1147 to be compatible with lower versions of Python.
            except AttributeError:
                from types import GenericAlias
                return GenericAlias(cls, item)  # type: ignore

I want to know others' opinions.

Example Code

https://learn.deeplearning.ai/langchain/lesson/5/question-and-answer

Python, DocArray & OS Version

OS==Windows 22H2
Python==3.12.0
docarray==0.39.1

Affected Components

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions