I wrote this Python function with what I thought were reasonable MyPy types: [code]…
- User
I wrote this Python function with what I thought were reasonable
MyPytypes:def _program_upload_group_for_processing( self, id: int, statuses: list[Status] ) -> DBProgramUploadGroup | Literal[False]:However, Python complains with
TypeError: 'function' object is not subscriptablewhen is reacheslist[status]. What gives? I've used this type of annotation elsewhere in the app (though I don't think in function arguments). What's the right way? - Assistant
Great question! The error you're seeing is due to how the
listtype is being used in your type annotation:def _program_upload_group_for_processing( self, id: int, statuses: list[Status] ) -> DBProgramUploadGroup | Literal[False]:The Problem
The error message:
TypeError: 'function' object is not subscriptablesuggests that
Statusis being interpreted as a function, not a type. This usually happens if:- You have a function named
Statusin your code, not a class or type. - Or, you haven't imported
Statusas a type.
But, another common cause is using
list[...]in Python versions before 3.9. Thelist[Status]syntax is only valid in Python 3.9 and later. In earlier versions, you must useList[Status]from thetypingmodule.How to Fix
1. Check your Python version
- If you're using Python 3.9+,
list[Status]is valid. - If you're using Python <3.9, you must use
List[Status]fromtyping.
2. Import the correct types
Make sure you have:
from typing import List, LiteralAnd that
Statusis a class or type, not a function.3. Update your annotation
For Python <3.9:
def _program_upload_group_for_processing( self, id: int, statuses: List[Status] ) -> DBProgramUploadGroup | Literal[False]:For Python 3.9+:
def _program_upload_group_for_processing( self, id: int, statuses: list[Status] ) -> DBProgramUploadGroup | Literal[False]:4. Check
StatusIf you have a function named
Status, rename it or import the correct type.
Summary Table
Python Version List Syntax Import Needed < 3.9 List[Status]from typing import List3.9+ list[Status]None (built-in)
TL;DR:
- Use
List[Status]if on Python <3.9. - Make sure
Statusis a type, not a function. - Import
Listfromtypingif needed.
Let me know if you need help checking your Python version or fixing your imports!
- You have a function named
- User
Okay I found the problem - I've defined
listas a function in this same class elsewhere. I'm on Python 3.13. What's the right way now? - Assistant
Great detective work!
The root cause is that you overwrote the built-inlistby defining a function namedlistin your class or module. This shadows the built-inlisttype, so when you writelist[Status], Python tries to subscript your function, not the built-in type, causing the error.The Right Way (Python 3.13)
- Do not define functions or variables named
list,dict,set, etc. These are built-in types and should not be shadowed. - In Python 3.9+, you can and should use the built-in generics:
list[Status] - If you must keep a function named
list(not recommended), you can usebuiltins.listfor type annotations:
But renaming your function is strongly preferred.import builtins def _program_upload_group_for_processing( self, id: int, statuses: builtins.list[Status] ) -> DBProgramUploadGroup | Literal[False]:
Best Practice
Rename your function to something else (e.g.,
list_items,list_statuses, etc.), so you can use the built-inlisttype freely in your code and type annotations.Example:
def _program_upload_group_for_processing( self, id: int, statuses: list[Status] ) -> DBProgramUploadGroup | Literal[False]: ...
Summary:
- Don't shadow built-in types like
list. - In Python 3.13, use
list[Status]for type annotations. - If you must, use
builtins.list[Status], but renaming is better.
Let me know if you want help refactoring your code!
- Do not define functions or variables named
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.