-
-
Notifications
You must be signed in to change notification settings - Fork 31.2k
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
bpo-37910: argparse usage wrapping should allow whitespace differences caused by metavar #15372
Closed
sjfranklin
wants to merge
9
commits into
python:main
from
sjfranklin:fix-37910-extra-whitespace-breaks-argparse-wrap
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
dd7ef9d
bpo-37910: argparse usage wrapping should not fail on whitespace diff…
41e22bf
revert accidental automatic PEP8 changes
8f79a6e
synced with upstream
fcf7d42
added unit tests for now allowable metavar characters including nearl…
dba2acc
sync with upstream
646fbad
bpo-37910: checks format_usage wrapping to ensure whitespaces do not …
30c69ff
bpo-37910: checks format_usage wrapping to ensure whitespaces do not …
ad76df5
bpo-37910: checks format_usage wrapping to ensure whitespaces do not …
d941a85
bpo-37910: checks format_usage wrapping to ensure whitespaces do not …
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5224,6 +5224,192 @@ def test_nargs_3_metavar_length3(self): | |
self.do_test_no_exception(nargs=3, metavar=("1", "2", "3")) | ||
|
||
|
||
class TestAddArgumentMetavarWrapNoException(TestCase): | ||
"""Check that certain special character wrap with no exceptions | ||
Based off TestAddArgumentMetavar""" | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please combine all of these into a single test method that runs the cases in a loop with unittest subtests. |
||
def do_test_no_exception(self, metavar): | ||
parser = argparse.ArgumentParser(prog='PROG' * 19) # force wrapping | ||
parser.add_argument("--foo", metavar=metavar) | ||
parser.format_usage() | ||
|
||
def test_metavar_nil(self): | ||
self.do_test_no_exception(metavar='') | ||
|
||
def test_metavar_space(self): | ||
self.do_test_no_exception(metavar=' ') | ||
|
||
def test_metavar_Line_Feed(self): | ||
self.do_test_no_exception(metavar='\n') | ||
|
||
def test_metavar_Tab(self): | ||
self.do_test_no_exception(metavar='\t') | ||
|
||
def test_metavar_Carriage_Return(self): | ||
self.do_test_no_exception(metavar='\r') | ||
|
||
def test_metavar_Carriage_Return_and_Line_Feed(self): | ||
self.do_test_no_exception(metavar='\r\n') | ||
|
||
# The rest would be unlikely in practice but should not fail | ||
|
||
def test_metavar_vLine_Tabulation(self): | ||
self.do_test_no_exception(metavar='\v') | ||
|
||
def test_metavar_x0b_Line_Tabulation(self): | ||
self.do_test_no_exception(metavar='\x0b') | ||
|
||
def test_metavar_f_Form_Feed(self): | ||
self.do_test_no_exception(metavar='\f') | ||
|
||
def test_metavar_x0c_Form_Feed(self): | ||
self.do_test_no_exception(metavar='\x0c') | ||
|
||
def test_metavar_File_Separator(self): | ||
self.do_test_no_exception(metavar='\x1c') | ||
|
||
def test_metavar_Group_Separator(self): | ||
self.do_test_no_exception(metavar='\x1d') | ||
|
||
def test_metavar_Record_Separator(self): | ||
self.do_test_no_exception(metavar='\x1e') | ||
|
||
def test_metavar_C1_Control_Code(self): | ||
self.do_test_no_exception(metavar='\x85') | ||
|
||
def test_metavar_Line_Separator(self): | ||
self.do_test_no_exception(metavar='\u2028') | ||
|
||
def test_metavar_Paragraph_Separator(self): | ||
self.do_test_no_exception(metavar='\u2029') | ||
|
||
def test_metavar_backslash(self): | ||
self.do_test_no_exception(metavar='\\') | ||
|
||
def test_metavar_single_quote(self): | ||
self.do_test_no_exception(metavar='\'') | ||
|
||
def test_metavar_double_quote(self): | ||
self.do_test_no_exception(metavar='\"') | ||
|
||
def test_metavar_ASCII_bell(self): | ||
self.do_test_no_exception(metavar='\a') | ||
|
||
def test_metavar_ASCII_backspace(self): | ||
self.do_test_no_exception(metavar='\b') | ||
|
||
# Unicode whitespaces per wikipedia.org/wiki/Whitespace_character | ||
|
||
def test_metavar_unicode_horizontal_tab(self): | ||
self.do_test_no_exception(metavar='\u0009') | ||
|
||
def test_metavar_unicode_line_feed(self): | ||
self.do_test_no_exception(metavar='\u000A') | ||
|
||
def test_metavar_unicode_vertical_tab(self): | ||
self.do_test_no_exception(metavar='\u000B') | ||
|
||
def test_metavar_unicode_form_feed(self): | ||
self.do_test_no_exception(metavar='\u000C') | ||
|
||
def test_metavar_unicode_carriage_return(self): | ||
self.do_test_no_exception(metavar='\u000D') | ||
|
||
def test_metavar_unicode_space(self): | ||
self.do_test_no_exception(metavar='\u0020') | ||
|
||
def test_metavar_unicode_next_line(self): | ||
self.do_test_no_exception(metavar='\u0085') | ||
|
||
def test_metavar_unicode_non_breaking_space(self): | ||
self.do_test_no_exception(metavar='\u00A0') | ||
|
||
def test_metavar_unicode_ogham_space_mark(self): | ||
self.do_test_no_exception(metavar='\u1680') | ||
|
||
def test_metavar_unicode_en_quad(self): | ||
self.do_test_no_exception(metavar='\u2000') | ||
|
||
def test_metavar_unicode_em_quad(self): | ||
self.do_test_no_exception(metavar='\u2001') | ||
|
||
def test_metavar_unicode_en_space(self): | ||
self.do_test_no_exception(metavar='\u2002') | ||
|
||
def test_metavar_unicode_em_space(self): | ||
self.do_test_no_exception(metavar='\u2003') | ||
|
||
def test_metavar_unicode_three_per_em_space(self): | ||
self.do_test_no_exception(metavar='\u2004') | ||
|
||
def test_metavar_unicode_four_per_em_space(self): | ||
self.do_test_no_exception(metavar='\u2005') | ||
|
||
def test_metavar_unicode_six_per_em_space(self): | ||
self.do_test_no_exception(metavar='\u2006') | ||
|
||
def test_metavar_unicode_figure_space(self): | ||
self.do_test_no_exception(metavar='\u2007') | ||
|
||
def test_metavar_unicode_puctuation_space(self): | ||
self.do_test_no_exception(metavar='\u2008') | ||
|
||
def test_metavar_unicode_thin_space(self): | ||
self.do_test_no_exception(metavar='\u2009') | ||
|
||
def test_metavar_unicode_hair_space(self): | ||
self.do_test_no_exception(metavar='\u200A') | ||
|
||
def test_metavar_unicode_line_separator(self): | ||
self.do_test_no_exception(metavar='\u2028') | ||
|
||
def test_metavar_unicode_paragraph_separator(self): | ||
self.do_test_no_exception(metavar='\u2029') | ||
|
||
def test_metavar_unicode_narrow_no_break_space(self): | ||
self.do_test_no_exception(metavar='\u202F') | ||
|
||
def test_metavar_unicode_medium_mathematical_space(self): | ||
self.do_test_no_exception(metavar='\u205F') | ||
|
||
def test_metavar_unicode_ideographic_space(self): | ||
self.do_test_no_exception(metavar='\u3000') | ||
|
||
def test_metavar_unicode_mongolian_vowel_separator(self): | ||
self.do_test_no_exception(metavar='\u180E') | ||
|
||
def test_metavar_unicode_zero_width_space(self): | ||
self.do_test_no_exception(metavar='\u200B') | ||
|
||
def test_metavar_unicode_zero_width_non_joiner(self): | ||
self.do_test_no_exception(metavar='\u200C') | ||
|
||
def test_metavar_unicode_zero_width_joiner(self): | ||
self.do_test_no_exception(metavar='\u200D') | ||
|
||
def test_metavar_unicode_word_joiner(self): | ||
self.do_test_no_exception(metavar='\u2060') | ||
|
||
def test_metavar_unicode_zero_width_non_breaking_space(self): | ||
self.do_test_no_exception(metavar='\uFEFF') | ||
|
||
# visible 'whitespace' characters, mainly with typesetting usages. | ||
def test_metavar_unicode_middle_dot(self): | ||
self.do_test_no_exception(metavar='\u00B7') | ||
|
||
def test_metavar_unicode_shouldered_open_box(self): | ||
self.do_test_no_exception(metavar='\u237D') | ||
|
||
def test_metavar_unicode_symbol_for_space(self): | ||
self.do_test_no_exception(metavar='\u2420') | ||
|
||
def test_metavar_unicode_blank_symbol(self): | ||
self.do_test_no_exception(metavar='\u2422') | ||
|
||
def test_metavar_unicode_open_box(self): | ||
self.do_test_no_exception(metavar='\u2423') | ||
|
||
|
||
class TestInvalidNargs(TestCase): | ||
|
||
EXPECTED_INVALID_MESSAGE = "invalid nargs value" | ||
|
3 changes: 3 additions & 0 deletions
3
Misc/NEWS.d/next/Library/2019-09-20-01-08-53.bpo-37910.hxlo0G.rst
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
argparse.py now allows metavar to be certain whitespace characters, such as | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pls use |
||
'' and '\t'. This fixes a bug where wrapping usage across multiple lines | ||
would raise an assertion error. |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add some tests in test_argparse.py verifying this behavior?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Absolutely. It might not be too soon since work's a bit busy at the moment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your review, @epicfaace. I added several new tests for a range of special characters.