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

Add support for SimpleNamespace #1132

Closed
DanielShaulov opened this issue Jan 15, 2016 · 3 comments
Closed

Add support for SimpleNamespace #1132

DanielShaulov opened this issue Jan 15, 2016 · 3 comments

Comments

@DanielShaulov
Copy link

The following code gives errors:

from types import SimpleNamespace
ns = SimpleNamespace()
ns.a = 5
print(ns.a/2)

There are two ways to make this work:

  • Treat it like a Dict[str, Any]. Allow all assignments and treat all reads as if they return Any.
  • Track the first assignment and then treat it as that type. Reading before assignment is an error.

I haven't seen any of mypy's code, but the first sounds simpler. Although there should already be something similar to the second option for module level variables.

@JukkaL JukkaL added the stubs label Jan 18, 2016
@JukkaL
Copy link
Collaborator

JukkaL commented Jan 18, 2016

Hmm I've never used SimpleNamespace. However, it doesn't look to be much like Dict[...]. A potential way to define it in a stub is like this:

_base = None  # type: Any
class SimpleNamespace(_base): ...

This means that mypy would basically assume that it can have an arbitrary, unknown set of attributes, due to the Any base class.

The change would have to go to stubs which live in the typeshed repo. Would you mind creating an issue there (https://github.com/python/typeshed/issues)?

@DanielShaulov
Copy link
Author

Hi, Thanks.
This works exactly like I meant in my first point.
A few questions:

  • can I just directly inherit in the stub? (seems like it works)
class SimpleNamespace(Any): ...
  • Is there any way to make it work like in my second option? tracking the assignments and checking the types?

@gvanrossum
Copy link
Member

Closing this, as it's a typeshed issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants