Python Forum
TypeError: 'type' object is not subscriptable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TypeError: 'type' object is not subscriptable
#1
Hi,

I'm fairly new to Python and I'm getting this error and to be honest, I have no clue why. I get it when running a pytest. The full stacktrace is:

Quote:emusim/pytest/test_central_bank.py:1: in <module>
from emusim.cockpit.supply.euro import CentralBank
emusim/cockpit/supply/__init__.py:1: in <module>
from .data_collector import DataCollector
emusim/cockpit/supply/data_collector.py:6: in <module>
class DataCollector(ABC):
emusim/cockpit/supply/data_collector.py:13: in DataCollector
def data_structure(self) -> OrderedDict[str, OrderedDict[str, bool]]:
E TypeError: 'type' object is not subscriptable

The relevant code is the following:

test_central_bank.py
from emusim.cockpit.supply.euro import CentralBank
__init__.py
from .data_collector import DataCollector
data_collector.py
from abc import ABC, abstractmethod
from collections import OrderedDict
from typing import List, KeysView


class DataCollector(ABC):

    def __init__(self):
        self.__data_dict: OrderedDict[str, OrderedDict[str, List[float]]] = OrderedDict()

    @abstractmethod
    @property
    def data_structure(self) -> OrderedDict[str, OrderedDict[str, bool]]:
        pass
What am I not seeing here?

Thanks in advance,
Stef
Reply
#2
Do you have to use typing? Why not simply write
from abc import ABC, abstractmethod, abstractproperty
from collections import OrderedDict
 
class DataCollector(ABC):
 
    def __init__(self):
        self.__data_dict = OrderedDict()

    @property
    @abstractmethod
    def data_structure(self):
        pass
    
class Spam(DataCollector):

    @DataCollector.data_structure.getter
    def data_structure(self):
        return "stru"

if __name__ ==  '__main__':
    s = Spam()
    print(s.data_structure)
If you really want to clutter the code with use typing, note that typing.OrderedDict is not the same as collections.OrderedDict. I guess you need to use the former in the type declarations.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  'int' object is not subscriptable after API call ed8484 1 379 Sep-18-2021, 02:06 PM
Last Post: ed8484
  TypeError: 'DataFrame' object is not callable using Pandas in Python sofiavlachou 1 1,406 Sep-02-2021, 03:24 PM
Last Post: buran
  TypeError: 'NoneType' object is not subscriptable syafiq14 2 1,848 Aug-26-2021, 06:18 AM
Last Post: alexaminar
  Message='int' object is not subscriptable DanielCook 4 562 Aug-10-2021, 05:21 PM
Last Post: deanhystad
  python 3: TypeError: a bytes-like object is required, not 'str' wardancer84 3 854 Jul-09-2021, 05:55 PM
Last Post: deanhystad
  You have any idea, how fix TypeError: unhashable type: 'list' lsepolis123 2 1,058 Jun-02-2021, 07:55 AM
Last Post: supuflounder
  TypeError: int() argument must be a string, a bytes-like object or a number, not 'Non Anldra12 2 1,737 May-02-2021, 03:45 PM
Last Post: Anldra12
  Bool Object is not Subscriptable quest 1 1,015 May-02-2021, 11:12 AM
Last Post: Yoriz
Star Type Error: 'in' object is not callable nman52 3 979 May-01-2021, 11:03 PM
Last Post: nman52
  Float Object is not Subscriptable quest 2 1,019 Apr-20-2021, 09:28 AM
Last Post: quest

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020