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 · 9 comments
Open

advanced SimpleNamespace typing #688

itajaja opened this issue Nov 26, 2019 · 9 comments
Labels
topic: feature Discussions about new features for Python's type annotations

Comments

@itajaja
Copy link

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
Member

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

@itajaja
Copy link
Author

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
Member

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 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
Member

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

@srittau srittau added the topic: feature Discussions about new features for Python's type annotations label Nov 4, 2021
@bartenra
Copy link

bartenra commented Jun 8, 2022

I expect this to work:

from typing import Protocol
from types import SimpleNamespace

class Person(Protocol):
    @property
    def name(self) -> str: ...
    
    @property
    def age(self) -> int: ...

def get_person() -> Person:
    return SimpleNamespace(
        name="Alice",
        age=40
    )

But I am not getting any type checking whatsoever.

@jace
Copy link

jace commented Nov 23, 2022

I use SimpleNamespace for one-off cases where specifying a dataclass or Protocol would be unnecessary boilerplate. I'd like to be able to type SimpleNamespace as a generic:

def returns_namespace() -> SimpleNamespace[int]:
    return SimpleNamespace(a=1, b=2)

This is not possible at the moment as SimpleNamespace accepts no type arguments, but it'll be nice to have this as a first step before TypedDict-level typing.

@szabi
Copy link

szabi commented Mar 24, 2023

def returns_namespace() -> SimpleNamespace[int]:
    return SimpleNamespace(a=1, b=2)

That should be -> SimpleNamespace[[int, int]] (cf. Callable)

@antonagestam
Copy link

Documentation of SimpleNamespace mentions:

However, for a structured record type use namedtuple() instead.

Shouldn't that cover the usecases brought up here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: feature Discussions about new features for Python's type annotations
Projects
None yet
Development

No branches or pull requests

8 participants