Fix Option(None) in Annotated crashing with AttributeError#1564
Open
bysiber wants to merge 1 commit intofastapi:masterfrom
Open
Fix Option(None) in Annotated crashing with AttributeError#1564bysiber wants to merge 1 commit intofastapi:masterfrom
bysiber wants to merge 1 commit intofastapi:masterfrom
Conversation
When using Annotated[str, Option(None)], the None default gets reinterpreted as a param_decl name since 'None is not ...' is True. Click then crashes calling .isidentifier() on None. Added an isinstance(str) check so only actual strings are treated as param_decl names. Non-string defaults are left as-is.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Using
Annotated[str, typer.Option(None)]crashes withAttributeError: 'NoneType' object has no attribute 'isidentifier'.The Bug
In
utils.py, when processingAnnotatedparameters, typer reinterpretsOption's first positional argument from "default value" to "first param_decl name". The guard only checksparameter_info.default is not ..., which passes forNonesinceNone is not ...isTrue. TheNonevalue then getscast(str, ...)and inserted intoparam_decls, and Click crashes when it tries to call.isidentifier()on it.The Fix
Added
isinstance(parameter_info.default, str)to the guard so only actual strings are treated as param_decl names. Non-string values (likeNone) are left asdefaultand handled by the normal default-value logic below.