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
  TypeError: cannot pickle ‘_asyncio.Future’ object Abdul_Rafey 1 268 Mar-07-2024, 03:40 PM
Last Post: deanhystad
  error in class: TypeError: 'str' object is not callable akbarza 2 444 Dec-30-2023, 04:35 PM
Last Post: deanhystad
Bug TypeError: 'NoneType' object is not subscriptable TheLummen 4 677 Nov-27-2023, 11:34 AM
Last Post: TheLummen
  TypeError: 'NoneType' object is not callable akbarza 4 916 Aug-24-2023, 05:14 PM
Last Post: snippsat
  [NEW CODER] TypeError: Object is not callable iwantyoursec 5 1,260 Aug-23-2023, 06:21 PM
Last Post: deanhystad
  TypeError: 'float' object is not callable #1 isdito2001 1 1,044 Jan-21-2023, 12:43 AM
Last Post: Yoriz
  TypeError: a bytes-like object is required ZeroX 13 3,834 Jan-07-2023, 07:02 PM
Last Post: deanhystad
  Help with python 'not subscriptable' error Extra 3 1,976 Dec-16-2022, 05:55 PM
Last Post: woooee
  declaring object parameters with type JonWayn 2 853 Dec-13-2022, 07:46 PM
Last Post: JonWayn
  TypeError: 'float' object is not callable TimofeyKolpakov 3 1,367 Dec-04-2022, 04:58 PM
Last Post: TimofeyKolpakov

Forum Jump:

User Panel Messages

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