8000 gh-108191: Add support of positional argument in SimpleNamespace constructor by serhiy-storchaka · Pull Request #108195 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-108191: Add support of positional argument in SimpleNamespace constructor #108195

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

Merged
merged 17 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Apply suggestions from code review
Co-authored-by: Eric Snow <ericsnowcurrently@gmail.com>
Co-authored-by: Pieter Eendebak <pieter.eendebak@gmail.com>
  • Loading branch information
3 people authored Mar 7, 2024
commit 299d437f02fc1332f9c9a4f773875a93280c69cc
5 changes: 3 additions & 2 deletions Doc/library/types.rst
Original file line number Diff line number Diff line change
Expand Up @@ -488,8 +488,9 @@ Additional Utility Classes and Functions
Unlike :class:`object`, with :class:`!SimpleNamespace` you can add and remove
attributes.

:py:class:`SimpleNamespace` objects may be initialized either
with keyword arguments or with a single positional argument.
:py:class:`SimpleNamespace` objects may be initialized
in the same way as :class:`dict`: either with keyword arguments,
with a single positional argument, or with both.
When initialized with keyword arguments,
those are directly added to the underlying namespace.
Alternatively, when initialized with a positional argument,
Expand Down
2 changes: 1 addition & 1 deletion Doc/whatsnew/3.13.rst
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ types

* :class:`~types.SimpleNamespace` constructor now allows specifying initial
values of attributes as a positional argument which must be a mapping or
an iterable or key-value pairs.
an iterable of key-value pairs.
(Contributed by Serhiy Storchaka in :gh:`108191`.)

typing
Expand Down
6 changes: 6 additions & 0 deletions Lib/test/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -1741,6 +1741,12 @@ def test_constructor(self):
ns4 = types.SimpleNamespace({'x': 1, 'y': 2}, x=4, z=3)
ns5 = types.SimpleNamespace([['x', 1], ['y', 2]], x=4, z=3)
ns6 = types.SimpleNamespace(UserDict({'x': 1, 'y': 2}), x=4, z=3)
ns7 = types.SimpleNamespace({'x': 1, 'y': 2})
ns8 = types.SimpleNamespace([['x', 1], ['y', 2]])
ns9 = types.SimpleNamespace([], x=4, z=3)
ns10 = types.SimpleNamespace({}, x=4, z=3)
ns11 = types.SimpleNamespace([])
ns12 = types.SimpleNamespace({})

with self.assertRaises(TypeError):
types.SimpleNamespace([], [])
Expand Down
0