Re:Strict Python function parameters
by zzzeek on Sunday January 23, 2022 @01:10PM
(#30048588)
typing tools won't know what the above decorator does, without some fancy PEP-612 annotations and I'm not sure pep612 can cover that level of automation. I think intent and all is clear for everyone just having the construction done in the method body for the (IMO, should be) seldom case this is needed
[ Parent
| on HN ]
Re:Strict Python function parameters
by IgorPartola on Sunday January 23, 2022 @01:33PM
(#30048875)
Eh. I don't use typing tools for most things because (and I know this isn't a popular opinion even though I believe it's the correct one) they rarely save you from real world problems. They are mostly useful for auto-complete in your IDE, so you have to weigh the time you spend annotating your code to help your IDE auto-complete function signatures vs the time the auto-complete saves you. I think that if you use this as:
def foo(bar: list):
print(bar)
that type checking will be happy.
Re:Strict Python function parameters
by daanluttik on Tuesday January 25, 2022 @06:11AM
(#30070535)
If you use it in conjunction with mypy it definitely saves you from alot of real word issues. In addition, it is also an amazing way of documenting an important element of your code. And annotations van be used by modules (like pydantic) to get even more use. Improvement to anotations have been the biggest improvement in python since 3.6 imho.
Re:Strict Python function parameters
by IgorPartola on Tuesday January 25, 2022 @03:33PM
(#30077654)
What kind of bugs have you encountered in practice that were caught by mypy but not at runtime, manual, or automated testing? I am genuinely curious because in my experience the only time I have seen this is when you either transpose the order or arguments to a function (detected the first time you run it) or if you pass None when an actual value was necessary (detected the first time you run it).