raise TypeError when giving wrong type of argument in functions
I wanted to know if there could be TypeError when giving wrong type for arguments in functions (This usually happens when using other module's function) e.g: def sum(nom1: int, nom2: int): nom = nom1 + nom2 return nom print(sum('hello',2)) if you run this code you will get TypeError for line 2 because 'you can only concatenate str (not "int") to str' But what am i saying is can it raise TypeError for line 4 because I gave 'hello' as nom1 and nom1 should be int Now if I want to check arguments types I should use: def sum(nom1: int, nom2: int): if isinstance(nom1, int) and isinstance(nom1, int): nom = nom1 + nom2 return nom else: raise TypeError('nom1 and nom2 should be int') print(sum('hello',2)) But if they can add what am I am i saying it can decrease lines of this function by 50% and also function author should not worry about checking types anymore! (I know I could use 'assert' but I just wanted to write it as simple as possible)
On 22 Aug 2020, at 15:00, rawmin.rx@gmail.com wrote:
I wanted to know if there could be TypeError when giving wrong type for arguments in functions (This usually happens when using other module's function)
e.g:
def sum(nom1: int, nom2: int): nom = nom1 + nom2 return nom print(sum('hello',2))
if you run this code you will get TypeError for line 2 because 'you can only concatenate str (not "int") to str' But what am i saying is can it raise TypeError for line 4 because I gave 'hello' as nom1 and nom1 should be int
Now if I want to check arguments types I should use:
def sum(nom1: int, nom2: int): if isinstance(nom1, int) and isinstance(nom1, int): nom = nom1 + nom2 return nom else: raise TypeError('nom1 and nom2 should be int') print(sum('hello',2))
But if they can add what am I am i saying it can decrease lines of this function by 50% and also function author should not worry about checking types anymore! (I know I could use 'assert' but I just wanted to write it as simple as possible)
Python does not check type annotations at runtime, although you could write a decorator that does this for you. It is unlikely that this will anytime soon. Ronald — Twitter / micro.blog: @ronaldoussoren Blog: https://blog.ronaldoussoren.net/ <https://blog.ronaldoussoren.net/>
On 8/22/20 10:34 AM, rawmin.rx@gmail.com wrote:
No. I wanted to know if they can add this suggestion in next versions
It has been decided that Python will NOT enforce type annotations itself. There are modules where you can add an annotation to your function to add the checking yourself. -- Richard Damon
On 8/22/20 11:59 AM, rawmin.rx@gmail.com wrote:
Oh OK Thank you. But just a question: do you know any of these modules?
The other thread someone suggested: https://github.com/agronholm/typeguard https://github.com/FelixTheC/strongtyping/issues/33 https://github.com/seandstewart/typical/issues/24 -- Richard Damon
On Sat, Aug 22, 2020 at 7:37 AM <rawmin.rx@gmail.com> wrote:
No. I wanted to know if they can add this suggestion in next versions
That will not happen. You may want to familiarize yourself with the intent of type annotations: https://www.python.org/dev/peps/pep-0526/#non-goals https://www.python.org/dev/peps/pep-3107/#rationale
participants (4)
-
Eric Fahlgren
-
rawmin.rx@gmail.com
-
Richard Damon
-
Ronald Oussoren