Throw a TypeError exception if using type annotations (type hints) & the passed arg or return value is the wrong type

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. Here is more info: https://dev.to/robdwaller/how-php-type-declarations-actually-work-1mm5 I would like to see this PHP feature in Python. A strict types declaration line can be added at the top any file or/and it could be set globally at the start of the script by doing something like `sys.strict_types = True`.

You can do this without having to change the language by writing a decorator that enforces the types. The type information is all recoverable from __attributes__. Be aware though that while enforcing types like int or str is easy, enforcing something list List[int] is less obvious -- you probably won't want to slow down computations with O(N) type checks. IIUC there are existing packages that expose decorators that do this, but I have never tried to use them. --Guido On Tue, Dec 24, 2019 at 6:37 AM Andrew T <andym2161@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. Here is more info: https://dev.to/robdwaller/how-php-type-declarations-actually-work-1mm5
I would like to see this PHP feature in Python. A strict types declaration line can be added at the top any file or/and it could be set globally at the start of the script by doing something like `sys.strict_types = True`. _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@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/DS75G2... Code of Conduct: http://python.org/psf/codeofconduct/
-- --Guido van Rossum (python.org/~guido) *Pronouns: he/him **(why is my pronoun here?)* <http://feministing.com/2015/02/03/how-using-they-as-a-singular-pronoun-can-c...>

On Dec 24, 2019, at 05:34, Andrew T <andym2161@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.
participants (3)
-
Andrew Barnert
-
Andrew T
-
Guido van Rossum