Re: Typing, how come that :int is not ensuring int parameter?

2020-06-11 Thread DL Neil via Python-list

On 12/06/20 9:32 AM, zljubi...@gmail.com wrote:

OK, as I can see nothing is enforced but I can use mypy and tests for the 
purpose.


Exactly! Well done - Python uses 'duck typing' and thus when the Typing 
module was added, it became an option. As mentioned elsewhere, it 
provides "hints" at 'compile time'.




As I am using pycharm, looks like I need a plugin for mypy.
There are two of them out there:
non official: https://plugins.jetbrains.com/plugin/11086-mypy
and official: https://plugins.jetbrains.com/plugin/13348-mypy-official-

Do you have any experience with any of these plugins?
Please share your opinion.


I don't have a competent opinion for the current Pycharm, but hope that 
others will answer.


TBH I'd be surprised if it doesn't come 'baked in'.
--
Regards =dn
--
https://mail.python.org/mailman/listinfo/python-list


Re: Typing, how come that :int is not ensuring int parameter?

2020-06-11 Thread zljubisic
OK, as I can see nothing is enforced but I can use mypy and tests for the 
purpose.

As I am using pycharm, looks like I need a plugin for mypy.
There are two of them out there:
non official: https://plugins.jetbrains.com/plugin/11086-mypy
and official: https://plugins.jetbrains.com/plugin/13348-mypy-official-

Do you have any experience with any of these plugins?
Please share your opinion.

Best regards.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Typing, how come that :int is not ensuring int parameter?

2020-06-11 Thread DL Neil via Python-list

On 12/06/20 8:51 AM, zljubi...@gmail.com wrote:

Hi,

If I run this code:
class Property:

 def __init__(self, var: int):
 self.a: int = var

 @property
 def a(self):
 return self.__a

 @a.setter
 def a(self, var: int):
 if var > 0 and var % 2 == 0:
 self.__a = var
 else:
 self.__a = 2

if __name__ == '__main__':
 x = Property(1.5)
 print(x.a)

I am getting 2.
How come, if I have created a class with :int in order to ensure that "a" 
property must be an integer, that I can create an instance with float (1.5)?



You may be confusing Python with some other programming language 
previously-learned. We need to 're-wire' the way your mind is working 
because whilst you are not-wrong for that-language, you are not-right 
for Python.


So, I'm not going to answer your question directly, but to offer you a 
learning-path:-


What did mypy (or...) say, when you ran tests against the source-code?
(Typing is no use without such a testing regime!)

Have you read the docs for Typing?
- and for extra bonus-points, the numerous PEPs (proposals and accepted) 
related to how Python implements Typing?
(that done, you will likely answer your own question, and accumulate 
some useful learning about Python - at both the practical and 
philosophical/idiomatic levels)

--
Regards =dn
--
https://mail.python.org/mailman/listinfo/python-list


Re: Typing, how come that :int is not ensuring int parameter?

2020-06-11 Thread Barry Scott



> On 11 Jun 2020, at 21:51, zljubi...@gmail.com wrote:
> 
> Hi,
> 
> If I run this code:
> class Property:
> 
>def __init__(self, var: int):
>self.a: int = var
> 
>@property
>def a(self):
>return self.__a
> 
>@a.setter
>def a(self, var: int):
>if var > 0 and var % 2 == 0:
>self.__a = var
>else:
>self.__a = 2
> 
> if __name__ == '__main__':
>x = Property(1.5)
>print(x.a)
> 
> I am getting 2.
> How come, if I have created a class with :int in order to ensure that "a" 
> property must be an integer, that I can create an instance with float (1.5)?

The : int is a type *hint* that isn not enforced by python.

If you use a checker program like  mypy it will use the type hints and report 
problems to your.

You can install myy with pip.

$ python3 -m pip install mypy

I put your code in a.py and then checked it:

$ /Library/Frameworks/Python.framework/Versions/3.8/bin/mypy a.py
a.py:18: error: Argument 1 to "Property" has incompatible type "float"; 
expected "int"
Found 1 error in 1 file (checked 1 source file)

As you can see it found the problem.

Barry



> 
> Regards.
> -- 
> https://mail.python.org/mailman/listinfo/python-list
> 

-- 
https://mail.python.org/mailman/listinfo/python-list


Typing, how come that :int is not ensuring int parameter?

2020-06-11 Thread zljubisic
Hi,

If I run this code:
class Property:

def __init__(self, var: int):
self.a: int = var

@property
def a(self):
return self.__a

@a.setter
def a(self, var: int):
if var > 0 and var % 2 == 0:
self.__a = var
else:
self.__a = 2

if __name__ == '__main__':
x = Property(1.5)
print(x.a)

I am getting 2.
How come, if I have created a class with :int in order to ensure that "a" 
property must be an integer, that I can create an instance with float (1.5)?

Regards.
-- 
https://mail.python.org/mailman/listinfo/python-list