Add support for SimpleNamespace Intellisense autocompletion #2630
Replies: 7 comments
-
Typeshed defines class SimpleNamespace:
def __init__(self, **kwargs: Any) -> None: ...
def __getattribute__(self, name: str) -> Any: ...
def __setattr__(self, name: str, value: Any) -> None: ...
def __delattr__(self, name: str) -> None: ... We're behaving as expected here, as the stub just says that you can give it anything for the kwargs, then access anything with the For this to be richer, there would need to be a PEP describing how should work in type checkers, similarly to what was done for TypedDict. At the moment, there's no change in typeshed that would make this richer, and we aren't keen on special casing types like this without a huge amount of motivating examples. What is your use case for this? Are types like |
Beta Was this translation helpful? Give feedback.
-
Hi, thanks for your reply. I am using SimpleNamespace as a replacement for NamedTuple where defining the namedtuple type can be skipped. from types import SimpleNamespace
APP_CONFIGURATION = SimpleNamespace(
foo="foo",
bar="baz"
)
# value can be used somewhere else
print(APP_CONFIGURATION.foo) instead of from typing import NamedTuple
AppConfigTuple = NamedTuple('AppConfigTuple', [('foo', str), ('bar', str)])
APP_CONFIGURATION = AppConfigTuple(
foo='foo',
bar='baz',
)
# value can be used somewhere else
print(APP_CONFIGURATION.foo) Autocompletion on SimpleNamespace the same way it is done for NamedTuple would be most helpful. |
Beta Was this translation helpful? Give feedback.
-
Okay; this behavior is still expected at the moment because there is no special casing defined for SimpleNamespace in terms of typing. NamedTuple is special, and has a PEP that describes how it should operate with type checking (which is what Pylance is at the end of the day). I'll leave this as an enhancement request to see if others want it, but we are a bit wary to add more special cases that aren't defined in PEPs, and the workaround is what we would have expected people to use to get full type info (use NamedTuple). |
Beta Was this translation helpful? Give feedback.
-
I am using SimpleNamespace as a quick container for related variables during prototyping and miss autocomplete - but even more the ability to rename all occurrences of an attribute. As an example consider creating an image processing pipeline with different stages. In each stage i store multiple intermediate results as an attribute in a corresponding SimpleNamespace. This offers me a better overview and allows me to pass all results linked to a given stage as one variable to a different function or the calling context. Additional intermediate results come an go during prototyping - and I would like to keep the grouping SimpleNamespace offers me, but avoid the burden of creating new entries in a NamedTuple everytime I want to try something out. I believe that for a final design a NamedTuple is the better choice, but a SimpleNamespace is faster to apply and adapt. For me - autocomplete and rename limited to the current scope(e.g. function body) would already be really helpful. |
Beta Was this translation helpful? Give feedback.
-
Moving this issue to discussion as an enhancement request for comments and upvotes. |
Beta Was this translation helpful? Give feedback.
-
I can't use Ex) out = SimpleNamespace(
root_py=text,
root_txt=text,
audio=SimpleNamespace(
bgm=SimpleNamespace(
underwater1=text,
underwater2=text,
),
sfx=SimpleNamespace(
collect=text,
dash=text,
head=text,
jump=text,
land=text,
),
),
fonts=SimpleNamespace(
monogram_ttf=text,
),
images=SimpleNamespace(
fallback_png=text,
chests=SimpleNamespace(
big_chest_png=text,
small_chest_png=text,
# ...
),
# ...
),
# ...
) With |
Beta Was this translation helpful? Give feedback.
-
simpleNamespace will be more pithy. return namedtuple('R', ['code', 'episode'])(code_number,episode)
# vs
return simpleNamespace(code=code_number, episode=episode) Once there are more parameters it becomes harder to read the code |
Beta Was this translation helpful? Give feedback.
-
Currently SimpleNamespace doesn't populate autocomplete suggestions for set attributes
Code Example
Same as: microsoft/python-language-server#2108
Beta Was this translation helpful? Give feedback.
All reactions