5

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.

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks, unfortunately considering dataclasses, named tuples, enums and all the usual suspects turned out Simplenamespace was much better fitting my use case, so its a bummer pycharm renders it unusable, despite coming from python's core Lib\types.py

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.