4 ms·
the lsp will complain about i~. The language happily does it anyway.
by vrighter 2y ago
the lsp will complain about i~.
The language happily does it anyway.
- MrJohz 2y agoWhich is why you use a type checker to ensure that it never happens in the codebase. If there is no line of code that ever does something like int_field: int = "string" or self.int_field = "string" Then you can be confident that the language will never store a string value in that integer field. And if you expand this to cover all fields and variables in your codebase, you can see that, if you use the type system correctly, the language won't violate those constraints. There are some exceptions to this, like I said in the previous comment, but I think when getting started with typed Python, it's easier to assume that the types will be valid, because that's the case almost all of the time. Usually, overriding type assertions requires some kind of explicit cast or statement, and this is usually documented.
- vrighter 2y agoanother way to accomplish this, and also eliminate any and all possibilities of human error, is to make the type checker part of the language. You're still doing the same thing, just with extra steps and implicit rules