I derive from str
to get strongly typed string references. They look like below, which are redundant, and I'd like to avoid the redundancy:
class VarRef(str):
def __new__(cls, *args, **kw):
return str.__new__(cls, *args, **kw)
class SceneRef(str):
def __new__(cls, *args, **kw):
return str.__new__(cls, *args, **kw)
class AmbienceRef(str):
def __new__(cls, *args, **kw):
return str.__new__(cls, *args, **kw)
This ensures, for example, that I need an explicit cast to get a VarRef
but that otherwise it functions like a string.
I have many more and hoping there's a way to copying the same __new__
constructor for each one.
I use these only for MyPy type checking, so at runtime it'd be fine if they were normal strings. If there is a mechanism to tag them only for type checking I'd be happy to use that approach.