mirror of
				https://github.com/python/cpython.git
				synced 2025-10-30 13:11:29 +00:00 
			
		
		
		
	gh-61011: Fix inheritance of nested mutually exclusive groups in argparse (GH-125210)
Previously, all nested mutually exclusive groups lost their connection to the group containing them and were displayed as belonging directly to the parser. Co-authored-by: Danica J. Sutherland <djsutherland@users.noreply.github.com>
This commit is contained in:
		
							parent
							
								
									0135848059
								
							
						
					
					
						commit
						18c7449768
					
				
					 4 changed files with 39 additions and 1 deletions
				
			
		|  | @ -1521,7 +1521,11 @@ def _add_container_actions(self, container): | ||||||
|         # NOTE: if add_mutually_exclusive_group ever gains title= and |         # NOTE: if add_mutually_exclusive_group ever gains title= and | ||||||
|         # description= then this code will need to be expanded as above |         # description= then this code will need to be expanded as above | ||||||
|         for group in container._mutually_exclusive_groups: |         for group in container._mutually_exclusive_groups: | ||||||
|             mutex_group = self.add_mutually_exclusive_group( |             if group._container is container: | ||||||
|  |                 cont = self | ||||||
|  |             else: | ||||||
|  |                 cont = title_group_map[group._container.title] | ||||||
|  |             mutex_group = cont.add_mutually_exclusive_group( | ||||||
|                 required=group.required) |                 required=group.required) | ||||||
| 
 | 
 | ||||||
|             # map the actions to their new mutex group |             # map the actions to their new mutex group | ||||||
|  |  | ||||||
|  | @ -2942,6 +2942,35 @@ def test_groups_parents(self): | ||||||
|     def test_wrong_type_parents(self): |     def test_wrong_type_parents(self): | ||||||
|         self.assertRaises(TypeError, ErrorRaisingArgumentParser, parents=[1]) |         self.assertRaises(TypeError, ErrorRaisingArgumentParser, parents=[1]) | ||||||
| 
 | 
 | ||||||
|  |     def test_mutex_groups_parents(self): | ||||||
|  |         parent = ErrorRaisingArgumentParser(add_help=False) | ||||||
|  |         g = parent.add_argument_group(title='g', description='gd') | ||||||
|  |         g.add_argument('-w') | ||||||
|  |         g.add_argument('-x') | ||||||
|  |         m = g.add_mutually_exclusive_group() | ||||||
|  |         m.add_argument('-y') | ||||||
|  |         m.add_argument('-z') | ||||||
|  |         parser = ErrorRaisingArgumentParser(prog='PROG', parents=[parent]) | ||||||
|  | 
 | ||||||
|  |         self.assertRaises(ArgumentParserError, parser.parse_args, | ||||||
|  |             ['-y', 'Y', '-z', 'Z']) | ||||||
|  | 
 | ||||||
|  |         parser_help = parser.format_help() | ||||||
|  |         self.assertEqual(parser_help, textwrap.dedent('''\ | ||||||
|  |             usage: PROG [-h] [-w W] [-x X] [-y Y | -z Z] | ||||||
|  | 
 | ||||||
|  |             options: | ||||||
|  |               -h, --help  show this help message and exit | ||||||
|  | 
 | ||||||
|  |             g: | ||||||
|  |               gd | ||||||
|  | 
 | ||||||
|  |               -w W | ||||||
|  |               -x X | ||||||
|  |               -y Y | ||||||
|  |               -z Z | ||||||
|  |         ''')) | ||||||
|  | 
 | ||||||
| # ============================== | # ============================== | ||||||
| # Mutually exclusive group tests | # Mutually exclusive group tests | ||||||
| # ============================== | # ============================== | ||||||
|  |  | ||||||
|  | @ -1814,6 +1814,7 @@ Reuben Sumner | ||||||
| Eryk Sun | Eryk Sun | ||||||
| Sanjay Sundaresan | Sanjay Sundaresan | ||||||
| Marek Šuppa | Marek Šuppa | ||||||
|  | Danica J. Sutherland | ||||||
| Hisao Suzuki | Hisao Suzuki | ||||||
| Kalle Svensson | Kalle Svensson | ||||||
| Andrew Svetlov | Andrew Svetlov | ||||||
|  |  | ||||||
|  | @ -0,0 +1,4 @@ | ||||||
|  | Fix inheritance of nested mutually exclusive groups from parent parser in | ||||||
|  | :class:`argparse.ArgumentParser`. Previously, all nested mutually exclusive | ||||||
|  | groups lost their connection to the group containing them and were displayed | ||||||
|  | as belonging directly to the parser. | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Serhiy Storchaka
						Serhiy Storchaka