Skip to content
TheChatArchiveThe Chat Archive
gpt-4.1-2025-04-14· OpenAIHugging Face LMArena Human Preference 140K

I wrote this Python function with what I thought were reasonable MyPy types: [code]…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with o3-2025-04-16’s answer.
  1. User

    I wrote this Python function with what I thought were reasonable MyPy types:

        def _program_upload_group_for_processing(
            self, id: int, statuses: list[Status]
        ) -> DBProgramUploadGroup | Literal[False]:
    

    However, Python complains with TypeError: 'function' object is not subscriptable when is reaches list[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?

  2. Assistant

    Great question! The error you're seeing is due to how the list type 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 subscriptable
    

    suggests that Status is being interpreted as a function, not a type. This usually happens if:

    • You have a function named Status in your code, not a class or type.
    • Or, you haven't imported Status as a type.

    But, another common cause is using list[...] in Python versions before 3.9. The list[Status] syntax is only valid in Python 3.9 and later. In earlier versions, you must use List[Status] from the typing module.

    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] from typing.

    2. Import the correct types

    Make sure you have:

    from typing import List, Literal
    

    And that Status is 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 Status

    If you have a function named Status, rename it or import the correct type.


    Summary Table

    Python VersionList SyntaxImport Needed
    < 3.9List[Status]from typing import List
    3.9+list[Status]None (built-in)

    TL;DR:

    • Use List[Status] if on Python <3.9.
    • Make sure Status is a type, not a function.
    • Import List from typing if needed.

    Let me know if you need help checking your Python version or fixing your imports!

  3. User

    Okay I found the problem - I've defined list as a function in this same class elsewhere. I'm on Python 3.13. What's the right way now?

  4. Assistant

    Great detective work!
    The root cause is that you overwrote the built-in list by defining a function named list in your class or module. This shadows the built-in list type, so when you write list[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 use builtins.list for type annotations:
      import builtins
      
      def _program_upload_group_for_processing(
          self, id: int, statuses: builtins.list[Status]
      ) -> DBProgramUploadGroup | Literal[False]:
      
      But renaming your function is strongly preferred.

    Best Practice

    Rename your function to something else (e.g., list_items, list_statuses, etc.), so you can use the built-in list type 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!

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.