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

advanced SimpleNamespace typing #688

Open
itajaja opened this issue Nov 26, 2019 · 5 comments
Open

advanced SimpleNamespace typing #688

itajaja opened this issue Nov 26, 2019 · 5 comments

Comments

@itajaja
Copy link

@itajaja itajaja commented Nov 26, 2019

the same way we now have TypedDict, it would be nice to have typed SimpleNamespace with equivalent semantics, so that nested structures can be easily checked. eg

class MySubType(TypedSimpleNamespace):
   bar: int

class MyType(TypedSimpleNamespace):
   foo: MySubType

# correct
a: MyType = SimpleNamespace(foo=SimpleNamespace(bar=1))
# incorrect, `bar` should be a number
a: MyType = SimpleNamespace(foo=SimpleNamespace(bar='abc'))
@JelleZijlstra
Copy link
Contributor

@JelleZijlstra JelleZijlstra commented Nov 26, 2019

I think Protocol can basically be used as your proposed TypedSimpleNameSpace.

@itajaja
Copy link
Author

@itajaja itajaja commented Nov 26, 2019

as far as I can tell, protocols can be used to cast the simple namespace into, but it won't help me typechecking SimpleNamespce, like in my example snippet. can you provide an example when using protocols where the correct line would succeed and the wrong line would file? I don't believe it's possible without adding a specific SimpleNamespace type

@ilevkivskyi
Copy link
Collaborator

@ilevkivskyi ilevkivskyi commented Nov 26, 2019

I think Protocol can basically be used as your proposed TypedSimpleNameSpace.

Almost, but not completely. IIUC auto-generated __init__() signature is important. On the other hand, the latter can be added when support for key types is added.

@itajaja
Copy link
Author

@itajaja itajaja commented Nov 26, 2019

the latter can be added when support for key types is added.

thanks @ilevkivskyi can you link to related resources about it?

@ilevkivskyi
Copy link
Collaborator

@ilevkivskyi ilevkivskyi commented Nov 26, 2019

We don't have a "centralized" issues to track this, but you can watch python/mypy#7856 and/or python/mypy#7790

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

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.