Skip to content

[BUG] Incompatibility with 3.12-style generics (PEP695) #1469

@zacharyrs

Description

@zacharyrs

Describe the bug

Using PEP695 type parameters syntax conflicts with TensorClass's bracket-based configuration.

To Reproduce

from tensordict import TensorClass
class A[T: int](TensorClass):
    a: int
    def do_thing(self, t: T) -> None:
        ...
        
class B[T: int](A[T]):
    ...
     
Traceback (most recent call last):
  File "<python-input-29>", line 1, in <module>
    class B[T: int](A[T]):
        ...
  File "<python-input-29>", line 1, in <generic parameters of B>
    class B[T: int](A[T]):
                    ~^^^
  File "/Users/.../python3.13/site-packages/tensordict/tensorclass.py", line 3072, in __getitem__
    name = "_".join(item)  # type: ignore
TypeError: sequence item 0: expected str instance, typing.TypeVar found```

Similarly...

class B(A[int]):
    ...
    
Traceback (most recent call last):
  File "<python-input-30>", line 1, in <module>
    class B(A[int]):
            ~^^^^^
  File "/Users/.../python3.13/site-packages/tensordict/tensorclass.py", line 3072, in __getitem__
    name = "_".join(item)  # type: ignore
TypeError: sequence item 0: expected str instance, type found

And finally...

class B(A["int"]):
    ...

Traceback (most recent call last):
  File "<python-input-31>", line 1, in <module>
    class B(A["int"]):
            ~^^^^^^^
  File "/Users/.../python3.13/site-packages/tensordict/tensorclass.py", line 3077, in __getitem__
    result = _TensorClassMeta(  # type: ignore
        cls_name,
    ...<2 lines>...
        **{_item: True for _item in item},
    )
  File "/Users/.../python3.13/site-packages/tensordict/tensorclass.py", line 3032, in __new__
    cls = super().__new__(mcs, name, bases, namespace, **kwargs)
  File "<frozen abc>", line 106, in __new__
  File "/Users/.../cpython-3.13.1-macos-aarch64-none/lib/python3.13/typing.py", line 1257, in _generic_init_subclass
    super(Generic, cls).__init_subclass__(*args, **kwargs)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
TypeError: TensorClass_int.__init_subclass__() takes no keyword arguments

Expected behavior

Generics to have priority.

System info

Describe the characteristic of your environment:

  • pip; uv venv
  • 3.13.1
import tensordict, numpy, sys, torch
print(tensordict.__version__, numpy.__version__, sys.version, sys.platform, torch.__version__)
0.10.0 2.1.3 3.13.1 (main, Jan  5 2025, 06:22:40) [Clang 19.1.6 ] darwin 2.8.0

Reason and Possible fixes

def __getitem__(cls, item: IndexType) -> Self:

Checklist

  • I have checked that there is no similar issue in the repo (required)
  • I have read the documentation (required)
  • I have provided a minimal working example to reproduce the bug (required)

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions