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)
I think the most popular library for this is typeguard:
https://github.com/agronholm/typeguard
I've also seen a couple of other similar libraries:
https://github.com/FelixTheC/strongtyping/issues/33 https://github.com/seandstewart/typical/issues/24
On Sat, Aug 22, 2020 at 3:01 PM 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-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/UNLCMU... Code of Conduct: http://python.org/psf/codeofconduct/
Hi Rawmin,
Have you considered using MyPy (http://mypy-lang.org/)?
Best,
[image: --] Felipe V. Rodrigues [image: https://%5Dabout.me/fvr https://about.me/fvr?promo=email_sig
On Sat, Aug 22, 2020 at 9:58 AM 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-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/UNLCMU... Code of Conduct: http://python.org/psf/codeofconduct/
Well I wanted a function to check it at run-time. I used to use mypy as pylint of my VS Code but now I've created a decorator for it. But thanks