Why SimpleNamespace code completion does not work in pycharm editor?
from types import SimpleNamespace
sn= SimpleNamespace(param_a = '1')
sn. # pressing '.' dot I'm NOT offered param_a
This does work in pycharm python console, suggesting SimpleNamespace instance must be somehow 'computed' at runtime first. However if purpose of SimpleNamespace is to provide a namespace, to group a few parameters and access them via dot notation, then, while technically I can still type sn.param_a manually, without code completion is the whole thing stripped most of its usefulness and I'd likely switch to plain class where code completion does work in editor (class instantiated or not) Tried on different machines\pycharm versions so does not look like some quirk of my environment.
Why SimpleNamespace code completion does not work in pycharm editor?
Because PyCharm doesn't have enough smarts to handle SimpleNamespaces specially (i.e. to know that the kwargs are just assigned into instance attributes).
This does work in pycharm python console, suggesting SimpleNamespace instance must be somehow 'computed' at runtime first
Yes, the console does something like vars()
or dir()
on the live object to introspect it. Static code analysis is an entirely different thing to inspecting objects at runtime.
The instance isn't "computed" in any special way at runtime.
I'd likely switch to plain class where code completion does work in editor (class instantiated or not)
You might want to use dataclasses
(or attrs
) for brevity; both are well supported by PyCharm.