method ... def construct_from_string(cls, string): ... pattern = re.compile(r"^my_type\[(?P.+)\]$") ... match = pattern.match(string) ... if match: ... return cls(**match.groupdict()) ... else: ... raise TypeError( ... f"Cannot construct a '{cls.__name__}' from '{string}'" ... ) z.'construct_from_string' expects a string, got z