Skip to content

馃悰 Ensure that Optional[list] values work correctly with callbacks - #1018

Merged
tiangolo merged 11 commits into
fastapi:masterfrom
solesensei:master
Sep 20, 2025
Merged

馃悰 Ensure that Optional[list] values work correctly with callbacks#1018
tiangolo merged 11 commits into
fastapi:masterfrom
solesensei:master

Conversation

@solesensei

Copy link
Copy Markdown
Contributor

This pull request refactors the generate_list_convertor function to handle the case when the value parameter is None.

Fixes: #762

@github-actions github-actions Bot added the docs Improvements or additions to documentation label Oct 12, 2024
@github-actions

This comment was marked as outdated.

@github-actions

This comment was marked as outdated.

@solesensei solesensei changed the title fix: optional[list] values are not working with callbacks fix(bug): optional[list] values are not working with callbacks Oct 14, 2024
@solesensei

solesensei commented Oct 15, 2024

Copy link
Copy Markdown
Contributor Author

@tiangolo, Hi! Please check this review. It blocks our application from upgrading to 0.10.0+.

@solesensei

Copy link
Copy Markdown
Contributor Author

cc: @svlandeg

@solesensei solesensei changed the title fix(bug): optional[list] values are not working with callbacks 馃悶 fix(bug): optional[list] values are not working with callbacks Oct 17, 2024
@solesensei solesensei changed the title 馃悶 fix(bug): optional[list] values are not working with callbacks 馃悰 fix(bug): optional[list] values are not working with callbacks Oct 17, 2024
@svlandeg

Copy link
Copy Markdown
Member

Hi @solesensei, thanks for the PR! We've got this on our internal queue and will let you know once we've been able to review it 馃檹

@svlandeg svlandeg self-assigned this Oct 23, 2024
@svlandeg svlandeg changed the title 馃悰 fix(bug): optional[list] values are not working with callbacks 馃悰 Ensure that Optional[list] values work correctly with callbacks Oct 23, 2024
@solesensei

solesensei commented Nov 7, 2024

Copy link
Copy Markdown
Contributor Author

@svlandeg I'm sorry, but could you please take a look at this PR? It's a simple one-line fix that shouldn't take much time.

Also it's the most voted unresolved issue of all time

@svlandeg

svlandeg commented Nov 8, 2024

Copy link
Copy Markdown
Member

@solesensei: like I said, we have this on our queue and will look at it soon. Please refrain from pinging maintainers directly, as this makes our Github mentions very difficult to use and we don't just give priority to whoever pings the most 馃槈

@github-actions

Copy link
Copy Markdown
Contributor

馃摑 Docs preview for commit 07350b5 at: https://543f0da8.typertiangolo.pages.dev

@svlandeg svlandeg left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! I've now had time to go through it in detail.

The tests you've added clearly showcase the issue on master when a callback is combined with a list convertor - giving an error when value is None at L655 of main.py.

In the test files, both test_tutorial005.py and test_tutorial005_an.py only contain one failing test, and both represent the same behaviour. Generally, tutorial files should also be included in the Typer documentation. In this case however, I don't think that adding more explanation to the documentation adds much. As such, I suggest to drastically reduce the unit test files and only test for the edge case mentioned above.

I'll go ahead and make those changes accordingly, to get the PR in a good shape to merge.

Comment thread docs_src/options/callback/tutorial005.py Outdated
Comment thread docs_src/options/callback/tutorial005_an.py Outdated
Comment thread tests/test_tutorial/test_options/test_callback/test_tutorial005.py Outdated
Comment thread tests/test_tutorial/test_options/test_callback/test_tutorial005_an.py Outdated
Comment thread typer/main.py Outdated
@svlandeg
svlandeg marked this pull request as draft December 26, 2024 14:48
Comment thread typer/main.py
Comment on lines -643 to +646
) -> Callable[[Sequence[Any]], Optional[List[Any]]]:
def internal_convertor(value: Sequence[Any]) -> Optional[List[Any]]:
if default_value is None and len(value) == 0:
return None
) -> Callable[[Optional[Sequence[Any]]], Optional[List[Any]]]:
def internal_convertor(value: Optional[Sequence[Any]]) -> Optional[List[Any]]:
if value is None or len(value) == 0:
return default_value

@svlandeg svlandeg Dec 26, 2024

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've changed the check to be more extensive and return default_value any time that value is None or empty. This needed to be updated when fixing the type, or the linter would complain about value possible being None when running the list comprehension.

@svlandeg svlandeg left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should now be ready for a final review by Tiangolo!

@svlandeg svlandeg removed their assignment Dec 26, 2024
@svlandeg
svlandeg marked this pull request as ready for review December 26, 2024 15:50
@svlandeg svlandeg added bug Something isn't working and removed docs Improvements or additions to documentation labels Jan 30, 2025
@solesensei

Copy link
Copy Markdown
Contributor Author

Hey! 馃憢

Any updates on this?

@solesensei

Copy link
Copy Markdown
Contributor Author

Hi, @tiangolo! Do you have a moment to look at this PR?

@tiangolo tiangolo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, thank you! 馃殌

And thanks a lot @svlandeg for the review and work. 馃檱

This will be available in Typer 0.19.1 in the next hours.

@tiangolo
tiangolo merged commit 53ae9ee into fastapi:master Sep 20, 2025
24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants