Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Ensure that Optional[list] values work correctly with callbacks #1018

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

solesensei
Copy link

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
Copy link

📝 Docs preview for commit 5c9ba8f at: https://d5834c3f.typertiangolo.pages.dev

Copy link

📝 Docs preview for commit ade4517 at: https://3a35550f.typertiangolo.pages.dev

@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
Copy link
Author

solesensei commented Oct 15, 2024

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

@solesensei
Copy link
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
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
Copy link
Author

solesensei commented Nov 7, 2024

@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
Copy link
Member

svlandeg commented Nov 8, 2024

@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 😉

Copy link

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

Copy link
Member

@svlandeg svlandeg left a comment

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.

docs_src/options/callback/tutorial005.py Outdated Show resolved Hide resolved
docs_src/options/callback/tutorial005_an.py Outdated Show resolved Hide resolved
typer/main.py Outdated Show resolved Hide resolved
@svlandeg svlandeg marked this pull request as draft December 26, 2024 14:48
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
Copy link
Member

@svlandeg svlandeg Dec 26, 2024

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.

Copy link
Member

@svlandeg svlandeg left a comment

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Improvements or additions to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants