[Python-ideas] Accepting a function argument of a particular type specified by the user

2021-04-25 Thread Shreyan Avigyan
Think it like this. We have this code in Python :- def add(a, b): return a + b Here we are taking two arguments a, b and then returning a + b. But we can pass in instance of any class like str, int, float, dict, user-defined class, etc. But we only want to add int here. Here we can modify i

[Python-ideas] Re: Accepting a function argument of a particular type specified by the user

2021-04-25 Thread Richard Damon
On 4/25/21 9:09 AM, Shreyan Avigyan wrote: > Think it like this. We have this code in Python :- > > def add(a, b): > return a + b > > Here we are taking two arguments a, b and then returning a + b. But we can > pass in instance of any class like str, int, float, dict, user-defined class, > et

[Python-ideas] Re: Accepting a function argument of a particular type specified by the user

2021-04-25 Thread Damian Shaw
Typeguard provides this functionality: https://typeguard.readthedocs.io/en/latest/userguide.html It's not perfect but that's because runtime type hints have lots of restrictions on what can be reasoned about them. But for simple and common cases it works very well. -- Damian (he / him) On Sun, A

[Python-ideas] Re: Accepting a function argument of a particular type specified by the user

2021-04-25 Thread Shreyan Avigyan
I am aware of all those libraries that allows us to type check. But it would be nice to have this feature built-in. I'm not talking about modifying type annotation but introducing a new feature. Like, def add(int a, int b): return a + b If type is not provided then take in any parameter typ

[Python-ideas] Re: Accepting a function argument of a particular type specified by the user

2021-04-25 Thread Chris Angelico
On Mon, Apr 26, 2021 at 4:19 AM Shreyan Avigyan wrote: > > I am aware of all those libraries that allows us to type check. But it would > be nice to have this feature built-in. I'm not talking about modifying type > annotation but introducing a new feature. Like, > > def add(int a, int b): >

[Python-ideas] Re: Accepting a function argument of a particular type specified by the user

2021-04-25 Thread Shreyan Avigyan
First of all the use case is when for example we have UserDefined class and my function or another class can only handle instances of the UserDefined class. Second of all why not add a built-in type check decorator then? ___ Python-ideas mailing list --

[Python-ideas] Re: Accepting a function argument of a particular type specified by the user

2021-04-25 Thread Chris Angelico
On Mon, Apr 26, 2021 at 4:58 AM Shreyan Avigyan wrote: > > First of all the use case is when for example we have UserDefined class and > my function or another class can only handle instances of the UserDefined > class. Second of all why not add a built-in type check decorator then? > What if i

[Python-ideas] Re: Accepting a function argument of a particular type specified by the user

2021-04-25 Thread Shreyan Avigyan
Thanks for clarifying. And I agree with you. Not writing checking code will make the function more flexible. Thanking you, With Regards ___ Python-ideas mailing list -- [email protected] To unsubscribe send an email to [email protected]

[Python-ideas] Re: Accepting a function argument of a particular type specified by the user

2021-04-25 Thread Richard Damon
On 4/25/21 3:08 PM, Shreyan Avigyan wrote: > Thanks for clarifying. And I agree with you. Not writing checking code will > make the function more flexible. > > Thanking you, > With Regards My experiance is that the type annotaions let my IDE warn me of wrong parameters, or give me hints as to wha