On Dec 24, 2019, at 05:34, Andrew T <andym2...@gmail.com> wrote: > > This is a feature PHP has had since PHP7 and is what I use all the time with > PHP. It works on a file by file basis (I guess for backward compatibility), a > declare line needs to be at the top of a file `declare(strict_types=1);` If > this line is at the top of the file, then a TypeError is thrown on any > method/function that uses type hints in the current file if the passed arg or > return value is the wrong type.
PHP’s feature doesn’t really make sense for Python. First, PHP is a weakly typed language; Python is a strongly but dynamically typed language. In PHP, without strict_type, if you pass the int 3 to a function that wants a string, it gets magically coerced to the string “3”. In Python, it remains an int. If you try to do something that both ints and strings can do, like hash it, it hashes as an int, not a string. If you try to do something an int can’t do, like iterate it or search it with a reflex, you get a TypeError. So, most of the benefits PHP gets from this feature are already there in Python. More importantly, PHP has an almost trivial type system. The only types you can declare are the native types—a 32-bit int, a byte string, and a few others. You can’t declare something to take, say, an Iterable of Spam objects. That means everything can be checked in constant time, but that wouldn’t be true in Python. Some things would take linear time to check. Others can’t be checked at all (e.g., you can’t tell whether a generator is an Iterable of Spam without consuming all the elements). So if you wanted this to work in Python, you’d have to change it so that every value keeps track of enough information to know which static types it can conform to. For example, a list would have to know which types have been inserted into it so it could tell you whether it’s actually an Iterable of Spam or not. And a generator would have to somehow know the types of everything it might conceivably generate, which is probably impossible without incorporating a static type inference system like mypy into the dynamic runtime. If you really just do want to check a handful of types like PHP, you could trivially write a decorator for that. But if you want to actually check Python’s full type system, you’d need massive changes to totally integrate the static and dynamic type systems. _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/Q7Z3I2BK54TWC4DV2SMA4JRUHJBTHMCK/ Code of Conduct: http://python.org/psf/codeofconduct/