Skip to content
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

Allow specifying SimpleNamespace attributes as a mapping/iterable in a positional argument #108191

Closed
serhiy-storchaka opened this issue Aug 21, 2023 · 4 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement

Comments

@serhiy-storchaka
Copy link
Member

serhiy-storchaka commented Aug 21, 2023

Feature or enhancement

Has this already been discussed elsewhere?

I have already discussed this feature proposal on Discourse

Links to previous discussion of this feature:

#96145
https://mail.python.org/archives/list/python-dev@python.org/message/TYIXRGOLWGV5TNJ3XMV443O3RWLEYB65/

Proposal:

It was discussed in the context of adding controversial AttrDict. The reason for adding AttrDict was to allow JSON object attributes to be accessed as Python object attributes rather than dict keys. The reason for not using SimpleNamespace for this was that you need to use a wrapper lambda x: SimpleNamespace(**x), which makes this solution less obvious.

Supporting a positional argument in SimpleNamespace(), which can be a mapping or an iterable of key-value pairs (like in the dict constructor) will make using SimpleNamespace with JSON more convenient.

SimpleNamespace({'a': 1, 'b': 2}) is the same as SimpleNamespace([('a', 1), ('b', 2)]) is the same as SimpleNamespace(a=1, b=2).

Linked PRs

@serhiy-storchaka serhiy-storchaka added the type-feature A feature request or enhancement label Aug 21, 2023
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Aug 21, 2023
…e constructor

SimpleNamespace({'a': 1, 'b': 2}) and SimpleNamespace([('a', 1), ('b', 2)])
are now the same as SimpleNamespace(a=1, b=2).
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Aug 21, 2023
…e constructor

SimpleNamespace({'a': 1, 'b': 2}) and SimpleNamespace([('a', 1), ('b', 2)])
are now the same as SimpleNamespace(a=1, b=2).
@CAM-Gerlach CAM-Gerlach changed the title Allow to specify SimpleNamespace attributes as a positional argument Allow specifying SimpleNamespace attributes as positional arguments Aug 21, 2023
@CAM-Gerlach CAM-Gerlach changed the title Allow specifying SimpleNamespace attributes as positional arguments Allow specifying SimpleNamespace attributes as a mapping/iterable in a positional argument Aug 21, 2023
@serhiy-storchaka
Copy link
Member Author

@rhettinger, @ericsnowcurrently, I would like to know your opinion about this feature.

Raymond, would it be useful to you, in any way?

Eric, is it compatible with your vision of SimpleNamespace?

If both answers are "yes", I'll merge the PR. If both answers are "no", I'll withdraw it and close the issue. If answers are mixed, we should have a discussion.

@ericsnowcurrently
Copy link
Member

Yeah, I'm fine with this. It didn't seem worth it originally but it certainly isn't a bad thing.

@iritkatriel iritkatriel added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Nov 27, 2023
serhiy-storchaka added a commit that referenced this issue Apr 24, 2024
…tructor (GH-108195)

SimpleNamespace({'a': 1, 'b': 2}) and SimpleNamespace([('a', 1), ('b', 2)])
are now the same as SimpleNamespace(a=1, b=2).
@vstinner
Copy link
Member

Congrats for landing this new feature.

@serhiy-storchaka
Copy link
Member Author

It is a minor feature. It was hanging for such long time only because I was not sure that it is worthy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

4 participants