Skip to content

Generic slots classes don't work #313

Closed
@gabbard

Description

@gabbard

Attempting to create a generic attrs class with slots=True fails if it does not inherit from some other generic class.

from typing import TypeVar, Generic, Container
from unittest import TestCase

from attr import attrs

T = TypeVar('T')


class TestGenericBug(TestCase):
    def test_no_slots_ok(self):
        @attrs
        class Foo(Generic[T]):
            pass

    def test_no_attrs(self):
        class Meep(Generic[T]):
            __slots__ = ()

    def test_frozen_with_generic_parent_ok(self):
        @attrs(frozen=True)
        class Foo2(Generic[T], Container[T]):
            def __contains__(self, x: object) -> bool:
                return False
   
   # Failure is here
    def test_slots_with_no_parent(self):
        with self.assertRaisesRegex(TypeError, "Cannot inherit from plain Generic"):
            @attrs(slots=True)
            class Foo4(Generic[T]):
                pass

The full failure stack trace (if we remove the assertRaises is:

  File "/Users/gabbard/repos/isi-flexnlp/tests/util/test_generic_bug.py", line 27, in test_slots_with_no_parent
    class Foo4(Generic[T]):
  File "/Users/gabbard/anaconda3/envs/flexnlp/lib/python3.6/site-packages/attr/_make.py", line 637, in wrap
    return builder.build_class()
  File "/Users/gabbard/anaconda3/envs/flexnlp/lib/python3.6/site-packages/attr/_make.py", line 372, in build_class
    return self._create_slots_class()
  File "/Users/gabbard/anaconda3/envs/flexnlp/lib/python3.6/site-packages/attr/_make.py", line 442, in _create_slots_class
    cd,
  File "/Users/gabbard/anaconda3/envs/flexnlp/lib/python3.6/typing.py", line 948, in __new__
    raise TypeError("Cannot inherit from plain Generic")
TypeError: Cannot inherit from plain Generic

I am using attrs 17.3.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions