[issue40864] spec_set/autospec/spec seems to not be reading attributes defined in class body
Karthikeyan Singaravelan
report at bugs.python.org
Fri Jun 5 00:06:35 EDT 2020
Karthikeyan Singaravelan <tir.karthi at gmail.com> added the comment:
mock uses dir to iterate through the attributes that needs to be specced [0]. Unless the variable is initialized it's not listed in dir. Below is an example where age is initialized and name is not. name is not present in dir(Person) and hence spec will not be able to detect this. This is similar to https://bugs.python.org/issue36580.
cat /tmp/baz.py
class Person:
name: str
age: int = 10
print(dir(Person))
print(Person.name)
python /tmp/baz.py
['__annotations__', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'age']
Traceback (most recent call last):
File "/tmp/baz.py", line 6, in <module>
print(Person.name)
AttributeError: type object 'Person' has no attribute 'name'
[0] https://github.com/python/cpython/blob/e005ead49b1ee2b1507ceea94e6f89c28ecf1f81/Lib/unittest/mock.py#L2647
----------
components: +Library (Lib) -Tests
nosy: +xtreak
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue40864>
_______________________________________
More information about the Python-bugs-list
mailing list