-
-
Notifications
You must be signed in to change notification settings - Fork 34.6k
gh-149614 - Restore deepcopiability of argparse.ArgumentParser instances #149617
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
base: main
Are you sure you want to change the base?
Changes from all commits
e3c3dbe
0457d17
9516991
e290465
3b5f635
0475e1a
dab2c92
b42718d
5646376
c665917
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -140,6 +140,48 @@ def test_parse_args(self): | |
| ) | ||
|
|
||
|
|
||
| class TestArgumentParserCopiable(unittest.TestCase): | ||
| def _get_parser(self): | ||
| parser = argparse.ArgumentParser(exit_on_error=False) | ||
| parser.add_argument('--foo', type=int, default=42) | ||
| parser.add_argument('bar', nargs='?', default='baz') | ||
| return parser | ||
|
|
||
| @force_not_colorized | ||
| def test_copiable(self): | ||
|
Member
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. I think we want Separately, we may also be able to fold copiable and deepcopiable into one test, since these are pretty much the same.
Contributor
Author
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. I've left these as two tests for now as there's testing for the different copy/deepcopy behaviour |
||
| import copy | ||
| parser = self._get_parser() | ||
| parser2 = copy.copy(parser) | ||
| ns = parser2.parse_args(['--foo', '123', 'quux']) | ||
| self.assertEqual(ns.foo, 123) | ||
| self.assertEqual(ns.bar, 'quux') | ||
| ns2 = parser2.parse_args([]) | ||
| self.assertEqual(ns2.foo, 42) | ||
| self.assertEqual(ns2.bar, 'baz') | ||
|
|
||
| # Test shallow copy also gets new arguments | ||
| parser.add_argument("--extra") | ||
| ns3 = parser2.parse_args(["--extra", "bar"]) | ||
| self.assertEqual(ns3.extra, "bar") | ||
|
|
||
| @force_not_colorized | ||
| def test_deepcopiable(self): | ||
| import copy | ||
| parser = self._get_parser() | ||
| parser2 = copy.deepcopy(parser) | ||
| ns = parser2.parse_args(['--foo', '123', 'quux']) | ||
|
Member
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. It'd be more robust to mutate parser2 (e.g. add_argument) and assert parser doesn't see it. As written, the test would still pass if deepcopy silently returned a shallow copy or even the same object. |
||
| self.assertEqual(ns.foo, 123) | ||
| self.assertEqual(ns.bar, 'quux') | ||
| ns2 = parser2.parse_args([]) | ||
| self.assertEqual(ns2.foo, 42) | ||
| self.assertEqual(ns2.bar, 'baz') | ||
|
|
||
| # Test deep copy does not get new arguments | ||
| parser.add_argument("--extra") | ||
| with self.assertRaises(argparse.ArgumentError): | ||
| parser2.parse_args(["--extra", "bar"]) | ||
|
|
||
|
|
||
| class TestArgumentParserPickleable(unittest.TestCase): | ||
|
|
||
| @force_not_colorized | ||
|
|
@@ -7863,12 +7905,25 @@ def fake_can_colorize(*, file=None): | |
|
|
||
| def test_fake_color_theme_matches_real(self): | ||
| from argparse import _colorless_theme | ||
|
|
||
| # Check the attributes match those of the 'real' theme | ||
| _colorize_nocolor = _colorize.get_theme(force_no_color=True).argparse | ||
| for k in _colorize_nocolor: | ||
| self.assertEqual( | ||
| getattr(_colorless_theme, k), getattr(_colorize_nocolor, k) | ||
| ) | ||
|
|
||
| def test_fake_color_theme_raises(self): | ||
| from argparse import _colorless_theme | ||
|
|
||
| # Make sure the _colorless_theme doesn't return empty strings | ||
| # for magic methods or private attributes | ||
| with self.assertRaises(AttributeError): | ||
| _colorless_theme.__unknown_dunder__ | ||
|
|
||
| with self.assertRaises(AttributeError): | ||
| _colorless_theme._private_attribute | ||
|
|
||
|
|
||
| class TestModule(unittest.TestCase): | ||
| def test_deprecated__version__(self): | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Fix a regression that broke the ability to deepcopy ``argparse.ArgumentParser`` instances. |
Uh oh!
There was an error while loading. Please reload this page.