8

Say I am defining a function that returns a types.SimpleNamespace. I would like to type-hint the result:

from types import SimpleNamespace

def func() -> SimpleNamespace(x: int, y: str):  # SyntaxError! What should be used instead?
    return SimpleNamespace(x=3, y='abc')

Note that SimpleNamespace is commonly used as alternative to tuple in which data members are named. For tuples, a corresponding type hint exists:

from typing import Tuple

def func() -> Tuple[int, str]:  # OK
    return 3, 'abc'

BTW, today I am using the following for documentation's sake:

from types import SimpleNamespace

def func() -> SimpleNamespace(x=int, y=str):  # Seems to work fine
    return SimpleNamespace(x=3, y='abc')

But this is not standard (so it won't be supported by type checkers), and perhaps not proper Python (or is it?).

amka66
  • 699
  • 7
  • 11
  • The type I'd merely `SimpleNamespace` – juanpa.arrivillaga Jan 01 '21 at 18:06
  • 2
    "Note that SimpleNamespace is commonly used as alternative to tuple in which data members are named. " No it isn't. It is really not a good replacement for a `tuple`, use a `NamedTuple` instead, or just a regular class (maybe a dataclass to avoid boilerplate). – juanpa.arrivillaga Jan 01 '21 at 18:08
  • @Carcigenicate: (1) You are indicating that SimpleNamespace is not like tuple in that it is mutable -- indeed correct, but still the question remains in case one would like a mutable object, or if one prefers the benefits of named data members. – amka66 Jan 01 '21 at 18:09
  • 1
    @Carcigenicate @juanpa.arrivillaga You are suggesting `NamedTuple` and `dataclass` as alternatives. Indeed they have named data members (check), both support type hinting (check), `NamedTuple` is even immutable and extends type `tuple` if one would like that, but _both define a new type by name_. I don't want to define a new type every time a function returns more than a single value (see [question](https://stackoverflow.com/questions/1123000/does-python-have-anonymous-classes)). – amka66 Jan 01 '21 at 18:45
  • @juanpa.arrivillaga As for using `SimpleNamespace` as the type hint -- it is like using `tuple` as a type hint instead of the elaborate `Tuple[int, str]`. – amka66 Jan 01 '21 at 18:48
  • @amk66 from the docs: `Changed in version 3.9: Attribute order in the repr changed from alphabetical to insertion (like dict).` Are you using python <3.9? – aerijman Jan 01 '21 at 19:09
  • @aerijman I don't see how are `repr` and attribute order related to this question. – amka66 Jan 01 '21 at 19:45
  • @amk66 `def func() -> SimpleNamespace(x=int, y=str): # Seems to work fine` but `def func() -> SimpleNamespace(x: int, y: str): # SyntaxError! What should be used instead?`. The first IS proper Python for version 3.3 to 3.9. The second is for >3.9 and I assume that your python is <3.9. If this does not answer your question, I missunderstood it and I apologize. – aerijman Jan 01 '21 at 19:52
  • @amka66 you are asking for something that doesn't exist in python: " I don't want to define a new type every time a function returns more than a single value", although note, in equivalent constructs in other languages, e.g Scala, you would literally be creating a new type. Why don't you want to create a new type? Now, `SimpleNamespace` could support something like what you want, but it doesn't, so maybe open an issue? I suspect people will just suggest you use `NamedTuple` or another custom class, though. – juanpa.arrivillaga Jan 01 '21 at 19:57
  • @juanpa.arrivillaga I agree the standard library may support it, in case it doesn't (say, implementing `__getitem__` for `SimpleNamespace`) . As for defining a new `NamedTuple` every time a function returns more than a single value – this will result in _two_ definitions per function, and _two_ new identifiers per function, which IMHO is too cumbersome and overwhelming, especially if there is another option... – amka66 Jan 02 '21 at 02:18
  • 1
    @aerijman I tried python 3.9 and still syntax error on `SimpleNamespace(x: int, y: str)`. Are you saying this is valid syntax in python 3.10? (i am not sure i understand) – amka66 Jan 02 '21 at 02:34

0 Answers0