Mailing List Archive

typing instance variable
Greetings,

This snippet of code raises ValueError during execution:

class MyClass:
__slots__ = 'foo'
foo: int = 0

mypy does not complain:

mypy myclass.py
Success: no issues found in 1 source file

But python3 does:

python3 myclass.py
Traceback (most recent call last):
File "myclass.py", line 1, in <module>
class MyClass:
ValueError: 'foo' in __slots__ conflicts with class variable

This is where the way of type declaration of instance variable comes from:

https://mypy.readthedocs.io/en/stable/cheat_sheet_py3.html
class MyClass:
# You can optionally declare instance variables in the class body
attr: int
# This is an instance variable with a default value
charge_percent: int = 100

Why Python believes foo is class variable?

python3 -V
Python 3.6.8

Kind regards,
David
--
https://mail.python.org/mailman/listinfo/python-list