Skip to content

fix(types): guard against IndexError on bare List in classifier fns#1356

Open
devteamaegis wants to merge 1 commit into
PrefectHQ:mainfrom
devteamaegis:fix/indexerror-on-unparameterised-generic
Open

fix(types): guard against IndexError on bare List in classifier fns#1356
devteamaegis wants to merge 1 commit into
PrefectHQ:mainfrom
devteamaegis:fix/indexerror-on-unparameterised-generic

Conversation

@devteamaegis

Copy link
Copy Markdown

What's broken

Calling is_classifier(List) or as_classifier(List) with an unparameterised typing.List (no type argument) raises IndexError: tuple index out of range. Both functions check get_origin(typ) is list and then unconditionally index get_args(typ)[0], but bare List produces get_args(List) == (), making the index crash immediately. Any Task constructed with result_type=List hits this path.

Why it happens

get_args returns an empty tuple for unparameterised generic aliases, so indexing position 0 is always an error.

Fix

Added a type_args = get_args(typ) guard before the index in both functions. In is_classifier, an empty type_args returns False (bare List is not a valid classifier). In as_classifier, it raises ValueError with a message directing callers to use a parameterised form like list[MyEnum].

Test

test_is_classifier_bare_list_no_indexerror confirms is_classifier(List) returns False and as_classifier(List) raises ValueError with the word "unparameterised", with no IndexError in either case.

Fixes #1355

…r and as_classifier

Both functions called get_args(typ)[0] after confirming get_origin(typ) is list,
but never checked whether get_args() returned a non-empty tuple. Bare typing.List
(no type parameter) produces get_args() == (), causing an immediate IndexError.

is_classifier now returns False for bare List; as_classifier raises ValueError
with a descriptive message directing callers to use a parameterised form.
@github-actions github-actions Bot added the tests label Jun 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: IndexError in is_classifier/as_classifier with bare typing.List

1 participant