[issue40864] spec_set/autospec/spec seems to not be reading attributes defined in class body

2020-10-05 Thread Evan Fagerberg


Change by Evan Fagerberg :


--
keywords: +patch
pull_requests: +21567
stage:  -> patch review
pull_request: https://github.com/python/cpython/pull/22572

___
Python tracker 
<https://bugs.python.org/issue40864>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40864] spec_set/autospec/spec seems to not be reading attributes defined in class body

2020-10-05 Thread Evan Fagerberg


Evan Fagerberg  added the comment:

Reflecting on it more, there should be a sensible way to retrieve the set 
attributes of the init method of any class without explicitly instantiating it, 
via the inspect module.

--

___
Python tracker 
<https://bugs.python.org/issue40864>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40864] spec_set/autospec/spec seems to not be reading attributes defined in class body

2020-06-06 Thread Evan Fagerberg


Evan Fagerberg  added the comment:

Rereading the documentation, I see that a class attribute set to null will 
return a MagicMock for that attribute. That might be a reasonable workaround. 
Perhaps the more concrete solution would be that dir lists uninitialized class 
attributes and if a type hint is present the class attributes uses a spec of 
that type hint.

--

___
Python tracker 
<https://bugs.python.org/issue40864>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40864] spec_set/autospec/spec seems to not be reading attributes defined in class body

2020-06-04 Thread Evan Fagerberg


Evan Fagerberg  added the comment:

Sorry one small note, the error in the example happens on
```python
logger.propagate = False
```

and not
```python
assert logger.propagate is False
```

--

___
Python tracker 
<https://bugs.python.org/issue40864>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40864] spec_set/autospec/spec seems to not be reading attributes defined in class body

2020-06-04 Thread Evan Fagerberg


New submission from Evan Fagerberg :

Hello, I really like that this library allows for really strict mocking however 
one thing I have noticed is that it seems like using spec on a mock does not 
properly read the class body for attributes like some of the documentation 
claims. For example this is a snippet of the Logger class in python 3.6's 
`logging` module
```python
class Logger(Filterer):
name: str
level: int
parent: Union[Logger, PlaceHolder]
propagate: bool
handlers: List[Handler]
disabled: int
```

Now I want to mock that class ensuring that propagate gets set to False for 
example
```python
from unittest import mock
from logging import Logger

logger = mock.Mock(spec_set=Logger)
logger.propagate = False
assert logger.propagate is False
*** AttributeError: Mock object has no attribute 'propagate'
```

I have noticed this does work when the value is initialized in the class body 
so for example

```python
class Logger(Filterer):
name: str
level: int
parent: Union[Logger, PlaceHolder]
propagate: bool = False
handlers: List[Handler]
disabled: int
```

This would not fail with the test in question.

Wondering if this is intended behavior or not or if I am misunderstanding 
something. I have tested this with Python 3.6.10, 3.8.2, all with the same 
result.

--
components: Tests
messages: 370712
nosy: efagerberg
priority: normal
severity: normal
status: open
title: spec_set/autospec/spec seems to not be reading attributes defined in 
class body
type: behavior
versions: Python 3.6, Python 3.8, Python 3.9

___
Python tracker 
<https://bugs.python.org/issue40864>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com