TLDR - not worth the hassle, in my opinion:
Between linting/mypy and unit tests that should cover most of your needs and little tricks around class analysis/metaclasses are likely not worth the extra cognitive load that they bring in. You will only "fail" your decorating once, but will have to read exotic scaffolding code for what you want to do every time.
details - what does actual use get flagged as?
i.e. if badchild1.name.startswith("John"):
will fail at runtime and I'd expect mypy or pylint for example to flag on analysis as well, as it's going to be a method object. So will concatenation. The only real candidates for oversight are f-strings, straight out booleans or ==
, !=
equality comparisons that don't care that it's not a string.
pylint has this to say:
test_171_prop.py:11 Method 'name' was expected to be 'property', found it instead as 'method' (invalid-overridden-method)
mypy had no issues however.
However, if I add this:
child = Child()
print("name:" + child.name)
then mypy says:
test_171_prop.py:16: error: Unsupported operand types for + ("str" and "Callable[[], str]")
Found 1 error in 1 file (checked 1 source file)
And running the code with the 2 new lines says:
TypeError: can only concatenate str (not "method") to str